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/unsafe replace #5098

Merged
merged 5 commits into from
Dec 19, 2023
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
6 changes: 6 additions & 0 deletions .changeset/honest-papayas-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/runtime': patch
---

fix: unsafeReplace fn fix
fix: unsafeReplace 函数修复, 处理找不到 searchValue 的边界 case
10 changes: 5 additions & 5 deletions packages/runtime/plugin-runtime/src/ssr/serverRender/helmet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// 用于 react-helmet 正则替换
import { EOL } from 'os';
import { HelmetData } from 'react-helmet';
import { unsafeReplace } from './utils';
import { safeReplace } from './utils';

const RE_HTML_ATTR = /<html[^>]*>/;
const RE_BODY_ATTR = /<body[^>]*>/;
Expand All @@ -15,12 +15,12 @@ export default function helmet(content: string, helmetData: HelmetData) {
let result = content;
const bodyAttributes = helmetData.bodyAttributes.toString();
if (bodyAttributes) {
result = unsafeReplace(result, RE_BODY_ATTR, `<body ${bodyAttributes}>`);
result = safeReplace(result, RE_BODY_ATTR, `<body ${bodyAttributes}>`);
}

const htmlAttributes = helmetData.htmlAttributes.toString();
if (htmlAttributes) {
result = unsafeReplace(result, RE_HTML_ATTR, `<html ${htmlAttributes}>`);
result = safeReplace(result, RE_HTML_ATTR, `<html ${htmlAttributes}>`);
}

const base = helmetData.base.toString();
Expand All @@ -36,7 +36,7 @@ export default function helmet(content: string, helmetData: HelmetData) {
const shouldReplaceTitle =
existTitleTag && TEST_TITLE_CONTENT.test(title.trim());
if (shouldReplaceTitle) {
result = result.replace(RE_TITLE, title);
result = safeReplace(result, RE_TITLE, title);
}

const helmetStr = [
Expand All @@ -51,5 +51,5 @@ export default function helmet(content: string, helmetData: HelmetData) {
return pre + (cur.length > 0 ? ` ${cur}${EOL}` : '');
}, '');

return unsafeReplace(result, RE_LAST_IN_HEAD, `${helmetStr}</head>`);
return safeReplace(result, RE_LAST_IN_HEAD, `${helmetStr}</head>`);
}
4 changes: 2 additions & 2 deletions packages/runtime/plugin-runtime/src/ssr/serverRender/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

import { isReact18 } from '../utils';
import { ServerRenderOptions } from './types';
import { CHUNK_JS_PLACEHOLDER } from './constants';
import { CHUNK_CSS_PLACEHOLDER } from './constants';

export default async function serverRender(options: ServerRenderOptions) {
if (options.context.ssrContext?.template) {
options.context.ssrContext.template =
options.context.ssrContext?.template.replace(
'</head>',
`${CHUNK_JS_PLACEHOLDER}</head>`,
`${CHUNK_CSS_PLACEHOLDER}</head>`,
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { serializeJson } from '@modern-js/runtime-utils/node';
import { RenderLevel, RuntimeContext } from '../types';
import { attributesToString, unsafeReplace } from '../utils';
import { attributesToString, safeReplace } from '../utils';
import { SSR_DATA_PLACEHOLDER } from '../constants';
import { BuildTemplateCb, buildTemplate } from './buildTemplate.share';

Expand All @@ -18,7 +18,7 @@ export function buildShellAfterTemplate(
function injectSSRDataScript(template: string) {
const ssrDataScript = buildSSRDataScript();

return unsafeReplace(template, SSR_DATA_PLACEHOLDER, ssrDataScript);
return safeReplace(template, SSR_DATA_PLACEHOLDER, ssrDataScript);

function buildSSRDataScript() {
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { matchRoutes } from '@modern-js/runtime-utils/router';
import helmetReplace from '../helmet';
import { RuntimeContext } from '../types';
import { CHUNK_CSS_PLACEHOLDER } from '../constants';
import { unsafeReplace } from '../utils';
import { safeReplace } from '../utils';
import {
HEAD_REG_EXP,
BuildTemplateCb,
Expand All @@ -31,7 +31,7 @@ function getHeadTemplate(beforeEntryTemplate: string, context: RuntimeContext) {
return buildTemplate(headTemplate, callbacks);

function injectCss(headTemplate: string) {
return unsafeReplace(headTemplate, CHUNK_CSS_PLACEHOLDER, getCssChunks());
return safeReplace(headTemplate, CHUNK_CSS_PLACEHOLDER, getCssChunks());

function getCssChunks() {
const { routeManifest, routerContext, routes } = context;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { unsafeReplace } from '../utils';
import { safeReplace } from '../utils';
import {
HTML_PLACEHOLDER,
SSR_DATA_PLACEHOLDER,
Expand All @@ -13,20 +13,19 @@ export function buildHtml(template: string, callbacks: BuildHtmlCb[]) {
}

export function createReplaceHtml(html: string): BuildHtmlCb {
return (template: string) => unsafeReplace(template, HTML_PLACEHOLDER, html);
return (template: string) => safeReplace(template, HTML_PLACEHOLDER, html);
}

export function createReplaceSSRDataScript(data: string): BuildHtmlCb {
return (template: string) =>
unsafeReplace(template, SSR_DATA_PLACEHOLDER, data);
safeReplace(template, SSR_DATA_PLACEHOLDER, data);
}

export function createReplaceChunkJs(js: string): BuildHtmlCb {
return (template: string) =>
unsafeReplace(template, CHUNK_JS_PLACEHOLDER, js);
return (template: string) => safeReplace(template, CHUNK_JS_PLACEHOLDER, js);
}

export function createReplaceChunkCss(css: string): BuildHtmlCb {
return (template: string) =>
unsafeReplace(template, CHUNK_CSS_PLACEHOLDER, css);
safeReplace(template, CHUNK_CSS_PLACEHOLDER, css);
}
8 changes: 3 additions & 5 deletions packages/runtime/plugin-runtime/src/ssr/serverRender/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ export function attributesToString(attributes: Record<string, any>) {
}

/**
* It is unsafe unsafeReplace, only support serachValue exsit one time.
* @param source
* @param searchValue
* @param replaceValue
* @returns
*/
export function unsafeReplace(
export function safeReplace(
source: string,
searchValue: RegExp | string,
searchValue: string | RegExp,
replaceValue: string,
) {
const [s1, s2] = source.split(searchValue);
return s1 + replaceValue + s2;
return source.replace(searchValue, () => replaceValue);
}