preactjs/wmr

Prerender: regexp capturing group replaces any instance of $1 (etc.)

danielweck opened this issue · 2 comments

Ouch! When the string $1 occurs anywhere in headHtml or body ... every instance of $1 gets replaced by the captured group:

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

html = html.replace(/(<body(\s[^>]*?)?>)/, '$1' + body);

I realised this when I injected hydration data into the HTML head, luckily so far none of my pre-rendered webpages' body markup contained $1 :)

My current workaround: str.replace(/\$1/g, '&#36;') works for result.html but fails for result.data as the escaped sequence doesn't actually get decoded by the browser.

ah, good catch!