fix(nuxt): extract all-literal page meta (#27821)

This commit is contained in:
Alexander Lichter 2024-06-25 18:53:30 +02:00 committed by GitHub
parent 9d3b43a6c8
commit d23c79f395
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 7 deletions

View File

@ -256,13 +256,19 @@ export async function getRouteMeta (contents: string, absolutePath: string): Pro
extractedMeta[key] = property.value.value extractedMeta[key] = property.value.value
} }
const extraneousMetaKeys = pageMetaArgument.properties for (const property of pageMetaArgument.properties) {
.filter(property => property.type === 'Property' && property.key.type === 'Identifier' && !(extractionKeys as unknown as string[]).includes(property.key.name)) if (property.type !== 'Property') {
// @ts-expect-error inferred types have been filtered out continue
.map(property => property.key.name) }
const isIdentifierOrLiteral = property.key.type === 'Literal' || property.key.type === 'Identifier'
if (extraneousMetaKeys.length) { if (!isIdentifierOrLiteral) {
dynamicProperties.add('meta') continue
}
const name = property.key.type === 'Identifier' ? property.key.name : String(property.value)
if (!(extractionKeys as unknown as string[]).includes(name)) {
dynamicProperties.add('meta')
break
}
} }
if (dynamicProperties.size) { if (dynamicProperties.size) {

View File

@ -75,6 +75,28 @@ describe('page metadata', () => {
} }
`) `)
}) })
it('should extract serialisable metadata all quoted', async () => {
const meta = await getRouteMeta(`
<script setup>
definePageMeta({
"otherValue": {
foo: 'bar',
},
})
</script>
`, filePath)
expect(meta).toMatchInlineSnapshot(`
{
"meta": {
"__nuxt_dynamic_meta_key": Set {
"meta",
},
},
}
`)
})
}) })
describe('normalizeRoutes', () => { describe('normalizeRoutes', () => {