Seeking assistance in understanding why my _document.tsx
is not loading properly within my nextJS application.
My Attempts So Far
I have been diligently following the NextJS documentation for creating a custom _document.js. Despite my efforts, I am unable to make my NextJS app recognize the page. I also attempted a similar approach using _app.js
. It's worth mentioning that we use TypeScript so the files are saved as .tsx
, etc.
This is how my default folder structure looks like -- apparently, we didn't have a root /pages
folder at all, contrary to what the documentation recommends. All our application components reside within /src
and /src/app
.
https://i.stack.imgur.com/v2RKU.png
You can see from the folder structure that I tried:
- Putting it in
projectName/src/pages/_document.tsx
- Trying it in
projectName/pages/_document.tsx
- Experimenting with
projectName/src/app/pages/_document.tsx
- Even attempting to place
_document.tsx
directly in both theprojectName/src
root andprojectName/src/app
root.
Unfortunately, none of these attempts seem to work: No matter what I do, my _document.tsx
just won't load.
Here's the content of my _document.tsx
file, containing the script that should ideally be loaded into the section of the application.
As of now, I have a workaround in place, but I anticipate that my team will request me to utilize _document.tsx
. Yet, I'm struggling to have my NextJS app locate it (and never seeing the script or console output this is document talking
).
If anyone could shed some light on why I am facing difficulty in getting my _document.tsx
to load correctly, and where I might be going wrong, it would be greatly appreciated.
import Document, { Html, Head, Main, NextScript } from 'next/document';
import Script from 'next/script';
class MyDocument extends Document {
render() {
console.log("this is document talking");
return (
<Html>
<Head>
<Script>
{`window.heap=window.heap||[],heap.load=function(e,t){window.heap.appid=e,window.heap.config=t=t||{};var r=document.createElement("script");r.type="text/javascript",r.async=!0,r.src="https://cdn.heapanalytics.com/js/heap-"+e+".js";var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(r,a);for(var n=function(e){return function(){heap.push([e].concat(Array.prototype.slice.call(arguments,0)))}},p=["addEventProperties","addUserProperties","clearEventProperties","identify","resetIdentity","removeEventProperty","setEventProperties","track","unsetEventProperty"],o=0;o<p.length;o++)heap[p[o]]=n(p[o])};
heap.load("123456789");`}
</Script>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
export default MyDocument;