question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@vueuse/head not working properly

See original GitHub issue

Hello Thanks for this perfect library. It is amazing. I am working on this vue-router example and need to use vueuse/head package to manage titles dynamically but not made work it. Are there any configs etc. about this?

Here are my app.js

import { createSSRApp } from "vue";
import { createRouter } from "./router";
import { createStore } from "../store/store";
import { createHead } from "@vueuse/head";

export { createApp };

function createApp({ Page }) {
  const app = createSSRApp(Page);
  const router = createRouter();
  const store = createStore();
  const head = createHead();
  app.use(store);
  app.use(router);
  app.use(head);
  return { app, router, store, head };
}

all.page.server.js

import { renderToString } from "@vue/server-renderer";
import { html } from "vite-plugin-ssr";
import { createHead, renderHeadToString } from "@vueuse/head";

import { createApp } from "./app";

export { render };
export { addContextProps };
export { setPageProps };

async function render({ contextProps }) {
  const { appHtml } = contextProps;

  const head = createHead();
  const { headTags, htmlAttrs, bodyAttrs } = renderHeadToString(head);

  return html`<!DOCTYPE html>
    <html>
      <head>
        ${html.dangerouslySetHtml(headTags)}
      </head>
      <body>
        <div id="app">${html.dangerouslySetHtml(appHtml)}</div>
      </body>
    </html>`;
}

async function addContextProps({ Page, contextProps }) {
  const { app, store, router } = createApp({ Page });

  await router.push(contextProps.url);
  await router.isReady();
  const appHtml = await renderToString(app);

  const INITIAL_STATE = store.state;

  return {
    INITIAL_STATE,
    appHtml,
  };
}

function setPageProps({ contextProps }) {
  const { INITIAL_STATE } = contextProps;
  return { INITIAL_STATE };
}

About.vue component

<template>
  <p>The count is preserved when switching between "Home" and "About".</p>
</template>

<script setup>
import { useHead } from "@vueuse/head";

useHead({
  title: "My About Page",
});
</script>

I cant see my title in SSR rendered code.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
brilloutcommented, Apr 11, 2021

Had a deeper look, it seems to be a problem on @vueuse/head’s side. I’d open a ticket over there.

2reactions
alsyapukovcommented, Apr 18, 2021

@ozergul Try to use vuex on server and get data from component

// newpage.page.server.js
async function render({ contextProps }) {
  const { appHtml, INITIAL_STATE } = contextProps

  let title = 'Default title'
  if(INITIAL_STATE.moduleStore.title) {
    tilte = INITIAL_STATE.moduleStore.title
  }


  return html`<!DOCTYPE html>
    <html>
      <head>
        <title>${title}</title>
      </head>
      <body>
        <div id="app">${html.dangerouslySetHtml(appHtml)}</div>
      </body>
    </html>`
}

async function addContextProps({ Page, contextProps }) {
  const { app, store, router } = createApp({ Page })

  router.push(contextProps.url)
  await router.isReady()

  app.provide('contextProps', contextProps)

  const appHtml = await renderToString(app)

  const INITIAL_STATE = store.state

  return {
    INITIAL_STATE,
    appHtml
  }
}

function setPageProps({ contextProps }) {
  const { INITIAL_STATE } = contextProps
  return { INITIAL_STATE }
}
// Your component
  inject: ['contextProps'],
  serverPrefetch(e) {
    return this.setTitle({ id: this.contextProps.title });
  },
  methods: {
    ...mapActions("moduleStore", ["setTitle"])
  }

I hope I helped you and understood the problem correctly

Read more comments on GitHub >

github_iconTop Results From Across the Web

VueUse Head v1 release - Harlan Wilton
A few months ago I was decided to fix a quick bug on @vueuse/head. ... This one was pretty tedious, but well worth...
Read more >
Vue-head not working in production
I have a little problem with my site when I put in production. well I installed the vue-head plugging in my site localhost...
Read more >
VueUse Head and Netlify PreRendering for SEO and Social ...
Netlify Prerendering combined with VueUse head empowers Vue.js 3 SPAs to be SEO and social friendly with virtually no extra work.
Read more >
@vueuse/head | Yarn - Package Manager
No unreleased changes. 1.0.22. fix: expose use function for plugins. 1.0.21. chore(deps): bump deps ...
Read more >
Injecting into <head> in Vue.js - Stack Overflow
This isn't addressed in documentation because it's not Vue's job. ... then you can go for dynamic loading of JS script tag.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found