Skip to content

headTags are empty when using pipeToWebWritable #27

Closed as not planned
Closed as not planned

Description

@daliborgogic
import { createHead, useHead } from '@unhead/vue'
import { createSSRApp, ref } from 'vue'
import { pipeToWebWritable } from '@vue/server-renderer'
import { renderSSRHead } from '@unhead/ssr'
import { describe, it, expect } from 'vitest'

const template = String.raw`<!doctype html><html lang="en"><!--head--><div id="app"><!--app--></div></html>`

describe('vue ssr', () => {
  it('pipeToWebWritable', async () => {
    const head = createHead()
    const app = createSSRApp({
      async setup() {
        const title = ref('initial title')
        useHead({
          title,
        })
        await new Promise(resolve => setTimeout(resolve, 200))
        title.value = 'new title'
        return () => '<div>hi</div>'
      },
    })
    app.use(head)

    const [prepend, append] = template.split('<!--app-->')
    const { headTags } = await renderSSRHead(head)
    const encoder = new TextEncoder()

    const { writable } = new TransformStream({
      start(controller) {
        // prepend.replace('<!--head-->', headTags)
        controller.enqueue(encoder.encode(prepend))
      },
      flush(controller) {
        controller.enqueue(encoder.encode(append))
      }
    })

    pipeToWebWritable(app, {}, writable)

    expect(headTags).eq('<title>new title</title>')
  })
})
 ❯ test/stream.test.js:29:22
     27|     await pipeToWebWritable(app, {}, writable)
     28| 
     29|     expect(headTags).eq('<title>new title</title>')
       |                      ^
     30|   })
     31| })
-    Expected   "<title>new title</title>"
+    Received   ""

Activity

  1. changed the title [-]Headtags are empty when using `pipeToWebWritable`[/-] [+]`headTags ` are empty when using `pipeToWebWritable`[/+] on Nov 28, 2022
  2. harlan-zw commented on Dec 1, 2022

    @harlan-zw
    Collaborator

    Hey @daliborgogic

    Thanks for the issue.

    I'm not entirely sure how to solve this, as head tags are written as the Vue app is rendered. Do you have any ideas?

  3. changed the title [-]`headTags ` are empty when using `pipeToWebWritable`[/-] [+]`headTags` are empty when using `pipeToWebWritable`[/+] on Dec 1, 2022
  4. daliborgogic commented on Dec 14, 2022

    @daliborgogic
    Author

    @harlan-zw For now no idea. Will check what we can and what we can with hooks. You can close this issue. If we found a solution we will make a PR.

  5. bukandicki commented on Dec 27, 2022

    @bukandicki

    have you tried this code @daliborgogic @harlan-zw ?

    const appHtml = await renderToString(app);
    const { headTags } = await renderSSRHead(head);

    I had the same case, then I was able to solve it by changing the order, first renderToString then renderSSRRHead

  6. harlan-zw commented on Dec 27, 2022

    @harlan-zw
    Collaborator

    Isn't that just regular SSR? My understanding of the Vue rendering stream is that it's sending chunks as they are rendered so the head data would need to be appended as it's rendered, which it isn't for performance reasons.

  7. daliborgogic commented on Apr 10, 2023

    @daliborgogic
    Author

    @harlan-zw yes, it's regular SSR. You are right. Workaround until there is a better way

    - const { headTags } = await renderSSRHead(head)
    + const decoder = new TextDecoder()
    + let c
    + let once = false
      const { readable, writable } = new TransformStream({
    -   start() {/**/},
    +   async transform(chunk, controller) {     
    +     if (!once) {
    +      const decodedChunk = decoder.decode(chunk)
    +      const { headTags } = await renderSSRHead(head)
    +      const replacedHead = prepend.replace(`<!--head-->`, headTags)
    +      const encodedChunk = encoder.encode(replacedHead + decodedChunk)
    +      c = encodedChunk
    +      once = true 
    +     } else {
    +       c = chunk
    +     }
    +     controller.enqueue(c)
    +   },
        flush(controller) {
          controller.enqueue(encoder.encode(append))
        }
      })
  8. harlan-zw commented on Sep 7, 2024

    @harlan-zw
    Collaborator

    Going to track here #396 which is part of the v2 roadmap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions