Skip to content

Commit

Permalink
fix: exclude prerendered redirects from sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Oct 30, 2024
1 parent a91a7f7 commit 37277ed
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export function isNuxtGenerate(nuxt: Nuxt = useNuxt()) {
].includes(resolveNitroPreset())
}

const NuxtRedirectHtmlRegex = /<!DOCTYPE html><html><head><meta http-equiv="refresh" content="0; url=([^"]+)"><\/head><\/html>/

export function setupPrerenderHandler(_options: { runtimeConfig: ModuleRuntimeConfig, logger: ConsolaInstance }, nuxt: Nuxt = useNuxt()) {
const { runtimeConfig: options, logger } = _options
const prerenderedRoutes = (nuxt.options.nitro.prerender?.routes || []) as string[]
Expand All @@ -64,6 +66,10 @@ export function setupPrerenderHandler(_options: { runtimeConfig: ModuleRuntimeCo
// extract alternatives from the html
if (!route.fileName?.endsWith('.html') || !html || ['/200.html', '/404.html'].includes(route.route))
return
// ignore redirects
if (html.match(NuxtRedirectHtmlRegex)) {
return
}

// maybe the user already provided a _sitemap on the route
route._sitemap = defu(route._sitemap, {
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/basic/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export default defineNuxtConfig({
site: {
url: 'https://nuxtseo.com',
},
routeRules: {
'/foo-redirect': {
redirect: '/foo',
},
},
debug: process.env.NODE_ENV === 'test',
sitemap: {
autoLastmod: false,
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/basic/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<NuxtLink to="/crawled">
crawled
</NuxtLink>
<NuxtLink to="/foo-redirect">
should be ignored as its a redirect
</NuxtLink>
<br>
<a href="/sitemap.xml">
sitemap.xml
Expand Down

0 comments on commit 37277ed

Please sign in to comment.