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

feat(runtime): add new configuration for reload on SSR URL mismatch #6731

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions .changeset/four-years-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/runtime': patch
---

feat: add new configuration for reload on SSR URL mismatch
feat: 添加新的配置,支持在 SSR URL 不匹配时重新加载页面
17 changes: 0 additions & 17 deletions packages/runtime/plugin-runtime/src/core/browser/hydrate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,6 @@ export function hydrateRoot(
_hydration: true,
};

const { ssrContext } = hydrateContext;

const currentPathname = normalizePathname(window.location.pathname);
const initialPathname =
ssrContext?.request?.pathname &&
normalizePathname(ssrContext.request.pathname);

if (
initialPathname &&
initialPathname !== currentPathname &&
context.router
) {
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
18 changes: 17 additions & 1 deletion packages/runtime/plugin-runtime/src/router/runtime/plugin.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { merge } from '@modern-js/runtime-utils/merge';
import { parsedJSONFromElement } from '@modern-js/runtime-utils/parsed';
import type { RouterSubscriber } from '@modern-js/runtime-utils/remix-router';
import {
type RouteObject,
Expand All @@ -11,6 +10,7 @@ import {
useLocation,
useMatches,
} from '@modern-js/runtime-utils/router';
import { normalizePathname } from '@modern-js/runtime-utils/url';
import type React from 'react';
import { useContext, useMemo } from 'react';
import { type Plugin, RuntimeReactContext } from '../../core';
Expand Down Expand Up @@ -49,6 +49,22 @@ export const routerPlugin = (
let routes: RouteObject[] = [];
return {
beforeRender(context) {
// In some scenarios, the initial pathname and the current pathname do not match.
// We add a configuration to support the page to reload.
if (window._SSR_DATA && userConfig.unstable_reloadOnURLMismatch) {
const { ssrContext } = context;
const currentPathname = normalizePathname(window.location.pathname);
const initialPathname =
ssrContext?.request?.pathname &&
normalizePathname(ssrContext.request.pathname);

if (initialPathname && 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();
}
}

// for garfish plugin to get basename,
// why not let garfish plugin just import @modern-js/runtime/router
// so the `router` has no type declare in RuntimeContext
Expand Down
5 changes: 5 additions & 0 deletions packages/runtime/plugin-runtime/src/router/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export type RouterConfig = {
future?: Partial<{
v7_startTransition: boolean;
}>;
/**
* An unstable feature, which will reload the page when the current browser URL and the SSR Context URL do not match.
*/
unstable_reloadOnURLMismatch?: boolean;
};
};

export type Routes = RouterConfig['routesConfig']['routes'];
Expand Down
Loading