d3xter-dev / sitemap-module-nuxt-3

Sitemap Module for Nuxt 3
https://sitemap.nuxtjs.org
MIT License
63 stars 12 forks source link

Hardcoded `.output/public` causing issues on Netlify #13

Open Anoesj opened 1 year ago

Anoesj commented 1 year ago

Hey!

I've run into some issues with this module when deploying a statically generated Nuxt app (using nuxt generate) to Netlify.

This module seems to assume that the output dir is always .output/public in the Nuxt source directory, because sitemaps are always basically written to path.join(nuxtInstance.options.srcDir, '.output/public), but Nitro recognizes Netlify (probably by checking process.env.NETLIFY), sets NITRO_PRESET to netlify and that changes the output directory to dist instead of .output/public. I tried to fix this in a PR, but I'm still getting some failing tests.

Could you look into this? Consider my PR just an experiment, I'm not a very experienced open source dev :sweat_smile:.

You can mimic Nitro's behavior on Netlify by running NITRO_PRESET=netlify yarn build-module. If ran, the current tests fail too.

d3xter-dev commented 1 year ago

Thanks! I will have a look

Anoesj commented 1 year ago

Hey @d3xter-dev, I edited the original post, because I found out it has to do with the NITRO_PRESET environment variable. You can find all Nitro presets here: https://github.com/unjs/nitro/blob/main/src/presets/index.ts. This is the file where Nitro presets are resolved: https://github.com/unjs/nitro/blob/main/src/options.ts.

ErwinAI commented 1 year ago

Changes look good to me. @d3xter-dev Any chance this can be merged / released?

Anoesj commented 1 year ago

Tbh this probably needs some extra work if some tests are failing. It just needs an extra set of eyes for a few hours.

offline-first commented 1 year ago

Any updates?

mariuscdejong commented 1 year ago

As a workaround for now, you can use this guide, replace the query with the dynamic routes.

https://content.nuxtjs.org/guide/recipes/sitemap

For example:

// server/routes/sitemap.xml.ts
import { SitemapStream, streamToPromise } from 'sitemap'

export default defineEventHandler(async (event) => {
  const config = useRuntimeConfig()
  const links = await $fetch('/api/routes')

  const sitemap = new SitemapStream({
    hostname: config.public.storyblok.siteUrl,
  })

  for (const link of links) {
    sitemap.write({
      url: link,
      changefreq: 'monthly'
    })
  }

  sitemap.end()
  return streamToPromise(sitemap)
})

Make sure to update your nuxt config to prerender the sitemap for static generation:

nitro: {
  prerender: {
    routes: ['/sitemap.xml'],
  }
}
jankohlbach commented 1 year ago

same issue here with vercel at least now I know what's the issue 😅 thanks @Anoesj for the fix @d3xter-dev is there a plan to merge this? otherwise I need to look for another solution, but this one would be the best

andrevferreiraa commented 1 year ago

Any plans to merge this?