I'm developing a Vue server-side templating engine, and part of the package includes a head management system.
I'm developing a Server-Side Renderer for Vue and for the SSR's head management to work, it needs a way to render VNode
s to text.
My Question: how can I render VNode(s) into a string? For example:
this.$ssrContext.head = renderVNodesToString(this.$slots.head)
Example use case:
master.vue
<template>
<div id="app">
<slot name="content"></slot>
</div>
</template>
<script>
export default {
created: function(){
if(this.$isServer){
var VNodesToRender = this.$slots.head
this.$ssrContext.head = 'rendered VNode here'
}
}
}
</script>
home.vue
<template>
<master>
<template slot="content">
Hello World
</template>
<template slot="head">
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<title>Hello</title>
</template>
</master>
</template>
<script>
import master from "layouts/master.vue"
export default {
components: {
master
}
}
</script>
My goal is getting home.vue
's head
slot rendered into a string and injecting it into the this.$ssrContext
so it can be read and injected on the server-side
in master.vue
, I can access this.$slots.head
with no issue, and it contains the correct VNode
s
Where the string is used
in {{{ head }}}
const renderer = createBundleRenderer(bundle.server, {
runInNewContext: false,
inject: false,
template: `<!DOCTYPE html>
<html>
<head>
{{{ head }}}
{{{ renderResourceHints() }}}
{{{ renderStyles() }}}
</head>
<body>
<!--vue-ssr-outlet-->
<script>${ bundle.client }</script>
</body>
</html>`
})
Note: This question is not like:
as it requires a string output, which is passed to the SSR