7

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 VNodes 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 VNodes

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:

  1. VueJS Render VNode
  2. Understanding Vnodes
  3. Can you render VNodes in a Vue template?

as it requires a string output, which is passed to the SSR

Dominus Vilicus
  • 903
  • 2
  • 8
  • 12
  • I think what you're looking for is [located in the vue ssr docs](https://ssr.vuejs.org/guide/#rendering-a-vue-instance). It's probably the `vue-server-renderer` package. Seems to be able to render anything down. – Ohgodwhy Dec 17 '18 at 02:48
  • @Ohgodwhy, unfortunately, that can't help. I'm already using it to render the .vue files. I'm trying to pass a variable from the .vue file to the $ssrContext – Dominus Vilicus Dec 17 '18 at 02:55

0 Answers0