Socket
Socket
Sign inDemoInstall

@funken-studio/sitemap-nuxt-3

Package Overview
Dependencies
185
Maintainers
3
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @funken-studio/sitemap-nuxt-3

Automatically generate or serve dynamic sitemap.xml for Nuxt projects


Version published
Weekly downloads
1.7K
decreased by-24.23%
Maintainers
3
Install size
46.4 MB
Created
Weekly downloads
 

Changelog

Source

4.0.4 (2022-11-19)

Bug Fixes

  • different sitemaps sharing same routes cache (8c959fe)

Readme

Source

@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

Keywords

FAQs

Last updated on 19 Nov 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc