frandiox / vite-ssr

Use Vite for server side rendering in Node
MIT License
824 stars 91 forks source link

buildHtmlDocument will replace $1 in __INITIAL_STATE__ #123

Closed MOSapeizer closed 2 years ago

MOSapeizer commented 2 years ago

Code Ref

https://github.com/frandiox/vite-ssr/blob/43baabedee697fcf4c83e0f8ee6d0d42dd9c96a1/src/utils/html.ts#L40-L51

demo

const template = '<div id="app"></div>'
const state = { productName: 'buy me, only $1' }

template.replace(/<div id="app"([\s\w\-"'=[\]]*)><\/div>/, `<div>${state.productName || "'{}'"}<\/div>`)

// expect:
// '<div>buy me, only $1</div>'

// result:
// '<div>buy me, only </div>'
thegouge commented 2 years ago

I discovered this issue around the same time, but I also pinpointed that around version 0.13.0 is where this bug appears (since rolling back to 0.12.x fixes it)