-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf!: rewrite i18n resolving and url normalizing (#319)
- Loading branch information
Showing
25 changed files
with
845 additions
and
590 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,38 @@ | ||
import { defineEventHandler, getQuery, setHeader } from 'h3' | ||
import { fixSlashes } from 'site-config-stack/urls' | ||
import { appendHeader, defineEventHandler, setHeader } from 'h3' | ||
import { useSimpleSitemapRuntimeConfig } from '../utils' | ||
import { buildSitemapIndex } from '../sitemap/builder/sitemap-index' | ||
import { buildSitemapIndex, urlsToIndexXml } from '../sitemap/builder/sitemap-index' | ||
import type { SitemapOutputHookCtx } from '../../types' | ||
import { createSitePathResolver, useNitroApp, useSiteConfig } from '#imports' | ||
import { useNitroUrlResolvers } from '..//sitemap/nitro' | ||
import { useNitroApp } from '#imports' | ||
|
||
export default defineEventHandler(async (e) => { | ||
const canonicalQuery = getQuery(e).canonical | ||
const isShowingCanonical = typeof canonicalQuery !== 'undefined' && canonicalQuery !== 'false' | ||
const runtimeConfig = useSimpleSitemapRuntimeConfig() | ||
const siteConfig = useSiteConfig(e) | ||
let sitemap = (await buildSitemapIndex({ | ||
event: e, | ||
canonicalUrlResolver: createSitePathResolver(e, { canonical: isShowingCanonical || !import.meta.dev, absolute: true, withBase: true }), | ||
relativeBaseUrlResolver: createSitePathResolver(e, { absolute: false, withBase: true }), | ||
fixSlashes: (path: string) => fixSlashes(siteConfig.trailingSlash, path), | ||
}, runtimeConfig)) | ||
|
||
const nitro = useNitroApp() | ||
const resolvers = useNitroUrlResolvers(e) | ||
const sitemaps = (await buildSitemapIndex(resolvers, runtimeConfig)) | ||
|
||
// tell the prerender to render the other sitemaps (if we prerender this one) | ||
// this solves the dynamic chunking sitemap issue | ||
if (import.meta.prerender) { | ||
appendHeader( | ||
e, | ||
'x-nitro-prerender', | ||
sitemaps.filter(entry => !!entry._sitemapName) | ||
.map(entry => encodeURIComponent(`/${entry._sitemapName}-sitemap.xml`)).join(', '), | ||
) | ||
} | ||
|
||
const indexResolvedCtx = { sitemaps } | ||
await nitro.hooks.callHook('sitemap:index-resolved', indexResolvedCtx) | ||
|
||
const ctx: SitemapOutputHookCtx = { sitemap, sitemapName: 'sitemap' } | ||
const output = urlsToIndexXml(indexResolvedCtx.sitemaps, resolvers, runtimeConfig) | ||
const ctx: SitemapOutputHookCtx = { sitemap: output, sitemapName: 'sitemap' } | ||
await nitro.hooks.callHook('sitemap:output', ctx) | ||
sitemap = ctx.sitemap | ||
|
||
setHeader(e, 'Content-Type', 'text/xml; charset=UTF-8') | ||
if (runtimeConfig.cacheMaxAgeSeconds) | ||
setHeader(e, 'Cache-Control', `public, max-age=${runtimeConfig.cacheMaxAgeSeconds}, must-revalidate`) | ||
else | ||
setHeader(e, 'Cache-Control', `no-cache, no-store`) | ||
return sitemap | ||
return ctx.sitemap | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.