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?
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.
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.
Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects.
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.
App
Next.js uses the App
component to initialize pages.
To override the default
App
, create the file./pages/_app.js
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
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.
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