Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nextjs public folder

Tags:

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. ?

like image 640
rap-2-h Avatar asked Jan 30 '19 08:01

rap-2-h


People also ask

What is the Public folder in next JS?

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.

Where is next JS build folder?

in the root directory of your project.

Where do I put the next JS image?

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.

Where should I store static images?

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.


2 Answers

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

like image 94
Praphan Klairith Avatar answered Sep 22 '22 13:09

Praphan Klairith


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 
like image 34
BadPirate Avatar answered Sep 21 '22 13:09

BadPirate