Skip to content

Commit

Permalink
feat: use prerender html directly in ssg (#12317)
Browse files Browse the repository at this point in the history
* feat: use prerender html directly in ssg

* fix: ssg

* fix: add bootstrap script
  • Loading branch information
MadCcc authored Apr 22, 2024
1 parent e8735e4 commit e0972d3
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 30 deletions.
1 change: 1 addition & 0 deletions examples/ssr-demo/.umirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default {
builder: 'webpack',
renderFromRoot: false,
},
exportStatic: {},
styles: [`body { color: red; }`, `https://a.com/b.css`],

metas: [
Expand Down
32 changes: 2 additions & 30 deletions packages/preset-umi/src/features/exportStatic/exportStatic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('</html>');
// TODO: improve return type for markup generator
const helmetContent = mainTpl.match(
/<head>[^]*?(<[^>]+data-rh[^]+)<\/head>/,
)?.[1];
const bodyContent = mainTpl.match(/<body[^>]*>([^]+?)<\/body>/)?.[1];

htmlTpl = htmlTpl
// append helmet content
.replace('</head>', `${helmetContent || ''}</head>`)
// replace #root with pre-rendered body content
.replace(
new RegExp(`<div id="${api.config.mountElementId}"[^>]*>.*?</div>`),
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) {
Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions packages/server/src/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ export function createMarkupGenerator(opts: CreateRequestHandlerOptions) {
onShellReady() {
stream.pipe(writable);
},
bootstrapScripts: [jsx.manifest.assets['umi.js'] || '/umi.js'],
onError: reject,
},
);
Expand Down

0 comments on commit e0972d3

Please sign in to comment.