Skip to content

Commit a7a4573

Browse files
authored
fix: use entryName to ensure match the target route correctly (#6706)
1 parent 5fd59c6 commit a7a4573

File tree

11 files changed

+66
-8
lines changed

11 files changed

+66
-8
lines changed

.changeset/slimy-rocks-mix.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+
fix: use entryName to ensure match the target route correctly after router.rewrite
6+
fix: 通过 entryName 确保 router.rewrite 后能匹配到正确的路由

packages/server/core/src/plugins/customServer/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export class CustomServer {
9292
const rewriteRoute = routes.find(route => route.entryName === current);
9393
if (rewriteRoute) {
9494
c.set('matchPathname', rewriteRoute.urlPath);
95+
c.set('matchEntryName', current);
9596
}
9697
}
9798

packages/server/core/src/plugins/render/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ function createRenderHandler(
120120
const locals = c.get('locals');
121121
const metrics = c.get('metrics');
122122
const matchPathname = c.get('matchPathname');
123+
const matchEntryName = c.get('matchEntryName');
123124
const loaderContext = getLoaderCtx(c as Context);
124125

125126
const request = c.req.raw;
@@ -136,6 +137,7 @@ function createRenderHandler(
136137
loaderContext,
137138
locals,
138139
matchPathname,
140+
matchEntryName,
139141
});
140142

141143
const { body, status, headers } = res;

packages/server/core/src/plugins/render/render.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,25 @@ function getRouter(routes: ServerRoute[]): Router<ServerRoute> {
7272
return router;
7373
}
7474

75+
type MatchedRoute = [ServerRoute, Params];
7576
function matchRoute(
7677
router: Router<ServerRoute>,
7778
pathname: string,
78-
): [ServerRoute, Params] {
79+
entryName?: string,
80+
): MatchedRoute {
7981
const matched = router.match('*', pathname);
80-
81-
const result = matched[0][0];
82-
83-
// here we can parse the dynamic server routes params
84-
return result || [];
82+
// For route rewrite in server.ts
83+
// If entryName is existed and the pathname matched multiple routes, we use entryName to find the target route
84+
if (entryName && matched[0].length > 1) {
85+
const matches: Array<MatchedRoute> = matched[0];
86+
const result = matches.find(
87+
([route]) => route.entryName === entryName,
88+
) as MatchedRoute;
89+
return result || [];
90+
} else {
91+
const result = matched[0][0];
92+
return result || [];
93+
}
8594
}
8695

8796
function getHeadersWithoutCookie(headers: Record<string, any>) {
@@ -117,12 +126,17 @@ export async function createRender({
117126
templates,
118127
serverManifest,
119128
locals,
129+
matchEntryName,
120130
matchPathname,
121131
loaderContext,
122132
},
123133
) => {
124134
const forMatchpathname = matchPathname ?? getPathname(req);
125-
const [routeInfo, params] = matchRoute(router, forMatchpathname);
135+
const [routeInfo, params] = matchRoute(
136+
router,
137+
forMatchpathname,
138+
matchEntryName,
139+
);
126140
const framework = metaName || 'modern-js';
127141
const fallbackHeader = `x-${cutNameByHyphen(framework)}-ssr-fallback`;
128142
let fallbackReason = null;

packages/server/core/src/types/render.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ export interface RenderOptions {
2323
/** For compat rewrite MPA, while not modify request */
2424
matchPathname?: string;
2525

26+
/** For compat rewrite MPA, while not modify request */
27+
matchEntryName?: string;
28+
2629
monitors?: Monitors;
2730

2831
serverManifest: ServerManifest;

packages/server/core/src/types/server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ type ServerVariables = {
6363
templates?: Record<string, string>;
6464

6565
matchPathname?: string;
66+
67+
matchEntryName?: string;
6668
/**
6769
* Communicating with custom server hook & modern ssrContext.
6870
*

tests/integration/server-hook/router/modern.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export default applyBaseConfig({
99
},
1010
server: {
1111
routes: {
12+
pc: '/',
13+
mobile: '/',
1214
home: '/rewrite',
1315
entry: '/redirect',
1416
},

tests/integration/server-hook/router/server/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export const afterMatch: AfterMatchHook = (ctx, next) => {
88
router.rewrite('entry');
99
} else if (pathname === '/redirect') {
1010
router.redirect('https://modernjs.dev/', 302);
11+
} else {
12+
router.rewrite('pc');
1113
}
1214

1315
next();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from 'react';
2+
3+
function App() {
4+
return <div>Mobile Page</div>;
5+
}
6+
7+
export default App;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from 'react';
2+
3+
function App() {
4+
return <div>PC Page</div>;
5+
}
6+
7+
export default App;

tests/integration/server-hook/router/tests/index.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('test status code page', () => {
3535
await browser.close();
3636
});
3737

38-
test.skip('should router rewrite correctly ', async () => {
38+
test('should router rewrite correctly ', async () => {
3939
await page.goto(`http://localhost:${port}/rewrite`);
4040
const text = await page.$eval('#root', el => el?.textContent);
4141
expect(text).toMatch('Entry Page');
@@ -48,4 +48,16 @@ describe('test status code page', () => {
4848
const text = await response!.text();
4949
expect(text).toMatch('Modern Web Development');
5050
});
51+
52+
test('should router redirect correctly ', async () => {
53+
await page.goto(`http://localhost:${port}/mobile`);
54+
const text = await page.$eval('#root', el => el?.textContent);
55+
expect(text).toMatch('PC Page');
56+
});
57+
58+
test('should router redirect correctly ', async () => {
59+
await page.goto(`http://localhost:${port}/pc`);
60+
const text = await page.$eval('#root', el => el?.textContent);
61+
expect(text).toMatch('PC Page');
62+
});
5163
});

0 commit comments

Comments
 (0)