Giter VIP home page Giter VIP logo

sitemap-module-nuxt-3's Introduction

@nuxtjs/sitemap

Sitemap Module

npm (scoped with tag) Downloads Build Status Coverage Status License

Automatically generate or serve dynamic sitemap.xml for Nuxt projects!

Read Documentation

๐Ÿ“– Release Notes

Generate sitemap.xml

Normally the sitemap.xml is served via a server middleware / handler, it is only generated in .output/public when running nuxi generate.

If you want to generate the sitemap.xml on every build, you can set the generateOnBuild option to true in the module configuration. (That option might not work if you are using dynamic routes)

// nuxt.config.js
modules: {
  ...
  ['@funken-studio/sitemap-nuxt-3', { generateOnBuild: true }],
  ...
}

Using dynamic routes

  • you are not able to use imports!!
  • see below for a usable workaround:

nuxt.config.ts

import dynamicRoutes from './helpers/dynamicRoutes'
...
 modules: [
    '@funken-studio/sitemap-nuxt-3',
],
sitemap: {
    hostname: 'https://example.com', 
    cacheTime: 1,
    routes: dynamicRoutes,
    defaults: {
      changefreq: 'daily',
      priority: 1,
      lastmod: new Date().toISOString(),
    },
},
...

/helpers/dynamicRoutes

/**
 * since we can't use imports here we just fetch
 * all our routes from a custom API endpoint where we can use imports
 */
export default async () => {
  return await $fetch('/api/sitemap_routes', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
  })
}

/server/api/sitemap_routes.ts

import { apiPlugin } from '@storyblok/vue'
import { eventHandler } from 'h3'

/**
 * We are using Storyblok as our CMS,
 * in order to have all news and testimonials pages in our sitemap
 * we need to fetch some from Storyblok
 */
export default eventHandler(async (event) => {
  const { req, res } = event
  if (req.method !== 'POST') {
    res.statusCode = 405
    res.end()
    return
  }
  const config = useRuntimeConfig()
  const { storyblokApi } = apiPlugin({ apiOptions: config.public.storyblok })
  console.log('[vue-sitemap] generate dynamic routes')

  const fetchRoutes = async (slug) => {
    const routes = []
    const pageInfo = await storyblokApi.get(`cdn/stories/?starts_with=${slug}`, {
      version: 'published',
      per_page: 1,
      page: 1,
    })

    const totalPages = Math.ceil(pageInfo.headers.total / 25)
    for (let page = 1; page <= totalPages; page++) {
      const pageNews = await storyblokApi.get(`cdn/stories/?starts_with=${slug}`, {
        version: 'published',
        page: page,
      })

      for (const news of pageNews.data.stories) {
        routes.push(`/${slug}/${news.slug}`)
      }

      routes.push(`/${slug}/${page}`)
    }
    return routes
  }

  return [...(await fetchRoutes('news')), ...(await fetchRoutes('testimonials'))]
})

License

MIT License

Contributors

sitemap-module-nuxt-3's People

Contributors

adam-lynch avatar ansidev avatar atinux avatar beardcoder avatar d3xter-dev avatar delucis avatar dennisdebest avatar dependabot[bot] avatar dohomi avatar geminii avatar idorenyinudoh avatar mannil avatar miteyema avatar nicopennec avatar oemueller avatar renovate-bot avatar renovate[bot] avatar ricardogobbosouza avatar sotnikovse avatar trurlmcbyte avatar welluh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.