Star
1.1K

Trailing Slashes in Vue & Nuxt

Learn why we might use a trailing slash, best practices for them and how to handle them in Nuxt.
Last Updated
Published

Introduction

Avoiding Duplicate Content with Trailing Slashes

Nuxt SEO

Sitemap URLs will have their trailing slashes removed by default. If you want to keep them, you can set the trailingSlash option to true:

nuxt.config.ts
export default defineNuxtConfig({
  site: {
    // optional: only if you have trailing slashes enabled
    trailingSlash: true
  },
})

Trailing slashes defines whether your URLs should end with a /. It's important to choose one option and stick with it to avoid duplicate content issues.

By default, the trailing slash is removed. Meaning any links generated by Nuxt SEO will have the trailing slash removed.

- /my-link-to-somewhere/
+ /my-link-to-somewhere

Enabling Trailing Slashes

Nuxt SEO allows you to enable global trailing slashes using site config.

This will automatically add trailing slashes to your sitemap, canonical URLs and more.

nuxt.config.ts
export default defineNuxtConfig({
  site: {
    trailingSlash: true,
  }
})

While Nuxt SEO can handle the correct trailing slashes for all of its own modules, it can't correct the behaviour of <NuxtLink> or other modules.

You will need to make sure you're using the correct slashes for these.

Alternatively, you can use the <SiteLink> component.

This has the exact same API as <NuxtLink> but will provide extra link resolving features.

<template>
  <!-- Will be set to /about/ if trailingSlash is enabled -->
  <SiteLink to="/about">
    About
  </SiteLink>
</template>

Props

  • withBase - Will add the app.baseURL to the link
  • absolute - Will render the link as an absolute path.