From e0972d3192fed75262f4ac48a113c391e696c350 Mon Sep 17 00:00:00 2001 From: MadCcc <1075746765@qq.com> Date: Mon, 22 Apr 2024 18:11:36 +0800 Subject: [PATCH] feat: use prerender html directly in ssg (#12317) * feat: use prerender html directly in ssg * fix: ssg * fix: add bootstrap script --- examples/ssr-demo/.umirc.ts | 1 + .../src/features/exportStatic/exportStatic.ts | 32 ++----------------- packages/server/src/ssr.ts | 1 + 3 files changed, 4 insertions(+), 30 deletions(-) diff --git a/examples/ssr-demo/.umirc.ts b/examples/ssr-demo/.umirc.ts index f5a95e511b89..53274d948833 100644 --- a/examples/ssr-demo/.umirc.ts +++ b/examples/ssr-demo/.umirc.ts @@ -11,6 +11,7 @@ export default { builder: 'webpack', renderFromRoot: false, }, + exportStatic: {}, styles: [`body { color: red; }`, `https://a.com/b.css`], metas: [ diff --git a/packages/preset-umi/src/features/exportStatic/exportStatic.ts b/packages/preset-umi/src/features/exportStatic/exportStatic.ts index 9ceb55d72b19..ae0d2f9be2b2 100644 --- a/packages/preset-umi/src/features/exportStatic/exportStatic.ts +++ b/packages/preset-umi/src/features/exportStatic/exportStatic.ts @@ -64,25 +64,9 @@ async function getPreRenderedHTML(api: IApi, htmlTpl: string, path: string) { markupRender ??= require(absServerBuildPath(api))._markupGenerator; try { - const markup = await markupRender(path); - const [mainTpl, extraTpl = ''] = markup.split(''); - // TODO: improve return type for markup generator - const helmetContent = mainTpl.match( - /[^]*?(<[^>]+data-rh[^]+)<\/head>/, - )?.[1]; - const bodyContent = mainTpl.match(/]*>([^]+?)<\/body>/)?.[1]; - - htmlTpl = htmlTpl - // append helmet content - .replace('', `${helmetContent || ''}`) - // replace #root with pre-rendered body content - .replace( - new RegExp(`
]*>.*?
`), - bodyContent, - ) - // append hidden templates - .replace(/$/, `${extraTpl}`); + const html = await markupRender(path); logger.info(`Pre-render for ${path}`); + return html; } catch (err) { logger.error(`Pre-render ${path} error: ${err}`); if (!ignorePreRenderError) { @@ -149,22 +133,10 @@ export default (api: IApi) => { const htmlData = api.appData.exportHtmlData; const htmlFiles: { path: string; content: string }[] = []; const { markupArgs: defaultMarkupArgs } = opts; - let asyncMarkupArgs: typeof defaultMarkupArgs; for (const { file, route, prerender } of htmlData) { let markupArgs = defaultMarkupArgs; - // mark async for the scripts of pre-rendered html - if (api.config.ssr && prerender) { - // copy args to avoid affect original object - markupArgs = asyncMarkupArgs ??= { - ...markupArgs, - scripts: markupArgs.scripts.map((script: any) => - script.src ? { ...script, async: true } : script, - ), - }; - } - // handle relative publicPath, such as `./` if (publicPath.startsWith('.')) { assert( diff --git a/packages/server/src/ssr.ts b/packages/server/src/ssr.ts index 08db25040205..59dc5a8b1893 100644 --- a/packages/server/src/ssr.ts +++ b/packages/server/src/ssr.ts @@ -252,6 +252,7 @@ export function createMarkupGenerator(opts: CreateRequestHandlerOptions) { onShellReady() { stream.pipe(writable); }, + bootstrapScripts: [jsx.manifest.assets['umi.js'] || '/umi.js'], onError: reject, }, );