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

2
  • I think what you're looking for is located in the vue ssr docs. It's probably the vue-server-renderer package. Seems to be able to render anything down. Commented Dec 17, 2018 at 2: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 Commented Dec 17, 2018 at 2:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.