_document.page.tsx 4.6 KB

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