Meta Tags and SEO

Nuxt gives you 3 different ways to add meta data to your application:

  • Globally using the nuxt.config.js
  • Locally using the head as an object
  • Locally using the head as a function so that you have access to data and computed properties.

Global Settings

Nuxt lets you define all default <meta> tags for your application inside the nuxt.config.js file using the head property. This is very useful for adding a default title and description tag for SEO purposes or for setting the viewport or adding the favicon.

nuxt.config.js

  1. export default {
  2. head: {
  3. title: 'my website title',
  4. meta: [
  5. { charset: 'utf-8' },
  6. { name: 'viewport', content: 'width=device-width, initial-scale=1' },
  7. {
  8. hid: 'description',
  9. name: 'description',
  10. content: 'my website description'
  11. }
  12. ],
  13. link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }]
  14. }
  15. }

Meta Tags and SEO - 图1

This will give you the same title and description on every page

Local Settings

You can also add titles and meta for each page by setting the head property inside your script tag on every page:

pages/index.vue

  1. <script>
  2. export default {
  3. head: {
  4. title: 'Home page',
  5. meta: [
  6. {
  7. hid: 'description',
  8. name: 'description',
  9. content: 'Home page description'
  10. }
  11. ],
  12. }
  13. }
  14. </script>

Meta Tags and SEO - 图2

Use head as an object to set a title and description only for the home page

pages/index.vue

  1. <template>
  2. <h1>{{ title }}</h1>
  3. </template>
  4. <script>
  5. export default {
  6. data() {
  7. return {
  8. title: 'Home page'
  9. }
  10. },
  11. head() {
  12. return {
  13. title: this.title,
  14. meta: [
  15. {
  16. hid: 'description',
  17. name: 'description',
  18. content: 'Home page description'
  19. }
  20. ]
  21. }
  22. }
  23. }
  24. </script>

Meta Tags and SEO - 图3

Use head as a function to set a title and description only for the home page. By using a function you have access to data and computed properties

Nuxt uses vue-meta to update the document head and meta attributes of your application.

Meta Tags and SEO - 图4

To avoid any duplication when used in child components, please give a unique identifier with the hid key to the meta description. This way vue-meta will know that it has to overwrite the default tag.

Meta Tags and SEO - 图5

Learn more about the options available for head, in the vue-meta documentation.

External Resources

You can include external resources such as scripts and fonts by adding them globally to the nuxt.config.js or locally in the head object or function.

Meta Tags and SEO - 图6

You can also pass each resource an optional body: true to include the resource before the closing </body> tag.

Global Settings

nuxt.config.js

  1. export default {
  2. head: {
  3. script: [
  4. {
  5. src: 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js'
  6. }
  7. ],
  8. link: [
  9. {
  10. rel: 'stylesheet',
  11. href: 'https://fonts.googleapis.com/css?family=Roboto&display=swap'
  12. }
  13. ]
  14. }
  15. }

Local Settings

pages/index.vue

  1. <template>
  2. <h1>About page with jQuery and Roboto font</h1>
  3. </template>
  4. <script>
  5. export default {
  6. head() {
  7. return {
  8. script: [
  9. {
  10. src:
  11. 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js'
  12. }
  13. ],
  14. link: [
  15. {
  16. rel: 'stylesheet',
  17. href: 'https://fonts.googleapis.com/css?family=Roboto&display=swap'
  18. }
  19. ]
  20. }
  21. }
  22. }
  23. </script>
  24. <style scoped>
  25. h1 {
  26. font-family: Roboto, sans-serif;
  27. }
  28. </style>