Skip to content

Commit

Permalink
fix(server): should get context from unstable middleware correctly (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
yimingjfe authored Oct 16, 2024
1 parent 518b783 commit a9e3eb7
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .changeset/strong-toes-trade.md
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions packages/cli/plugin-data-loader/src/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const handleRequest = async ({
onError?: (error: unknown) => void;
onTiming?: (name: string, dur: number) => void;
context: {
loaderContext?: Map<string, unknown>;
reporter?: Reporter;
};
}): Promise<Response | void> => {
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions packages/server/core/src/plugins/render/dataHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const dataHandler = async (
onError,
onTiming,
serverManifest,
loaderContext,
}: SSRRenderOptions & {
serverRoutes: ServerRoute[];
},
Expand All @@ -28,6 +29,7 @@ export const dataHandler = async (
serverRoutes,
context: {
reporter,
loaderContext,
},
onTiming,
onError,
Expand Down
1 change: 1 addition & 0 deletions packages/server/core/src/types/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type ServerLoaderBundle = {
routes: NestedRoute[];
context: {
reporter?: Reporter;
loaderContext?: Map<string, unknown>;
};

onError?: OnError;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import { Link } from '@modern-js/runtime/router';

export default function Page() {
return <div>Login</div>;
return (
<div>
Login
<Link className="to-home" to="/">
Home
</Link>
</div>
);
}
17 changes: 17 additions & 0 deletions tests/integration/server-hook/new-middleware/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

0 comments on commit a9e3eb7

Please sign in to comment.