Skip to content

Commit

Permalink
fix(prerendering): prefer runtime site url validation
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Sep 2, 2024
1 parent 6ef8dcd commit 779d100
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
4 changes: 0 additions & 4 deletions src/prerender.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { mkdir, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { withBase } from 'ufo'
import { assertSiteConfig } from 'nuxt-site-config-kit'
import { useNuxt } from '@nuxt/kit'
import type { Nuxt } from '@nuxt/schema'
import type { Nitro, PrerenderRoute } from 'nitropack'
Expand Down Expand Up @@ -59,9 +58,6 @@ export function setupPrerenderHandler(_options: { runtimeConfig: ModuleRuntimeCo
let prerenderer: Nitro
nitro.hooks.hook('prerender:init', async (_prerenderer: Nitro) => {
prerenderer = _prerenderer
assertSiteConfig('@nuxtjs/sitemap', {
url: 'Required to generate absolute canonical URLs for your sitemap.',
}, { throwError: false })
})
nitro.hooks.hook('prerender:generate', async (route) => {
const html = route.contents
Expand Down
16 changes: 14 additions & 2 deletions src/runtime/nitro/sitemap/nitro.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getQuery, setHeader } from 'h3'
import { getQuery, setHeader, createError } from 'h3'
import type { H3Event } from 'h3'
import { fixSlashes } from 'site-config-stack/urls'
import { defu } from 'defu'
Expand All @@ -10,7 +10,7 @@ import type {
SitemapDefinition,
SitemapRenderCtx,
} from '../../types'
import { mergeOnKey, splitForLocales } from '../../utils-pure'
import { logger, mergeOnKey, splitForLocales } from '../../utils-pure'
import { createNitroRouteRuleMatcher } from '../kit'
import { buildSitemapUrls, urlsToXml } from './builder/sitemap'
import { normaliseEntry } from './urlset/normalise'
Expand All @@ -37,6 +37,18 @@ export function useNitroUrlResolvers(e: H3Event): NitroUrlResolvers {
export async function createSitemap(event: H3Event, definition: SitemapDefinition, runtimeConfig: ModuleRuntimeConfig) {
const { sitemapName } = definition
const nitro = useNitroApp()
if (import.meta.prerender) {
const config = useSiteConfig(event)
if (!config.url && !nitro._sitemapWarned) {
nitro._sitemapWarned = true
logger.error('Sitemap Site URL missing!')
logger.info('To fix this please add `{ site: { url: \'site.com\' } }` to your Nuxt config or a `NUXT_PUBLIC_SITE_URL=site.com` to your .env. Learn more at https://nuxtseo.com/site-config/getting-started/how-it-works')
throw new createError({
statusMessage: 'You must provide a site URL to prerender a sitemap.',
statusCode: 500,
})
}
}
const resolvers = useNitroUrlResolvers(event)
let sitemapUrls = await buildSitemapUrls(definition, resolvers, runtimeConfig)

Expand Down

0 comments on commit 779d100

Please sign in to comment.