_document.page.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* eslint-disable @next/next/google-font-display */
  2. import React from 'react';
  3. import Document, {
  4. DocumentContext, DocumentInitialProps,
  5. Html, Head, Main, NextScript,
  6. } from 'next/document';
  7. import type { GrowiPluginResourceEntries } from '~/features/growi-plugin/server/services';
  8. import type { CrowiRequest } from '~/interfaces/crowi-request';
  9. import loggerFactory from '~/utils/logger';
  10. const logger = loggerFactory('growi:page:_document');
  11. type HeadersForGrowiPluginProps = {
  12. pluginResourceEntries: GrowiPluginResourceEntries;
  13. }
  14. const HeadersForGrowiPlugin = (props: HeadersForGrowiPluginProps): JSX.Element => {
  15. const { pluginResourceEntries } = props;
  16. return (
  17. <>
  18. { pluginResourceEntries.map(([installedPath, href]) => {
  19. if (href.endsWith('.js')) {
  20. // eslint-disable-next-line @next/next/no-sync-scripts
  21. return <script type="module" key={`script_${installedPath}`} src={href} />;
  22. }
  23. if (href.endsWith('.css')) {
  24. // eslint-disable-next-line @next/next/no-sync-scripts
  25. return <link rel="stylesheet" key={`link_${installedPath}`} href={href} />;
  26. }
  27. return <></>;
  28. }) }
  29. </>
  30. );
  31. };
  32. interface GrowiDocumentProps {
  33. themeHref: string,
  34. customScript: string | null,
  35. customCss: string | null,
  36. customNoscript: string | null,
  37. pluginResourceEntries: GrowiPluginResourceEntries;
  38. }
  39. declare type GrowiDocumentInitialProps = DocumentInitialProps & GrowiDocumentProps;
  40. class GrowiDocument extends Document<GrowiDocumentInitialProps> {
  41. static override async getInitialProps(ctx: DocumentContext): Promise<GrowiDocumentInitialProps> {
  42. const initialProps: DocumentInitialProps = await Document.getInitialProps(ctx);
  43. const { crowi } = ctx.req as CrowiRequest;
  44. const { customizeService } = crowi;
  45. const { themeHref } = customizeService;
  46. const customScript: string | null = customizeService.getCustomScript();
  47. const customCss: string | null = customizeService.getCustomCss();
  48. const customNoscript: string | null = customizeService.getCustomNoscript();
  49. // retrieve plugin manifests
  50. const growiPluginService = await import('~/features/growi-plugin/server/services').then(mod => mod.growiPluginService);
  51. const pluginResourceEntries = await growiPluginService.retrieveAllPluginResourceEntries();
  52. return {
  53. ...initialProps,
  54. themeHref,
  55. customScript,
  56. customCss,
  57. customNoscript,
  58. pluginResourceEntries,
  59. };
  60. }
  61. renderCustomScript(customScript: string | null): JSX.Element {
  62. if (customScript == null || customScript.length === 0) {
  63. return <></>;
  64. }
  65. return <script id="customScript" dangerouslySetInnerHTML={{ __html: customScript }} />;
  66. }
  67. renderCustomCss(customCss: string | null): JSX.Element {
  68. if (customCss == null || customCss.length === 0) {
  69. return <></>;
  70. }
  71. return <style dangerouslySetInnerHTML={{ __html: customCss }} />;
  72. }
  73. renderCustomNoscript(customNoscript: string | null): JSX.Element {
  74. if (customNoscript == null || customNoscript.length === 0) {
  75. return <></>;
  76. }
  77. return <noscript dangerouslySetInnerHTML={{ __html: customNoscript }} />;
  78. }
  79. override render(): JSX.Element {
  80. const {
  81. customCss, customScript, customNoscript,
  82. themeHref, pluginResourceEntries,
  83. } = this.props;
  84. return (
  85. <Html>
  86. <Head>
  87. {this.renderCustomScript(customScript)}
  88. <link rel="stylesheet" key="link-theme" href={themeHref} />
  89. <HeadersForGrowiPlugin pluginResourceEntries={pluginResourceEntries} />
  90. {this.renderCustomCss(customCss)}
  91. </Head>
  92. <body>
  93. {this.renderCustomNoscript(customNoscript)}
  94. <Main />
  95. <NextScript />
  96. </body>
  97. </Html>
  98. );
  99. }
  100. }
  101. export default GrowiDocument;