Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Handle titleTemplate like vue-meta #22

Closed
mathieutu opened this issue Mar 4, 2021 · 3 comments · Fixed by #49
Closed

[Feature Request] Handle titleTemplate like vue-meta #22

mathieutu opened this issue Mar 4, 2021 · 3 comments · Fixed by #49

Comments

@mathieutu
Copy link

Hi,
It could be really cool to have a titleTemplate like vue-meta, to be able to set a part of title for each page, and a global part.

For now, I've just made my own use head that just do that:

import { useHead as originalUseHead, HeadObject } from '@vueuse/head'

export const useHead = (head: HeadObject) => originalUseHead({
  ...head,
  title: `${head.title} - MyProject`
})

But it can't be customized in the app.

Thanks for your amazing work,
Mathieu.

@glen-84
Copy link

glen-84 commented Sep 21, 2021

@egoist Would you accept a PR for this?

Maybe createHead could take an options object, with a titleFormatter: (title: string) => string option?

@theoephraim
Copy link

theoephraim commented Dec 15, 2021

would definitely make sense to support natively... but until then, if anyone else needs a workaround, here is what I do. It's pretty hacky but it works.

utils/use-page-title.ts

import { useHead } from '@vueuse/head';
import { unref, computed, ComputedRef } from 'vue';

let siteTitle = 'hello';
let separator = '|';

export const usePageTitle = (pageTitle: string | ComputedRef<string>) => useHead(computed(() => ({
  title: `${unref(pageTitle)} ${separator} ${siteTitle}`,
})));

// this is expected to only be called once - should maybe add a check / error
export const usePageTitleTemplate = (newSiteTitle: string, newSeparator?: string) => {
  siteTitle = newSiteTitle;
  if (newSeparator) separator = newSeparator;
  return useHead({ title: siteTitle });
};

then once (usually in App.vue) call usePageTitleTemplate('My cool site');

and in each page component call usePageTitle('About us')

@gilesbutler
Copy link

Would love to see this too!

@antfu antfu closed this as completed in #49 Aug 1, 2022
antfu added a commit that referenced this issue Aug 1, 2022
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants