Skip to content

Commit

Permalink
Merge pull request #913 from preactjs/fix/avoid-replacing-regex-group…
Browse files Browse the repository at this point in the history
…-token-with-matched-results

(fix) Avoid replacing regex group token when present in html
  • Loading branch information
cristianbote authored Apr 4, 2022
2 parents 11663db + 21ec4f4 commit caf7ff8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-pandas-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'wmr': patch
---

When prerendering avoid replacing regex group token \$1 when is present in html contents
4 changes: 2 additions & 2 deletions packages/wmr/src/lib/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ async function workerCode({ cwd, out, publicPath, customRoutes }) {
html = html.replace(/(<html(\s[^>]*?)?>)/, `<html lang="${enc(head.lang)}">`);
}

html = html.replace(/(<\/head>)/, headHtml + '$1');
html = html.replace(/(<\/head>)/, (_, groupMatched) => headHtml + groupMatched);

// Inject pre-rendered HTML into the start of <body>:
html = html.replace(/(<body(\s[^>]*?)?>)/, '$1' + body);
html = html.replace(/(<body(\s[^>]*?)?>)/, (_, groupMatched) => groupMatched + body);

// Write the generated HTML to disk:
await fs.mkdir(path.dirname(outFile), { recursive: true }).catch(Object);
Expand Down
2 changes: 1 addition & 1 deletion packages/wmr/test/fixtures/prerender-data/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export function prerender() {
return { html: '<h1>it works</h1>', links: ['/'], data: { hello: 'world' } };
return { html: '<h1>it works$1</h1>', links: ['/'], data: { hello: 'world' } };
}

0 comments on commit caf7ff8

Please sign in to comment.