Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(prod-server): add handleSSRFallback hook #5376

Merged
merged 3 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/kind-apples-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@modern-js/prod-server': minor
'@modern-js/server-core': minor
---

feat: add server SSR fallback hook
feat: 新增 server SSR 降级 hook 实现
6 changes: 6 additions & 0 deletions packages/server/core/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ const extendContext = createAsyncPipeline<

const handleError = createParallelWorkflow<{ error: Error }>();

const handleSSRFallback = createParallelWorkflow<{
ctx: ModernServerContext;
type: 'query' | 'error' | 'header';
}>();

export type RequestResult = { isfinish: boolean };
const beforeMatch = createAsyncPipeline<
{ context: ModernServerContext },
Expand Down Expand Up @@ -212,6 +217,7 @@ const serverHooks = {
extendSSRContext,
extendContext,
handleError,
handleSSRFallback,
beforeMatch,
afterMatch,
prefetch,
Expand Down
9 changes: 7 additions & 2 deletions packages/server/prod-server/src/libs/render/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ export const createRenderHandler: CreateRenderHandler = ({
}

// handles ssr first
const useCSR =
forceCSR && (ctx.query.csr || ctx.headers[calcFallback(metaName)]);
const queryCSR = ctx.query.csr;
const headerFallback = ctx.headers[calcFallback(metaName)];
const useCSR = forceCSR && (queryCSR || headerFallback);
if (route.isSSR && !useCSR) {
try {
const userAgent = ctx.getReqHeader('User-Agent') as string | undefined;
Expand Down Expand Up @@ -118,7 +119,11 @@ export const createRenderHandler: CreateRenderHandler = ({
(err as Error).stack || (err as Error).message,
);
ctx.res.set(calcFallback(metaName), '1');
await runner.handleSSRFallback({ ctx, type: 'error' });
}
} else if (route.isSSR && useCSR) {
const fallbackType: 'query' | 'header' = queryCSR ? 'query' : 'header';
await runner.handleSSRFallback({ ctx, type: fallbackType });
}

return {
Expand Down
12 changes: 12 additions & 0 deletions packages/server/prod-server/tests/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ describe('test render function', () => {
extendSSRContext: () => {
// empty
},
handleSSRFallback: () => {
// empty
},
} as any,
);

Expand Down Expand Up @@ -121,6 +124,9 @@ describe('test render function', () => {
extendSSRContext: () => {
// empty
},
handleSSRFallback: () => {
// empty
},
} as any,
});
expect(renderResult!.content).toMatch('Modern.js');
Expand Down Expand Up @@ -166,6 +172,9 @@ describe('test render function', () => {
extendSSRContext: () => {
// empty
},
handleSSRFallback: () => {
// empty
},
} as any,
});
expect(renderResult!.content.toString()).toMatch('csr');
Expand Down Expand Up @@ -209,6 +218,9 @@ describe('test render function', () => {
extendSSRContext: () => {
// empty
},
handleSSRFallback: () => {
// empty
},
} as any,
});
expect(renderResult!.content.toString()).toMatch('csr');
Expand Down
Loading