_document.page.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 { CrowiRequest } from '~/interfaces/crowi-request';
  8. import type { IPluginService, GrowiPluginResourceEntries } from '~/server/service/plugin';
  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<any>;
  44. const { customizeService, pluginService } = 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 pluginResourceEntries = await (pluginService as IPluginService).retrieveAllPluginResourceEntries();
  51. return {
  52. ...initialProps,
  53. themeHref,
  54. customScript,
  55. customCss,
  56. customNoscript,
  57. pluginResourceEntries,
  58. };
  59. }
  60. renderCustomScript(customScript: string | null): JSX.Element {
  61. if (customScript == null || customScript.length === 0) {
  62. return <></>;
  63. }
  64. return <script id="customScript" dangerouslySetInnerHTML={{ __html: customScript }} />;
  65. }
  66. renderCustomCss(customCss: string | null): JSX.Element {
  67. if (customCss == null || customCss.length === 0) {
  68. return <></>;
  69. }
  70. return <style dangerouslySetInnerHTML={{ __html: customCss }} />;
  71. }
  72. renderCustomNoscript(customNoscript: string | null): JSX.Element {
  73. if (customNoscript == null || customNoscript.length === 0) {
  74. return <></>;
  75. }
  76. return <noscript dangerouslySetInnerHTML={{ __html: customNoscript }} />;
  77. }
  78. override render(): JSX.Element {
  79. const {
  80. customCss, customScript, customNoscript,
  81. themeHref, pluginResourceEntries,
  82. } = this.props;
  83. return (
  84. <Html>
  85. <Head>
  86. {this.renderCustomScript(customScript)}
  87. <link rel='preload' href="/static/fonts/PressStart2P-latin.woff2" as="font" type="font/woff2" />
  88. <link rel='preload' href="/static/fonts/PressStart2P-latin-ext.woff2" as="font" type="font/woff2" />
  89. <link rel='preload' href="/static/fonts/Lato-Regular-latin.woff2" as="font" type="font/woff2" />
  90. <link rel='preload' href="/static/fonts/Lato-Regular-latin-ext.woff2" as="font" type="font/woff2" />
  91. <link rel='preload' href="/static/fonts/Lato-Bold-latin.woff2" as="font" type="font/woff2" />
  92. <link rel='preload' href="/static/fonts/Lato-Bold-latin-ext.woff2" as="font" type="font/woff2" />
  93. <link rel="stylesheet" key="link-theme" href={themeHref} />
  94. <HeadersForGrowiPlugin pluginResourceEntries={pluginResourceEntries} />
  95. {this.renderCustomCss(customCss)}
  96. </Head>
  97. <body>
  98. {this.renderCustomNoscript(customNoscript)}
  99. <Main />
  100. <NextScript />
  101. </body>
  102. </Html>
  103. );
  104. }
  105. }
  106. export default GrowiDocument;