Skip to content

Commit

Permalink
fix: custom routes redirect (#527)
Browse files Browse the repository at this point in the history
Co-authored-by: Tom <[email protected]>
  • Loading branch information
wangjinyang and Tom authored May 9, 2023
1 parent fbe388a commit a53f47b
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 11 deletions.
28 changes: 18 additions & 10 deletions packages/platform-web/src/shuvi-app/react/view/ReactView.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand Down
8 changes: 7 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions test/e2e/custom-routes.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
6 changes: 6 additions & 0 deletions test/fixtures/custom-routes/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "custom-routes",
"dependencies": {
"shuvi": "workspace:*"
}
}
16 changes: 16 additions & 0 deletions test/fixtures/custom-routes/shuvi.config.js
Original file line number Diff line number Diff line change
@@ -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' }
]
}
]
};
1 change: 1 addition & 0 deletions test/fixtures/custom-routes/src/routes/about/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => <div id="about">About Page</div>;
8 changes: 8 additions & 0 deletions test/fixtures/custom-routes/src/routes/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { RouterView } from '@shuvi/runtime';
import React from 'react';

const MyApp = () => {
return <RouterView />;
};

export default MyApp;
3 changes: 3 additions & 0 deletions test/fixtures/custom-routes/src/routes/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Entry() {
return <div id="index">Index Page</div>;
}

0 comments on commit a53f47b

Please sign in to comment.