Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next.js use of _app.js and _document.js

Tags:

next.js

I currently use _document.js to inject css into my Next.js app and I would like to start using _app.js to help inject data into pages.

Was wondering if it possible to use _document and _app in tandem or should we only use one or the other?

like image 516
ramesh Avatar asked Jun 26 '18 10:06

ramesh


People also ask

What is _APP and _document in NextJS?

They serve different purpose and can be used in the same application. According to NextJS docs: Next. js uses the App component to initialize pages. To override, create the ./pages/_app.js file and override the App class.

What does _APP js do in NextJS?

Using _app. js will be rendered on every page. this is a good way to do custom layouts without having to wrap each page component in your /pages directory.

How use CDN link in NextJS?

To set up a CDN, you can set up an asset prefix and configure your CDN's origin to resolve to the domain that Next. js is hosted on. The exact configuration for uploading your files to a given CDN will depend on your CDN of choice. The only folder you need to host on your CDN is the contents of .


1 Answers

Short answer: Yes, You can use both. They serve different purpose and can be used in the same application.

According to NextJS docs:

Next.js uses the App component to initialize pages.

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

and

Pages in Next.js skip the definition of the surrounding document's markup. For example, you never include <html>, <body>, etc. To override that default behavior, you must create a file at ./pages/_document.js, where you can extend the Document class.

Note: _document.js is only rendered on the server side and not on the client side. so event handlers like onClick is not going to work.

like image 165
MoHo Avatar answered Sep 16 '22 14:09

MoHo