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

chore: remove server data parse in convention routes #6651

Merged
merged 4 commits into from
Dec 24, 2024
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
7 changes: 7 additions & 0 deletions .changeset/young-trains-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@modern-js/plugin-router-v5': patch
'@modern-js/runtime': patch
---

chore: remove server data parse when use convention routes
chore: 移除约定式路由时解析 server data 的逻辑
22 changes: 2 additions & 20 deletions packages/runtime/plugin-router-v5/src/runtime/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@ import {
import { modifyRoutesHook } from './hooks';
import { getLocation, renderRoutes, urlJoin } from './utils';

declare global {
interface Window {
_SERVER_DATA?: {
router: {
baseUrl: string;
params: Record<string, string>;
};
};
}
}

export type SingleRouteConfig = RouteProps & {
redirect?: string;
routes?: SingleRouteConfig[];
Expand Down Expand Up @@ -115,19 +104,12 @@ export const routerPlugin = (userConfig: RouterConfig = {}): Plugin => {

const select = (pathname: string) =>
serverBase.find(baseUrl => pathname.search(baseUrl) === 0) || '/';
if (isBrow) {
window._SERVER_DATA = parsedJSONFromElement(
'__MODERN_SERVER_DATA__',
);
}

const getRouteApp = () => {
if (isBrow) {
return (props: any) => {
const runtimeContext = useContext(RuntimeReactContext);
const baseUrl = (
window._SERVER_DATA?.router.baseUrl ||
select(location.pathname)
).replace(/^\/*/, '/');
const baseUrl = select(location.pathname).replace(/^\/*/, '/');
const basename =
baseUrl === '/'
? urlJoin(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export const routerPlugin = (
},
setup: api => {
let routes: RouteObject[] = [];
window._SERVER_DATA = parsedJSONFromElement('__MODERN_SERVER_DATA__');
return {
beforeRender(context) {
// for garfish plugin to get basename,
Expand Down Expand Up @@ -98,9 +97,7 @@ export const routerPlugin = (
* _internalRouterBaseName: garfish plugin params, priority
* basename: modern config file config
*/
const baseUrl = (
window._SERVER_DATA?.router.baseUrl || select(location.pathname)
).replace(/^\/*/, '/');
const baseUrl = select(location.pathname).replace(/^\/*/, '/');
const _basename =
baseUrl === '/'
? urlJoin(
Expand Down
11 changes: 0 additions & 11 deletions packages/runtime/plugin-runtime/src/router/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@ import type {
} from '@modern-js/runtime-utils/router';
import type { NestedRoute, PageRoute } from '@modern-js/types';

declare global {
interface Window {
_SERVER_DATA?: {
router: {
baseUrl: string;
params: Record<string, string>;
};
};
}
}

export type SingleRouteConfig = RouteProps & {
redirect?: string;
routes?: SingleRouteConfig[];
Expand Down
27 changes: 3 additions & 24 deletions packages/server/core/src/plugins/render/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function matchRoute(

const result = matched[0][0];

// here we can parse the dynamic server routes params
return result || [];
}

Expand Down Expand Up @@ -237,14 +238,6 @@ async function renderHandler(
mode: 'ssr' | 'csr',
onError: (e: unknown) => Promise<void>,
) {
// inject server.baseUrl message
const serverData = {
router: {
baseUrl: options.routeInfo.urlPath,
params: options.params,
},
};

let response: Response | null = null;

const { serverManifest } = options;
Expand Down Expand Up @@ -301,12 +294,10 @@ async function renderHandler(
response = csrRender(options.html);
}

const newRes = transformResponse(response, injectServerData(serverData));

const { routeInfo } = options;
applyExtendHeaders(newRes, routeInfo);
applyExtendHeaders(response, routeInfo);

return newRes;
return response;

function applyExtendHeaders(r: Response, route: ServerRoute) {
Object.entries(route.responseHeaders || {}).forEach(([k, v]) => {
Expand Down Expand Up @@ -357,15 +348,3 @@ function csrRender(html: string): Response {
}),
});
}

function injectServerData(serverData: Record<string, any>) {
const { head } = REPLACE_REG.before;
const searchValue = new RegExp(head);

const replcaeCb = (beforeHead: string) =>
`${beforeHead}<script type="application/json" id="__MODERN_SERVER_DATA__">${JSON.stringify(
serverData,
)}</script>`;

return (template: string) => template.replace(searchValue, replcaeCb);
}
Loading