_document.page.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import React from 'react';
  2. import fs from 'fs';
  3. import Document, {
  4. DocumentContext, DocumentInitialProps,
  5. Html, Head, Main, NextScript,
  6. } from 'next/document';
  7. // import { renderScriptTagsByGroup, renderStyleTagsByGroup } from '~/service/cdn-resources-loader';
  8. import { resolveFromRoot } from '~/utils/project-dir-utils';
  9. interface GrowiDocumentProps {
  10. bootJsPath: string;
  11. }
  12. declare type GrowiDocumentInitialProps = DocumentInitialProps & GrowiDocumentProps;
  13. async function importCustomManifest(): Promise<any> {
  14. const customManifestStr: string = await fs.readFileSync(resolveFromRoot('.next/custom-manifest.json'), 'utf-8');
  15. return JSON.parse(customManifestStr);
  16. }
  17. class GrowiDocument extends Document<GrowiDocumentProps> {
  18. static override async getInitialProps(ctx: DocumentContext): Promise<GrowiDocumentInitialProps> {
  19. const initialProps: DocumentInitialProps = await Document.getInitialProps(ctx);
  20. const customManifest: any = await importCustomManifest();
  21. const bootJsPath = customManifest['boot.js'];
  22. return { ...initialProps, bootJsPath };
  23. }
  24. override render(): JSX.Element {
  25. const { bootJsPath } = this.props;
  26. return (
  27. <Html>
  28. <Head>
  29. {/* eslint-disable-next-line @next/next/no-sync-scripts */}
  30. <script src={bootJsPath}></script>
  31. {/*
  32. {renderScriptTagsByGroup('basis')}
  33. {renderStyleTagsByGroup('basis')}
  34. */}
  35. </Head>
  36. <body>
  37. <Main />
  38. <NextScript />
  39. </body>
  40. </Html>
  41. );
  42. }
  43. }
  44. export default GrowiDocument;