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?
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?
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.jsfile 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.
_app.js is rendered on both server-side and client-side (on the server during the initial SSR, on the client-side after hydration and then on every page/route navigation). _document.js "wraps" _app.js._app.js is only mentioned in the 'Learn' section here: nextjs.org/learn/basics/assets-metadata-css/global-styles Note: you will need to restart your server after adding it.process.env from _document.js?