Skip to content

Commit e5b16df

Browse files
authored
chore: should load html template while server preparing (#6643)
1 parent 94b4212 commit e5b16df

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

.changeset/unlucky-boats-own.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@modern-js/server-core': patch
3+
---
4+
5+
chore: should load html template while server preparing
6+
chore: 在 server 准备时应该加载 html 模板

packages/server/core/src/adapters/node/plugins/resource.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ export async function getHtmlTemplates(pwd: string, routes: ServerRoute[]) {
4141
export function injectTemplates(
4242
pwd: string,
4343
routes?: ServerRoute[],
44+
htmlTemplatePromise?: ReturnType<typeof getHtmlTemplates>,
4445
): Middleware<ServerEnv> {
4546
return async (c, next) => {
4647
if (routes && !c.get('templates')) {
47-
const templates = await getHtmlTemplates(pwd, routes);
48+
const templates = await (htmlTemplatePromise ||
49+
getHtmlTemplates(pwd, routes));
4850
c.set('templates', templates);
4951
}
5052

@@ -149,9 +151,13 @@ export const injectResourcePlugin = (): ServerPlugin => ({
149151
async prepare() {
150152
const { middlewares, routes, distDirectory: pwd } = api.useAppContext();
151153

154+
let htmlTemplatePromise:
155+
| ReturnType<typeof getHtmlTemplates>
156+
| undefined;
152157
// In Production, should warmup server bundles on prepare.
153158
if (isProd()) {
154159
getServerManifest(pwd, routes || [], console);
160+
htmlTemplatePromise = getHtmlTemplates(pwd, routes || []);
155161
}
156162

157163
middlewares.push({
@@ -163,7 +169,7 @@ export const injectResourcePlugin = (): ServerPlugin => ({
163169
middlewares.push({
164170
name: 'inject-html',
165171

166-
handler: injectTemplates(pwd, routes),
172+
handler: injectTemplates(pwd, routes, htmlTemplatePromise),
167173
});
168174
},
169175
};

0 commit comments

Comments
 (0)