Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): csr 和 dynamicImport 一起使用导致组件 2 次 mount #6057

Merged
merged 3 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/renderer-react/src/renderRoutes/renderRoutes.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function TestInitialProps({ foo }: { foo: string }) {
}

let mountCount = 0;
let renderCount = 0;
function TestInitialPropsWithoutUnmount({ foo }: { foo: string }) {
React.useEffect(() => {
return () => {
Expand All @@ -25,6 +26,14 @@ function TestInitialPropsWithoutUnmount({ foo }: { foo: string }) {
);
}

function TestInitialPropsWithMount({ foo }: { foo: string }) {
React.useEffect(() => {
mountCount++;
}, []);
renderCount++;
return <h1 data-testid="test">{foo}</h1>;
}

const getInitialProps = async () => {
return new Promise((resolve) => {
setTimeout(() => {
Expand All @@ -37,6 +46,7 @@ const getInitialProps = async () => {

TestInitialProps.getInitialProps = getInitialProps;
TestInitialPropsWithoutUnmount.getInitialProps = getInitialProps;
TestInitialPropsWithMount.getInitialProps = getInitialProps;

function TestInitialPropsParent({
foo,
Expand Down Expand Up @@ -128,6 +138,10 @@ const routerConfig = {
path: '/get-initial-props-without-unmount',
component: TestInitialPropsWithoutUnmount as any,
},
{
path: '/get-initial-props-with-mount',
component: TestInitialPropsWithMount as any,
},
{
path: '/get-initial-props-embed',
component: TestInitialPropsParent as any,
Expand Down Expand Up @@ -188,6 +202,8 @@ let routes = renderRoutes(routerConfig);
beforeEach(() => {
window.g_useSSR = true;
window.g_initialProps = null;
mountCount = 0;
renderCount = 0;
});

afterEach(() => {
Expand Down Expand Up @@ -306,6 +322,24 @@ test('/get-initial-props-without-unmount', async () => {
expect((await screen.findByTestId('test')).innerHTML).toEqual('bar');
});

test('/get-initial-props-with-mount', async () => {
const newRoutes = renderRoutes(routerConfig);

expect(mountCount).toEqual(0);
expect(renderCount).toEqual(0);

const { container } = render(
<MemoryRouter initialEntries={['/get-initial-props-with-mount']}>
{newRoutes}
</MemoryRouter>,
);

await waitFor(() => getByText(container, 'bar'));

expect(mountCount).toEqual(1);
expect(renderCount).toEqual(2);
});

test('/get-initial-props-embed', async () => {
const newRoutes = renderRoutes(routerConfig);
const { container } = render(
Expand Down
9 changes: 5 additions & 4 deletions packages/renderer-react/src/renderRoutes/renderRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ function wrapInitialPropsFetch(route: IRoute, opts: IOpts): IComponent {
*/
const handleGetInitialProps = async () => {
// preload when enalbe dynamicImport
let preloadComponent: any = Component;
if (Component.preload) {
const preloadComponent = await Component.preload();
preloadComponent = await Component.preload();
// for test case, really use .default
Component = preloadComponent.default || preloadComponent;
preloadComponent = preloadComponent.default || preloadComponent;
}
const defaultCtx = {
isServer: false,
Expand All @@ -51,15 +52,15 @@ function wrapInitialPropsFetch(route: IRoute, opts: IOpts): IComponent {
...(opts.getInitialPropsCtx || {}),
...restRouteParams,
};
if (Component?.getInitialProps) {
if (preloadComponent?.getInitialProps) {
const ctx = await opts.plugin.applyPlugins({
key: 'ssr.modifyGetInitialPropsCtx',
type: ApplyPluginsType.modify,
initialValue: defaultCtx,
async: true,
});

const initialProps = await Component!.getInitialProps!(
const initialProps = await preloadComponent!.getInitialProps!(
ctx || defaultCtx,
);
setInitialProps(initialProps);
Expand Down