From a53f47b003493f334fbe004fcd48cd30f09b51da Mon Sep 17 00:00:00 2001 From: jinyangwang <214997474@qq.com> Date: Tue, 9 May 2023 20:55:24 +0800 Subject: [PATCH] fix: custom routes redirect (#527) Co-authored-by: Tom <21499747@qq.com> --- .../shuvi-app/react/view/ReactView.server.tsx | 28 ++++++++++++------- pnpm-lock.yaml | 8 +++++- test/e2e/custom-routes.test.ts | 27 ++++++++++++++++++ test/fixtures/custom-routes/package.json | 6 ++++ test/fixtures/custom-routes/shuvi.config.js | 16 +++++++++++ .../custom-routes/src/routes/about/page.js | 1 + .../custom-routes/src/routes/layout.js | 8 ++++++ .../fixtures/custom-routes/src/routes/page.js | 3 ++ 8 files changed, 86 insertions(+), 11 deletions(-) create mode 100644 test/e2e/custom-routes.test.ts create mode 100644 test/fixtures/custom-routes/package.json create mode 100644 test/fixtures/custom-routes/shuvi.config.js create mode 100644 test/fixtures/custom-routes/src/routes/about/page.js create mode 100644 test/fixtures/custom-routes/src/routes/layout.js create mode 100644 test/fixtures/custom-routes/src/routes/page.js diff --git a/packages/platform-web/src/shuvi-app/react/view/ReactView.server.tsx b/packages/platform-web/src/shuvi-app/react/view/ReactView.server.tsx index 7782a76cc..5e4b84fc7 100644 --- a/packages/platform-web/src/shuvi-app/react/view/ReactView.server.tsx +++ b/packages/platform-web/src/shuvi-app/react/view/ReactView.server.tsx @@ -27,16 +27,24 @@ export class ReactServerView implements IReactServerView { } if (redirected) { - const { location, status } = state as { - location: string; - status: number; - }; - return redirect( - isThirdSite(location) - ? location - : router.resolve(location, pathname).href, - status - ); + // handel loader redirect + if ( + state && + typeof (state as { location: string }).location === 'string' + ) { + const { location, status } = state as { + location: string; + status: number; + }; + return redirect( + isThirdSite(location) + ? location + : router.resolve(location, pathname).href, + status + ); + } + // handle router internal redirect + return redirect(pathname); } const loadableModules: string[] = []; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 39adaaed3..3ff7e4e53 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -728,6 +728,12 @@ importers: test/fixtures/css: specifiers: {} + test/fixtures/custom-routes: + specifiers: + shuvi: workspace:* + dependencies: + shuvi: link:../../../packages/shuvi + test/fixtures/dll: specifiers: '@types/node': 18.0.6 @@ -6747,8 +6753,8 @@ packages: engines: {node: '>=10'} hasBin: true dependencies: - is-text-path: 1.0.1 JSONStream: 1.3.5 + is-text-path: 1.0.1 lodash: 4.17.21 meow: 8.1.2 split2: 3.2.2 diff --git a/test/e2e/custom-routes.test.ts b/test/e2e/custom-routes.test.ts new file mode 100644 index 000000000..0016e3857 --- /dev/null +++ b/test/e2e/custom-routes.test.ts @@ -0,0 +1,27 @@ +import { AppCtx, Page, devFixture } from '../utils'; + +jest.setTimeout(5 * 60 * 1000); + +describe('custom routes', () => { + let ctx: AppCtx; + let page: Page; + + beforeAll(async () => { + ctx = await devFixture('custom-routes'); + }); + afterAll(async () => { + await ctx.close(); + }); + afterEach(async () => { + await page.close(); + }); + it('should support redirect', async () => { + page = await ctx.browser.page(ctx.url('/redirect0')); + expect(await page.$text('#index')).toBe('Index Page'); + }); + + test('should support nest redirect', async () => { + page = await ctx.browser.page(ctx.url('/redirect1')); + expect(await page.$text('#about')).toBe('About Page'); + }); +}); diff --git a/test/fixtures/custom-routes/package.json b/test/fixtures/custom-routes/package.json new file mode 100644 index 000000000..ed6a46e5c --- /dev/null +++ b/test/fixtures/custom-routes/package.json @@ -0,0 +1,6 @@ +{ + "name": "custom-routes", + "dependencies": { + "shuvi": "workspace:*" + } +} diff --git a/test/fixtures/custom-routes/shuvi.config.js b/test/fixtures/custom-routes/shuvi.config.js new file mode 100644 index 000000000..f7d7af7c8 --- /dev/null +++ b/test/fixtures/custom-routes/shuvi.config.js @@ -0,0 +1,16 @@ +export default { + ssr: true, + routes: [ + { + path: '/', + component: 'layout', + children: [ + { path: '/redirect0', redirect: '/' }, + { path: '', component: 'page' }, + { path: '/redirect1', redirect: '/redirect2' }, + { path: '/redirect2', redirect: '/about' }, + { path: '/about', component: 'about/page' } + ] + } + ] +}; diff --git a/test/fixtures/custom-routes/src/routes/about/page.js b/test/fixtures/custom-routes/src/routes/about/page.js new file mode 100644 index 000000000..d02914eaa --- /dev/null +++ b/test/fixtures/custom-routes/src/routes/about/page.js @@ -0,0 +1 @@ +export default () =>
About Page
; diff --git a/test/fixtures/custom-routes/src/routes/layout.js b/test/fixtures/custom-routes/src/routes/layout.js new file mode 100644 index 000000000..21b2bb13b --- /dev/null +++ b/test/fixtures/custom-routes/src/routes/layout.js @@ -0,0 +1,8 @@ +import { RouterView } from '@shuvi/runtime'; +import React from 'react'; + +const MyApp = () => { + return ; +}; + +export default MyApp; diff --git a/test/fixtures/custom-routes/src/routes/page.js b/test/fixtures/custom-routes/src/routes/page.js new file mode 100644 index 000000000..d476b25bb --- /dev/null +++ b/test/fixtures/custom-routes/src/routes/page.js @@ -0,0 +1,3 @@ +export default function Entry() { + return
Index Page
; +}