renderToString doesn't render Component is script (json ld)
Vue version
3.2.45
Link to minimal reproduction
https://github.com/yooouuri/render-to-string-script-tag
import { createSSRApp } from 'vue'
import { renderToString } from 'vue/server-renderer'
const app = createSSRApp({
template: `<Component :is="'script'" data-something="value" v-html="JSON.stringify({ foo: 'bar' })" />`,
})
renderToString(app).then(html => console.log(html))
Steps to reproduce
Clone the minimal reproduction repo
git clone https://github.com/yooouuri/render-to-string-script-tag
yarn
node index.js
What is expected?
After running node index.js
<script data-something="value">{"foo":"bar"}</script>
What is actually happening?
But It logs
<script data-something="value"></script>
Logs what I would expect
System Info
System:
OS: macOS 12.4
CPU: (8) arm64 Apple M1 Pro
Memory: 110.16 MB / 16.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 16.18.0 - /opt/homebrew/opt/node@16/bin/node
Yarn: 1.22.19 - /opt/homebrew/bin/yarn
npm: 8.19.2 - /opt/homebrew/opt/node@16/bin/npm
Browsers:
Firefox: 107.0
Safari: 15.5
npmPackages:
vue: ^3.2.45 => 3.2.45
Any additional comments?
No response
I don't think this syntax is allowed in JavaScript. It is equivalent to the following code:
<script>"foo": "bar"</script>
@zhangzhonghe I forgot something to mention...
Im trying to render a json-ld script.
<Component :is="'script'" type="application/ld+json" v-html="JSON.stringify({ foo: 'bar' })" />
Gives me:
So in a SFC it works as expected.
And when I use the renderToString
method (used when in SSR mode)
import { createSSRApp } from 'vue'
import { renderToString } from 'vue/server-renderer'
const app = createSSRApp({
template: `<Component :is="'script'" type="application/ld+json" v-html="JSON.stringify({ foo: 'bar' })" />`,
})
renderToString(app).then(html => console.log(html))
It logs
So there is a difference between client side rendering and SSR.
we ignore innerHTML
tag in SSR.
duplicate of #6435
The root cause is that directives with children overwrite (e.g. v-html & v-text) are not working with Component
yet in SSR.
Close this one.
a workaround:
<script>
import { h } from 'vue'
export default {
setup(props) {
return () =>
h('script', {
type: "application/ld+json",
innerHTML: JSON.stringify({ foo: 'bar' }),
});
},
}
</script>