Skip to content

Commit

Permalink
fix: handle the quick navigation issue in ssr (#5129)
Browse files Browse the repository at this point in the history
  • Loading branch information
yimingjfe authored Dec 20, 2023
1 parent 4b4d08a commit 5e9a6a9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/wise-cooks-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/runtime': patch
---

fix: handle the quick navigation issue in ssr
fix: 修复 ssr 场景下 quick navigation 的问题
13 changes: 11 additions & 2 deletions packages/runtime/plugin-runtime/src/ssr/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { loadableReady } from '@loadable/component';
import hoistNonReactStatics from 'hoist-non-react-statics';
import { parsedJSONFromElement } from '@modern-js/runtime-utils/parsed';
import type { Plugin } from '../core';
import type { Plugin, RuntimeContext } from '../core';
import {
RenderLevel,
SSRServerContext,
Expand Down Expand Up @@ -33,13 +33,22 @@ export const ssr = (config: SSRPluginConfig): Plugin => ({

return {
client: async ({ App, context, ModernRender, ModernHydrate }) => {
const hydrateContext: { _hydration?: boolean } = {
const hydrateContext: RuntimeContext & { __hydration?: boolean } = {
...context,
get routes() {
return context.routes;
},
_hydration: true,
};
const { ssrContext } = hydrateContext;
const { pathname: initialPathname } = ssrContext!.request;
const currentPathname = window.location.pathname;
if (initialPathname !== currentPathname) {
const errorMsg = `The initial URL ${initialPathname} and the URL ${currentPathname} to be hydrated do not match, reload.`;
console.error(errorMsg);
window.location.reload();
}

const callback = () => {
// won't cause component re-render because context's reference identity doesn't change
delete hydrateContext._hydration;
Expand Down

0 comments on commit 5e9a6a9

Please sign in to comment.