Where is the public folder for a nextjs project?
I mean, is there somewhere where I can put favicon.png
, google site verification, manifest.json
, robots.txt
, etc. ?
static files such as images, site verification files, and favicons are placed in a public folder of a nextjs root application. the files in the public folder are accessible with the base url. For example, if you placed the robots. txt file in the public folder, It is accessible with the localhost:3000/robots.
in the root directory of your project.
Firstly import the image component in next. js and define the image path in src props in the image component. Now your image shows in the browser. In the image component, src, width, and height props are required by next.
Apps in the standard environment can serve static files from a Google Cloud option like Cloud Storage, serve them directly, or use a third-party content delivery network (CDN). Hosting your static site on Google Cloud can cost less than using a traditional hosting provider, as Google Cloud provides a free tier.
Next.js has a public/
folder
Create stuff like public/favicon.png
, public/robots.txt
and that's all you need.
And put your static
folder inside public
to keep your assets in it with all assets bundling and compressing benefits.
/public /static /images /css /fonts robots.txt manifest.json
Documentation
For web process anything in /public is at the url so easy. However, if you are trying to access the files on the server side of nextjs (either in one of the static async SSR, or as an API call) - as the paths there seem to need to be absolute paths. To access that you need to capture the running directory at build time, and then access it.
next.config.js:
module.exports = { serverRuntimeConfig: { PROJECT_ROOT: __dirname } }
And a helper for getting server side paths:
import path from 'path' import getConfig from 'next/config' const serverPath = (staticFilePath: string) => { return path.join(getConfig().serverRuntimeConfig.PROJECT_ROOT, staticFilePath) } export default serverPath
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With