/* eslint-disable @next/next/google-font-display */
import React from 'react';
import Document, {
DocumentContext, DocumentInitialProps,
Html, Head, Main, NextScript,
} from 'next/document';
import type { CrowiRequest } from '~/interfaces/crowi-request';
import type { IPluginService, GrowiPluginResourceEntries } from '~/server/service/plugin';
import loggerFactory from '~/utils/logger';
const logger = loggerFactory('growi:page:_document');
type HeadersForGrowiPluginProps = {
pluginResourceEntries: GrowiPluginResourceEntries;
}
const HeadersForGrowiPlugin = (props: HeadersForGrowiPluginProps): JSX.Element => {
const { pluginResourceEntries } = props;
return (
<>
{ pluginResourceEntries.map(([installedPath, href]) => {
if (href.endsWith('.js')) {
// eslint-disable-next-line @next/next/no-sync-scripts
return ;
}
if (href.endsWith('.css')) {
// eslint-disable-next-line @next/next/no-sync-scripts
return ;
}
return <>>;
}) }
>
);
};
interface GrowiDocumentProps {
themeHref: string,
customScript: string | null,
customCss: string | null,
customNoscript: string | null,
pluginResourceEntries: GrowiPluginResourceEntries;
}
declare type GrowiDocumentInitialProps = DocumentInitialProps & GrowiDocumentProps;
class GrowiDocument extends Document {
static override async getInitialProps(ctx: DocumentContext): Promise {
const initialProps: DocumentInitialProps = await Document.getInitialProps(ctx);
const { crowi } = ctx.req as CrowiRequest;
const { customizeService, pluginService } = crowi;
const { themeHref } = customizeService;
const customScript: string | null = customizeService.getCustomScript();
const customCss: string | null = customizeService.getCustomCss();
const customNoscript: string | null = customizeService.getCustomNoscript();
// retrieve plugin manifests
const pluginResourceEntries = await (pluginService as IPluginService).retrieveAllPluginResourceEntries();
return {
...initialProps,
themeHref,
customScript,
customCss,
customNoscript,
pluginResourceEntries,
};
}
renderCustomScript(customScript: string | null): JSX.Element {
if (customScript == null || customScript.length === 0) {
return <>>;
}
return ;
}
renderCustomCss(customCss: string | null): JSX.Element {
if (customCss == null || customCss.length === 0) {
return <>>;
}
return ;
}
renderCustomNoscript(customNoscript: string | null): JSX.Element {
if (customNoscript == null || customNoscript.length === 0) {
return <>>;
}
return ;
}
override render(): JSX.Element {
const {
customCss, customScript, customNoscript,
themeHref, pluginResourceEntries,
} = this.props;
return (
{this.renderCustomScript(customScript)}
{this.renderCustomCss(customCss)}
{this.renderCustomNoscript(customNoscript)}
);
}
}
export default GrowiDocument;