Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.

docs: add documentation for titleTemplate#5057

Closed
entity wants to merge 6 commits intonuxt:mainfrom
entity:main
Closed

docs: add documentation for titleTemplate#5057
entity wants to merge 6 commits intonuxt:mainfrom
entity:main

Conversation

@entity
Copy link
Copy Markdown
Contributor

@entity entity commented May 19, 2022

🔗 Linked issue

nuxt/nuxt#13981

❓ Type of change

  • 📖 Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

This change adds more clarity to the titleTemplate option, as currently in Nuxt 3 this doesn't work as intended. This resolves nuxt/nuxt#13981.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@netlify
Copy link
Copy Markdown

netlify Bot commented May 19, 2022

Deploy Preview for nuxt3-docs ready!

Name Link
🔨 Latest commit b746828
🔍 Latest deploy log https://app.netlify.com/sites/nuxt3-docs/deploys/62889508251d530008b5a346
😎 Deploy Preview https://deploy-preview-5057--nuxt3-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@entity entity changed the title DOCS: Added more detail to titleTemplate. docs: Added more detail to titleTemplate. May 19, 2022
Comment thread docs/content/2.guide/2.features/4.head-management.md Outdated
@entity
Copy link
Copy Markdown
Contributor Author

entity commented May 19, 2022

Co-authored-by: Alexander Lichter <manniL@gmx.net>
Comment thread docs/content/2.guide/2.features/4.head-management.md Outdated
Comment thread docs/content/2.guide/2.features/4.head-management.md Outdated
entity and others added 2 commits May 19, 2022 12:28
Co-authored-by: Damian <48835293+DamianGlowala@users.noreply.github.com>
Co-authored-by: Damian <48835293+DamianGlowala@users.noreply.github.com>
Comment thread docs/content/2.guide/2.features/4.head-management.md Outdated
## Title Templates
In previous versions of Nuxt, you could use the `titleTemplate` option to dynamically customize the title of your site. However, this is no longer supported in Nuxt 3, and can instead be done via a plugin.

Create a `title.js` plugin within your `plugins` folder, with the following code:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Create a `title.js` plugin within your `plugins` folder, with the following code:
The `titleTemplate` can either be a string, where `%s` is replaced with the title, or a function. If you want to use a function (for full control), then this cannot be set in your `nuxt.config`, and it is recommended instead to set it within your `app.vue` file, where it will apply to all pages on your site:

Comment on lines +37 to +47
```js
export default defineNuxtPlugin(() => {
useHead({
titleTemplate: (titleChunk) => {
return titleChunk ? `${titleChunk} - Site Title` : 'Site Title';
},
});
});
```

Then you can set a title as you usually would:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```js
export default defineNuxtPlugin(() => {
useHead({
titleTemplate: (titleChunk) => {
return titleChunk ? `${titleChunk} - Site Title` : 'Site Title';
},
});
});
```
Then you can set a title as you usually would:

```vue [/pages/my-page.vue]
<script setup>
useHead({
title: 'My Page'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
title: 'My Page'
titleTemplate: (titleChunk) => {
return titleChunk ? `${titleChunk} - Site Title` : 'Site Title';
},

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we needed a plugin for it to work? As the current function approach does nothing.

Copy link
Copy Markdown
Member

@danielroe danielroe May 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the function approach should work everywhere but in nuxt.config. If not, it's a bug and I'll fix it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even with the following example in app.vue:

<script setup>
useHead({
  titleTemplate: (titleChunk) => {
    return titleChunk ? `${titleChunk} - Site` : 'Site: Default Text';
  },
});
</script>

<template>
  <div>
    <NuxtLayout>
      <NuxtPage/>
    </NuxtLayout>
  </div>
</template>

If you don't pass a useHead to your page, it won't default it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's a separate issue.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR to fix it: #5064

});
</script>
```
This will now appear as 'My Page - Site Title' in the browser tab. You can also pass `null` to default to the site title.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This will now appear as 'My Page - Site Title' in the browser tab. You can also pass `null` to default to the site title.
Now, if you set the title to `My Page` with `useHead` on another page of your site, the title would appear as 'My Page - Site Title' in the browser tab. You can also pass `null` to default to the site title.

@danielroe danielroe changed the title docs: Added more detail to titleTemplate. docs: add documentation for titleTemplate May 19, 2022
@danielroe danielroe added the documentation Improvements or additions to documentation label May 19, 2022
Co-authored-by: Daniel Roe <daniel@roe.dev>
@pi0 pi0 added the pending label May 20, 2022
@pi0
Copy link
Copy Markdown
Member

pi0 commented May 20, 2022

Thanks for working on this amazing PR @heychazza @manniL @danielroe 💚

I believe before adding docs and instead of advising to use a Nuxt plugin, we can improve experience by suggesting to either use new app.vue which is meant to be entry of global setup such as layout and styles, it can also be used to declare titleTemplate without an additional file.

Additionally, we could introduce app/router.options support for runtime function.

@danielroe
Copy link
Copy Markdown
Member

danielroe commented May 20, 2022

@pi0 Agreed. I think my pending review comments should exactly implement that 👍 Once suggestions are accepted, it no longer advises to use a Nuxt plugin.

@entity
Copy link
Copy Markdown
Contributor Author

entity commented May 20, 2022

The only issue I've personally found, but this would be a separate issue :-

Setting it within app.vue, if you don't specify a title within a page, it'll show up as null.

I'll be back on PC in ~2 hours so I could make a reproduction repo if needed.

TL;DR is if no title is specified in pages, it doesn't use the default from the titleTemplate.

@danielroe
Copy link
Copy Markdown
Member

@heychazza I have submitted a PR to fix that 😉

@entity
Copy link
Copy Markdown
Contributor Author

entity commented May 20, 2022

@heychazza I have submitted a PR to fix that 😉

Smh Daniel, beating me to it 😂

Thanks though, haha

@entity
Copy link
Copy Markdown
Contributor Author

entity commented May 21, 2022

Oh eek, I tried to do another PR, so I reset my main repo, apologies, will fix that and re-send it.

This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

3.x documentation Improvements or additions to documentation pending

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot set titleTemplate as function

5 participants