From a9e3eb7a8e36c4810b1a5902160559213d279543 Mon Sep 17 00:00:00 2001 From: Ming Date: Wed, 16 Oct 2024 15:11:35 +0800 Subject: [PATCH] fix(server): should get context from unstable middleware correctly (#6391) --- .changeset/strong-toes-trade.md | 7 +++++++ .../cli/plugin-data-loader/src/runtime/index.ts | 5 +++-- .../core/src/plugins/render/dataHandler.ts | 2 ++ packages/server/core/src/types/server.ts | 1 + .../new-middleware/src/routes/login/page.tsx | 11 ++++++++++- .../new-middleware/tests/index.test.ts | 17 +++++++++++++++++ 6 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 .changeset/strong-toes-trade.md diff --git a/.changeset/strong-toes-trade.md b/.changeset/strong-toes-trade.md new file mode 100644 index 000000000000..b89fa965f36c --- /dev/null +++ b/.changeset/strong-toes-trade.md @@ -0,0 +1,7 @@ +--- +'@modern-js/plugin-data-loader': patch +'@modern-js/server-core': patch +--- + +fix(server): should get context from unstable middleware correctly +fix(server): 应该正确地获取到 loaderContext diff --git a/packages/cli/plugin-data-loader/src/runtime/index.ts b/packages/cli/plugin-data-loader/src/runtime/index.ts index b0364ba72c3b..812c8a30a144 100644 --- a/packages/cli/plugin-data-loader/src/runtime/index.ts +++ b/packages/cli/plugin-data-loader/src/runtime/index.ts @@ -64,6 +64,7 @@ export const handleRequest = async ({ onError?: (error: unknown) => void; onTiming?: (name: string, dur: number) => void; context: { + loaderContext?: Map; reporter?: Reporter; }; }): Promise => { @@ -77,13 +78,13 @@ export const handleRequest = async ({ const basename = entry.urlPath; const end = time(); - const { reporter } = context; + const { reporter, loaderContext } = context; const routes = transformNestedRoutes(routesConfig, reporter); const { queryRoute } = createStaticHandler(routes, { basename, }); - const requestContext = createRequestContext(); + const requestContext = createRequestContext(loaderContext); // initial requestContext // 1. inject reporter requestContext.set(reporterCtx, reporter); diff --git a/packages/server/core/src/plugins/render/dataHandler.ts b/packages/server/core/src/plugins/render/dataHandler.ts index 638895b54ee8..7c54d39753cd 100644 --- a/packages/server/core/src/plugins/render/dataHandler.ts +++ b/packages/server/core/src/plugins/render/dataHandler.ts @@ -11,6 +11,7 @@ export const dataHandler = async ( onError, onTiming, serverManifest, + loaderContext, }: SSRRenderOptions & { serverRoutes: ServerRoute[]; }, @@ -28,6 +29,7 @@ export const dataHandler = async ( serverRoutes, context: { reporter, + loaderContext, }, onTiming, onError, diff --git a/packages/server/core/src/types/server.ts b/packages/server/core/src/types/server.ts index adcc4b8509cb..c1cf94ba3553 100644 --- a/packages/server/core/src/types/server.ts +++ b/packages/server/core/src/types/server.ts @@ -35,6 +35,7 @@ type ServerLoaderBundle = { routes: NestedRoute[]; context: { reporter?: Reporter; + loaderContext?: Map; }; onError?: OnError; diff --git a/tests/integration/server-hook/new-middleware/src/routes/login/page.tsx b/tests/integration/server-hook/new-middleware/src/routes/login/page.tsx index 407f50a6a704..a9e7e8c5e46d 100644 --- a/tests/integration/server-hook/new-middleware/src/routes/login/page.tsx +++ b/tests/integration/server-hook/new-middleware/src/routes/login/page.tsx @@ -1,3 +1,12 @@ +import { Link } from '@modern-js/runtime/router'; + export default function Page() { - return
Login
; + return ( +
+ Login + + Home + +
+ ); } diff --git a/tests/integration/server-hook/new-middleware/tests/index.test.ts b/tests/integration/server-hook/new-middleware/tests/index.test.ts index 749c17245038..1ecc4ba61c2d 100644 --- a/tests/integration/server-hook/new-middleware/tests/index.test.ts +++ b/tests/integration/server-hook/new-middleware/tests/index.test.ts @@ -96,4 +96,21 @@ describe('test new middleware run correctly', () => { expect(body).toMatch(message); }); + + test('should get loaderContext correctly', async () => { + const url = `http://localhost:${port}/`; + const response = await axios.get(url); + const body = response.data; + expect(body).toMatch('Liming'); + await page.goto(`http://localhost:${port}/login`, { + waitUntil: ['networkidle0'], + }); + + const element = await page.$('.to-home'); + await element?.click(); + await new Promise(resolve => setTimeout(resolve, 1000)); + const rootElm = await page.$('#root'); + const targetText = await page.evaluate(el => el?.textContent, rootElm); + expect(targetText?.trim()).toEqual('Hello Liming'); + }); });