_document.page.tsx 4.1 KB

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