Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Next.js prefix the app.js with an underscore?

Tags:

next.js

When I create a Next.js project with the npx create-next-app command than there is an _app.js file under the pages folder. In which case do I need to prefix a file with an underscore in a Next.js project?

like image 805
Niklas Weber Avatar asked Jan 23 '21 08:01

Niklas Weber


People also ask

What does App js do in NextJS?

Next.js uses the App component to initialize pages. You can override it and control the page initialization. Which allows you to do amazing things like: Persisting layout between page changes.

Should I use underscore in JavaScript?

The underscore _ is simply an acceptable character for variable/function names; it has no additional functionality. Underscore variable is the standard for private variables and methods. There is no true class privacy in JavaScript.

What is underscore in React?

Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects.

Can you use getServerSideProps in _APP js?

getServerSideProps does not work in _app. js . see docs. you could use the older getInitialProps in your custom app component but then the automatic static optimisation is disabled, which is something Next.


Video Answer


2 Answers

  • Custom App

Next.js uses the App component to initialize pages.

To override the default App, create the file ./pages/_app.js


  • Custom Document

A custom Document is commonly used to augment your application's <html> and <body> tags.

To override the default Document, create the file ./pages/_document.js

like image 167
itsazzad Avatar answered Sep 28 '22 04:09

itsazzad


You're only gonna need that prefix in the default nextjs pages like _app.js, the _document.js, etc. you can look it up in the Next.js Docs, I think the prefix is used to avoid routes like "/app" or "/document", and let them free in case you need use them for your project.

like image 44
edgarlr Avatar answered Sep 28 '22 04:09

edgarlr