Working with Meta Tags in Nuxt.js

Tutorial 2 of 5

Introduction

In this tutorial, we will be discussing how to use meta tags in a Nuxt.js application. Meta tags are crucial for SEO because they provide search engines with information about your web page. You will learn how to effectively incorporate these tags into your Nuxt.js application to optimize your site's discoverability.

By the end of this tutorial, you will be able to:
- Understand the role of meta tags in SEO
- Use Nuxt.js’s built-in head method to manage meta tags
- Customize meta tags for each page in your Nuxt.js application

Before you start, it's recommended that you have a basic understanding of Nuxt.js and Vue.js.

Step-by-Step Guide

Nuxt.js uses Vue Meta to update the headers and HTML attributes of your application. Vue Meta is a Vue.js plugin that manages your app's meta information.

To start, you need to set up the head method in your page's component. This method returns an object where you can set the page's meta tags.

Example:

export default {
  head: {
    title: 'Page Title',
    meta: [
      { hid: 'description', name: 'description', content: 'Page Description' }
    ]
  }
}

In this example, hid is used as a unique identifier. It helps to avoid duplicate meta tags when you have global and page-specific meta tags.

Code Examples

Let's look at a few examples of how to use meta tags effectively:

1. Setting Global Meta Tags:

In your nuxt.config.js file, you can set global meta tags that will be applied to all pages.

export default {
  head: {
    title: 'Global Title',
    meta: [
      { hid: 'description', name: 'description', content: 'Global Description' }
    ]
  }
}

2. Overriding Global Meta Tags:

You can override global meta tags in your page-specific component like so:

export default {
  head: {
    title: 'Page Title',
    meta: [
      { hid: 'description', name: 'description', content: 'Page Description' }
    ]
  }
}

Summary

In this tutorial, we learned about the importance of meta tags for SEO and how to use them in a Nuxt.js application. We also learned how to set global and page-specific meta tags, and how to override global tags when necessary.

Next, you might want to learn about other SEO techniques for Nuxt.js, such as generating dynamic routes for your pages.

Practice Exercises

  1. Set up a Nuxt.js application and add global meta tags in the nuxt.config.js file.
  2. Create a page-specific component and override the global meta tags.
  3. Create another component and add meta tags without the hid property. Observe what happens when you have duplicate meta tags.

Remember, practice is key when learning a new concept. Keep experimenting with different meta tags and observe how they affect your SEO. Happy coding!