Blog>
Snippets

Using Nuxt.js for Automatic SEO Improvements in Vue.js 3

Outline the process of setting up a Nuxt.js project with Vue.js 3 for achieving out-of-the-box SEO enhancements like server-side rendering and auto-generated meta tags.
// 1. Install Nuxt.js 3 (assuming you have Node.js and npm installed)
npm init nuxt@3
This command creates a new Nuxt.js 3 project, which is compatible with Vue.js 3 by default.
// 2. Create a new page in the pages directory
cd <your-project-name>
mkdir pages
touch pages/index.vue
Navigate to your Nuxt.js project directory, create a 'pages' directory if it doesn't already exist, and then create a new 'index.vue' file for the homepage.
<template>
  <h1>My Nuxt.js Page</h1>
</template>

<script>
export default {
  // Using Nuxt's asyncData for server-side rendering
  async asyncData() {
    // Fetch data here for server-side rendering
  },
  // Define meta tags for this page
  head() {
    return {
      title: 'My Nuxt.js Page',
      meta: [
        { hid: 'description', name: 'description', content: 'This is an example of a meta description.' }
      ]
    };
  }
};
</script>
In this Vue file, we create a simple template for the homepage. The asyncData method is used for server-side rendering of async content. The head method is used to define the meta tags for SEO purposes, like the page title and meta description.
// 3. Start the development server
npm run dev
This command starts the local development server for your Nuxt.js project so you can see your page in action with SSR and SEO meta tags.