Subscribe on changes!

RenderToString does not support ref type teleport

avatar
Dec 3rd 2020

Version

3.0.4

Reproduction link

https://codesandbox.io/s/naughty-wind-fufkn

Steps to reproduce

ref type teleport

https://github.com/vuejs/vue-next/blob/master/packages/runtime-core/__tests__/components/Teleport.spec.ts#L38-L54

teleport SSR test

https://github.com/vuejs/vue-next/blob/master/packages/server-renderer/__tests__/ssrTeleport.spec.ts#L59-L73

When ref type report is used, it is not reflected in ctx

const ctx: SSRContext = {}
const html = await renderToString(
  createApp({
    setup() {
      const svg = ref()
      const circle = ref()

      return {
        svg,
        circle
      }
    },
    template: `
      <svg ref="svg"></svg>
      <teleport :to="svg" v-if="svg">
        <circle ref="circle"></circle>
      </teleport>
    `
  }),
  ctx
)
console.log(html) // <!--[--><svg></svg><!----><!--]-->
console.log(ctx) // return {}

What is expected?

The same result as with id

What is actually happening?

No teleport object in context

avatar
Dec 3rd 2020

IMO:

- With ref target, teleports can find the target in current component scope, so it's rendered to string directly.

- With id target, teleports cannot determine where the target is during SSR, so it's extracted to the context.

avatar
Dec 3rd 2020
  • With ref target, teleports can find the target in current component scope, so it's rendered to string directly.

@meteorlxy If so, I would like to get results that include circle in the renderToString.

avatar
Dec 3rd 2020

My previous comment was wrong.

The ref is still undefined during SSR, it's only available after mounted.

So it should not be extracted to ctx.

avatar
Dec 3rd 2020

I understand your kind answer, but I still have some questions.

  • Based on the result code of renderToString using id or ref, Does not the server-side rendering code expose the teleport template?
  • If made source using ref and v-if (Like an example link), where would the reference be kept and processed after mounted?
avatar
Dec 7th 2020

The to has to be present at the moment the component renders, so as explained, using an unitialized ref won't work. In general, it's better to have the teleport target outside of the component where teleport is used: https://v3.vuejs.org/api/built-in-components.html#teleport. Because of this you cannot use the ref="svgRef" attribute for teleports. You can still use a dynamic selector