_document.page.tsx 4.3 KB

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