-
Notifications
You must be signed in to change notification settings - Fork 128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
npm run build sitemap.xml create not found #248
Comments
Nuxt 3 removed the I am currently working on getting this sitemap-module running on a project with Nuxt 3. If it works out , I may post it here or open a PR. Basicly you need to use the new server middleware which doesn't have access to the nuxt instance which makes things a bit more complicated but possible. |
also see nuxt/nuxt#13559 |
So I kinda did it 🥳, at least a simple version supporting a single sitemap.xml (no sitemapindex.xml etc) If there are enough people who need a full working version, I would offer to create a Nuxt 3 Version of this module. I hope that helps someone 😄
modules: [
// ....
'@nuxtjs/sitemap',
'~/modules/sitemap/module.js',
],
import { defineNuxtModule } from '@nuxt/kit'
export default defineNuxtModule({
setup(options, nuxt) {
/**
* remove default middleware so our works on dev as well
*/
nuxt.options.serverMiddleware = nuxt.options.serverMiddleware.filter((mw) => !mw.path.includes('sitemap.xml'))
/**
* Use addServerHandler to dynamicly add server middlewares in Nuxt 3
*/
// addServerHandler({
// handler: '/path/to/handler',
// })
/**
* pass routes to nitro runtime config
*/
nuxt.hook('nitro:build:before', (nitro) => {
nitro.options.runtimeConfig.sitemap = nuxt.options.runtimeConfig.sitemap
})
/**
* Get all static routes and remove dynamic ones
*/
nuxt.hook('pages:extend', (routes) => {
nuxt.options.sitemap.staticRoutes = routes
.map((r) => r.path)
.filter((path) => ![':', '*'].some((c) => path.includes(c)))
nuxt.options.runtimeConfig.sitemap = nuxt.options.sitemap
})
},
})
import { defineEventHandler } from 'h3'
import { excludeRoutes } from '@nuxtjs/sitemap/lib/routes.js'
import { createRoutesCache } from '@nuxtjs/sitemap/lib/cache.js'
import { createSitemap } from '@nuxtjs/sitemap/lib/builder.js'
import generateETag from 'etag'
import fresh from 'fresh'
export const globalCache = { staticRoutes: null, routes: null }
const defaults = {
path: '/sitemap.xml',
hostname: undefined,
exclude: [],
routes: [],
cacheTime: 1000 * 60 * 15,
etag: false,
filter: undefined,
gzip: false,
xmlNs: undefined,
xslUrl: undefined,
trailingSlash: false,
lastmod: undefined,
i18n: undefined,
defaults: {},
}
export default defineEventHandler(async (event) => {
const config = useRuntimeConfig()
const sitemap = { ...defaults, ...config.sitemap }
if ([sitemap.pathGzip, sitemap.path].includes(event.req.url)) {
const generateDynamicRoutes = async (slug) => {
console.log('[vue-sitemap] generate dynamic routes')
const routes = []
// make requests to an API or something and build an array of routes
return routes
}
/**
* Init cache if not active yet
*/
if (!globalCache.staticRoutes) {
/**
* add dynamic routes
*/
sitemap.routes = async () => [
...(await generateDynamicRoutes('news')),
]
globalCache.staticRoutes = () => excludeRoutes(sitemap.exclude, sitemap.staticRoutes)
globalCache.routes = createRoutesCache(globalCache, sitemap)
}
if (sitemap.gzip) {
try {
// Init sitemap
const routes = await globalCache.routes.get('routes')
const gzip = await createSitemap(sitemap, routes, '/', event.req).toGzip()
// Check cache headers
if (validHttpCache(gzip, sitemap.etag, event.req, event.res)) {
return
}
// Send http response
event.res.setHeader('Content-Type', 'application/gzip')
return event.res.end(gzip)
} catch (err) {
return err
}
}
try {
// Init sitemap
const routes = await globalCache.routes.get('routes')
const xml = await createSitemap(sitemap, routes, '/', event.req).toXML()
// Check cache headers
if (validHttpCache(xml, sitemap.etag, event.req, event.res)) {
return
}
// Send http response
event.res.setHeader('Content-Type', 'application/xml')
return xml
} catch (err) {
return err
}
}
})
function validHttpCache(entity, options, req, res) {
if (!options) {
return false
}
const { hash } = options
const etag = hash ? hash(entity, options) : generateETag(entity, options)
if (fresh(req.headers, { etag })) {
res.statusCode = 304
res.end()
return true
}
// Add ETag header
res.setHeader('ETag', etag)
return false
} |
I guess I'm missing something. I get the following error when going to /sitemap.xml:
What am I doing wrong? I added this to nuxt.config.ts (in addition to the things you posted above):
|
try console.log in the sitemap.ts const config = useRuntimeConfig()
const sitemap = { ...defaults, ...config.sitemap }
console.log(sitemap) and in the module.js nuxt.hook('pages:extend', (routes) => {
console.log('adding static routes to nitro config....')
......
}) static routes are only set once during build time so if you add a new static page you need to restart the server e.g. yarn dev / yarn build && yarn start |
Followed your steps but getting undefined errors on nuxt.options.serverMiddleware? Edit: Error appears to no longer be occurring. |
@d3xter-dev I do see the "adding static routes to nitro config" console log.
|
@Lars- yeah the staticRotues are missing so, something seems to be wrong in the @scottingle you could try |
I'll check later. It takes a bit too much time for now unfortunately. But I really do appreciate the replies. If I know more, I'll let you know. |
Would this work with dynamic routes? |
@scottingle nope, they should be excluded thats what's the nuxt.options.sitemap.staticRoutes = routes
.map((r) => r.path)
.filter((path) => ![':', '*'].some((c) => path.includes(c))) is for, dynamic routes need to be added in the /**
* add dynamic routes
*/
sitemap.routes = async () => [
...(await generateDynamicRoutes('products')),
]
.... |
Hey @d3xter-dev, thanks for providing a solution for Nuxt 3! Does this support Nuxt Content 2? |
@heychazza not sure what you mean by that |
Does this support the new version of Nuxt Content? |
Thank you @d3xter-dev for helping make this plugin work with V3 ! Can you confirm me that this fix doesn't work with a sitemapindex ? I'm using an index of sitemaps on my app and waiting to switch to V3. |
@heychazza ah that's another nuxt module I see, should be fine as long as Nuxt Content loads before the @desaintflorent yeah you would have to add support for a sitemap-module/lib/middleware.js Lines 124 to 167 in 0bc0ebd
|
I would greatly appreciate a nuxt3 module for this :d, and sooner than later, thousands of devs. |
@d3xter-dev Pog. Could you provide a Repository to contribute on? Seems the next-sitemap guys are kinda inactive |
is this still not available in nuxt 3? |
We decided that we'll start working on porting the whole module to Nuxt 3 in the next few days 😄 |
wow thank you @d3xter-dev looking forward to it! |
Damn seems nuxt content isn't yet supported by this :(
|
I am happy to announce 🥳
https://github.com/funken-studio/sitemap-module-nuxt-3/tree/experimental Please give me feedback :) |
@d3xter-dev |
Yeah, you're correct. I'm also facing the same issue for nuxt content :( |
In npm run dev state, sitemap.xml is created, but in npm run start state after npm run build, sitemap.xml is not created.
Is /sitemap.xml normal after npm run dev command?
After npm run build and after npm run start, sitemap.xml 404 error is returned. I hope it gets patched soon.
Currently, it is temporarily used as https://github.com/benoitdemaegdt/nuxt3-sitemap.
npm 8.5.0
Node 16.14.2
nuxtjs3 v3.0.0-rc.1
The text was updated successfully, but these errors were encountered: