Skip to content

Commit 37277ed

Browse files
committedOct 30, 2024
fix: exclude prerendered redirects from sitemap
1 parent a91a7f7 commit 37277ed

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed
 

‎src/prerender.ts

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ export function isNuxtGenerate(nuxt: Nuxt = useNuxt()) {
4343
].includes(resolveNitroPreset())
4444
}
4545

46+
const NuxtRedirectHtmlRegex = /<!DOCTYPE html><html><head><meta http-equiv="refresh" content="0; url=([^"]+)"><\/head><\/html>/
47+
4648
export function setupPrerenderHandler(_options: { runtimeConfig: ModuleRuntimeConfig, logger: ConsolaInstance }, nuxt: Nuxt = useNuxt()) {
4749
const { runtimeConfig: options, logger } = _options
4850
const prerenderedRoutes = (nuxt.options.nitro.prerender?.routes || []) as string[]
@@ -64,6 +66,10 @@ export function setupPrerenderHandler(_options: { runtimeConfig: ModuleRuntimeCo
6466
// extract alternatives from the html
6567
if (!route.fileName?.endsWith('.html') || !html || ['/200.html', '/404.html'].includes(route.route))
6668
return
69+
// ignore redirects
70+
if (html.match(NuxtRedirectHtmlRegex)) {
71+
return
72+
}
6773

6874
// maybe the user already provided a _sitemap on the route
6975
route._sitemap = defu(route._sitemap, {

‎test/fixtures/basic/nuxt.config.ts

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ export default defineNuxtConfig({
88
site: {
99
url: 'https://nuxtseo.com',
1010
},
11+
routeRules: {
12+
'/foo-redirect': {
13+
redirect: '/foo',
14+
},
15+
},
1116
debug: process.env.NODE_ENV === 'test',
1217
sitemap: {
1318
autoLastmod: false,

‎test/fixtures/basic/pages/index.vue

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
<NuxtLink to="/crawled">
55
crawled
66
</NuxtLink>
7+
<NuxtLink to="/foo-redirect">
8+
should be ignored as its a redirect
9+
</NuxtLink>
710
<br>
811
<a href="/sitemap.xml">
912
sitemap.xml

0 commit comments

Comments
 (0)