perf(nuxt): clear route meta build cache when pages change (#25514)

This commit is contained in:
Bobbie Goede 2024-02-04 22:27:32 +01:00 committed by GitHub
parent 541a802fef
commit 3e5560ed42
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -135,18 +135,25 @@ export function extractScriptContent (html: string) {
const PAGE_META_RE = /(definePageMeta\([\s\S]*?\))/ const PAGE_META_RE = /(definePageMeta\([\s\S]*?\))/
const DYNAMIC_META_KEY = '__nuxt_dynamic_meta_key' as const const DYNAMIC_META_KEY = '__nuxt_dynamic_meta_key' as const
const pageContentsCache: Record<string, string> = {}
const metaCache: Record<string, Partial<Record<keyof NuxtPage, any>>> = {} const metaCache: Record<string, Partial<Record<keyof NuxtPage, any>>> = {}
async function getRouteMeta (contents: string, absolutePath?: string): Promise<Partial<Record<keyof NuxtPage, any>>> { async function getRouteMeta (contents: string, absolutePath: string): Promise<Partial<Record<keyof NuxtPage, any>>> {
if (contents in metaCache) { return metaCache[contents] } // set/update pageContentsCache, invalidate metaCache on cache mismatch
if (!(absolutePath in pageContentsCache) || pageContentsCache[absolutePath] !== contents) {
pageContentsCache[absolutePath] = contents
delete metaCache[absolutePath]
}
if (absolutePath in metaCache) { return metaCache[absolutePath] }
const script = extractScriptContent(contents) const script = extractScriptContent(contents)
if (!script) { if (!script) {
metaCache[contents] = {} metaCache[absolutePath] = {}
return {} return {}
} }
if (!PAGE_META_RE.test(script)) { if (!PAGE_META_RE.test(script)) {
metaCache[contents] = {} metaCache[absolutePath] = {}
return {} return {}
} }
@ -158,7 +165,7 @@ async function getRouteMeta (contents: string, absolutePath?: string): Promise<P
}) as unknown as Program }) as unknown as Program
const pageMetaAST = ast.body.find(node => node.type === 'ExpressionStatement' && node.expression.type === 'CallExpression' && node.expression.callee.type === 'Identifier' && node.expression.callee.name === 'definePageMeta') const pageMetaAST = ast.body.find(node => node.type === 'ExpressionStatement' && node.expression.type === 'CallExpression' && node.expression.callee.type === 'Identifier' && node.expression.callee.name === 'definePageMeta')
if (!pageMetaAST) { if (!pageMetaAST) {
metaCache[contents] = {} metaCache[absolutePath] = {}
return {} return {}
} }
@ -221,7 +228,7 @@ async function getRouteMeta (contents: string, absolutePath?: string): Promise<P
extractedMeta.meta[DYNAMIC_META_KEY] = dynamicProperties extractedMeta.meta[DYNAMIC_META_KEY] = dynamicProperties
} }
metaCache[contents] = extractedMeta metaCache[absolutePath] = extractedMeta
return extractedMeta return extractedMeta
} }