vue.jsvuejs3server-side-renderinginertiajsjson-ld

how set dynamically jsonld in vue 3 ssr


When i try pass it by component is='script' type='application/ld+json' it has error: "TypeError: Cannot convert a Symbol value to a string", but when i pass it by teleport it works but only on client. I use laravel inertiajs vue3 ssr

I tried to use v-html on component <component is='script' key='webSite' v-html='webSiteJSONLD' type='application/ld+json'></component> but then it just insert data like attribute innerHTML in tag script.

in other way will be error: "TypeError: Cannot convert a Symbol value to a string" <component is='script' key='webSite' type='application/ld+json'>{{ webSiteJSONLD }}</component>

text json const webSiteJSONLD=computed(()=>{ return `{ '@context': 'https://schema.org', '@type': 'WebSite', 'url': '${import.meta.env.SSR?import.meta.env.VITE_APP_URL:host.value}' }`; });


Solution

  • I writed fork head's component of inertia js and change function renderTag in component. And it works.But with bugs

    renderTag(node) {
      // check specific symbol from node.type
      switch (node.type.toString()) {
        case 'Symbol(Text)':
        case 'Symbol(v-txt)':
          return node.children
        case 'Symbol()':
        case 'Symbol(Comment)':
          return ''
        // if nothing expected, return a html string
        default:
          return this.renderTagStart(node) + 
          (node.children ? this.renderTagChildren(node) : '') +
          (!this.isUnaryTag(node) ? `</${node.type}>` : '')
      }
    }