mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 13:45:18 +00:00
fix(nuxt): also augment page metadata after pages:extend
hook (#27134)
This commit is contained in:
parent
da28077be3
commit
0536dbeed9
@ -386,7 +386,7 @@ This option allows exposing some route metadata defined in `definePageMeta` at b
|
|||||||
|
|
||||||
This only works with static or strings/arrays rather than variables or conditional assignment. See [original issue](https://github.com/nuxt/nuxt/issues/24770) for more information and context.
|
This only works with static or strings/arrays rather than variables or conditional assignment. See [original issue](https://github.com/nuxt/nuxt/issues/24770) for more information and context.
|
||||||
|
|
||||||
<!-- You can disable this feature if it causes issues in your project.
|
You can disable this feature if it causes issues in your project.
|
||||||
|
|
||||||
```ts twoslash [nuxt.config.ts]
|
```ts twoslash [nuxt.config.ts]
|
||||||
export default defineNuxtConfig({
|
export default defineNuxtConfig({
|
||||||
@ -394,7 +394,7 @@ export default defineNuxtConfig({
|
|||||||
scanPageMeta: false
|
scanPageMeta: false
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
``` -->
|
```
|
||||||
|
|
||||||
## cookieStore
|
## cookieStore
|
||||||
|
|
||||||
|
@ -61,8 +61,12 @@ export default defineNuxtModule({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const pages = await resolvePagesRoutes()
|
const pages = await resolvePagesRoutes()
|
||||||
await nuxt.callHook('pages:extend', pages)
|
if (pages.length) {
|
||||||
if (pages.length) { return true }
|
if (nuxt.apps.default) {
|
||||||
|
nuxt.apps.default.pages = pages
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -75,7 +79,6 @@ export default defineNuxtModule({
|
|||||||
|
|
||||||
nuxt.hook('app:templates', async (app) => {
|
nuxt.hook('app:templates', async (app) => {
|
||||||
app.pages = await resolvePagesRoutes()
|
app.pages = await resolvePagesRoutes()
|
||||||
await nuxt.callHook('pages:extend', app.pages)
|
|
||||||
|
|
||||||
if (!nuxt.options.ssr && app.pages.some(p => p.mode === 'server')) {
|
if (!nuxt.options.ssr && app.pages.some(p => p.mode === 'server')) {
|
||||||
logger.warn('Using server pages with `ssr: false` is not supported with auto-detected component islands. Set `experimental.componentIslands` to `true`.')
|
logger.warn('Using server pages with `ssr: false` is not supported with auto-detected component islands. Set `experimental.componentIslands` to `true`.')
|
||||||
@ -153,10 +156,9 @@ export default defineNuxtModule({
|
|||||||
logs: nuxt.options.debug,
|
logs: nuxt.options.debug,
|
||||||
async beforeWriteFiles (rootPage) {
|
async beforeWriteFiles (rootPage) {
|
||||||
rootPage.children.forEach(child => child.delete())
|
rootPage.children.forEach(child => child.delete())
|
||||||
let pages = nuxt.apps.default?.pages
|
const pages = nuxt.apps.default?.pages || await resolvePagesRoutes()
|
||||||
if (!pages) {
|
if (nuxt.apps.default) {
|
||||||
pages = await resolvePagesRoutes()
|
nuxt.apps.default.pages = pages
|
||||||
await nuxt.callHook('pages:extend', pages)
|
|
||||||
}
|
}
|
||||||
function addPage (parent: EditableTreeNode, page: NuxtPage) {
|
function addPage (parent: EditableTreeNode, page: NuxtPage) {
|
||||||
// @ts-expect-error TODO: either fix types upstream or figure out another
|
// @ts-expect-error TODO: either fix types upstream or figure out another
|
||||||
|
@ -58,21 +58,30 @@ export async function resolvePagesRoutes (): Promise<NuxtPage[]> {
|
|||||||
scannedFiles.sort((a, b) => a.relativePath.localeCompare(b.relativePath, 'en-US'))
|
scannedFiles.sort((a, b) => a.relativePath.localeCompare(b.relativePath, 'en-US'))
|
||||||
|
|
||||||
const allRoutes = await generateRoutesFromFiles(uniqueBy(scannedFiles, 'relativePath'), {
|
const allRoutes = await generateRoutesFromFiles(uniqueBy(scannedFiles, 'relativePath'), {
|
||||||
shouldExtractBuildMeta: nuxt.options.experimental.scanPageMeta || nuxt.options.experimental.typedPages,
|
|
||||||
shouldUseServerComponents: !!nuxt.options.experimental.componentIslands,
|
shouldUseServerComponents: !!nuxt.options.experimental.componentIslands,
|
||||||
vfs: nuxt.vfs,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return uniqueBy(allRoutes, 'path')
|
const pages = uniqueBy(allRoutes, 'path')
|
||||||
|
|
||||||
|
const shouldAugment = nuxt.options.experimental.scanPageMeta || nuxt.options.experimental.typedPages
|
||||||
|
|
||||||
|
if (shouldAugment) {
|
||||||
|
const augmentedPages = await augmentPages(pages, nuxt.vfs)
|
||||||
|
await nuxt.callHook('pages:extend', pages)
|
||||||
|
await augmentPages(pages, nuxt.vfs, augmentedPages)
|
||||||
|
augmentedPages.clear()
|
||||||
|
} else {
|
||||||
|
await nuxt.callHook('pages:extend', pages)
|
||||||
|
}
|
||||||
|
|
||||||
|
return pages
|
||||||
}
|
}
|
||||||
|
|
||||||
type GenerateRoutesFromFilesOptions = {
|
type GenerateRoutesFromFilesOptions = {
|
||||||
shouldExtractBuildMeta?: boolean
|
|
||||||
shouldUseServerComponents?: boolean
|
shouldUseServerComponents?: boolean
|
||||||
vfs?: Record<string, string>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function generateRoutesFromFiles (files: ScannedFile[], options: GenerateRoutesFromFilesOptions = {}): Promise<NuxtPage[]> {
|
export function generateRoutesFromFiles (files: ScannedFile[], options: GenerateRoutesFromFilesOptions = {}): NuxtPage[] {
|
||||||
const routes: NuxtPage[] = []
|
const routes: NuxtPage[] = []
|
||||||
|
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
@ -124,17 +133,26 @@ export async function generateRoutesFromFiles (files: ScannedFile[], options: Ge
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.shouldExtractBuildMeta && options.vfs) {
|
|
||||||
const fileContent = file.absolutePath in options.vfs ? options.vfs[file.absolutePath] : fs.readFileSync(file.absolutePath, 'utf-8')
|
|
||||||
Object.assign(route, await getRouteMeta(fileContent, file.absolutePath))
|
|
||||||
}
|
|
||||||
|
|
||||||
parent.push(route)
|
parent.push(route)
|
||||||
}
|
}
|
||||||
|
|
||||||
return prepareRoutes(routes)
|
return prepareRoutes(routes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function augmentPages (routes: NuxtPage[], vfs: Record<string, string>, augmentedPages = new Set<NuxtPage>()) {
|
||||||
|
for (const route of routes) {
|
||||||
|
if (!augmentedPages.has(route) && route.file) {
|
||||||
|
const fileContent = route.file in vfs ? vfs[route.file] : fs.readFileSync(route.file, 'utf-8')
|
||||||
|
Object.assign(route, await getRouteMeta(fileContent, route.file))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (route.children && route.children.length > 0) {
|
||||||
|
await augmentPages(route.children, vfs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return augmentedPages
|
||||||
|
}
|
||||||
|
|
||||||
const SFC_SCRIPT_RE = /<script[^>]*>([\s\S]*?)<\/script[^>]*>/i
|
const SFC_SCRIPT_RE = /<script[^>]*>([\s\S]*?)<\/script[^>]*>/i
|
||||||
export function extractScriptContent (html: string) {
|
export function extractScriptContent (html: string) {
|
||||||
const match = html.match(SFC_SCRIPT_RE)
|
const match = html.match(SFC_SCRIPT_RE)
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { describe, expect, it, vi } from 'vitest'
|
import { describe, expect, it, vi } from 'vitest'
|
||||||
import type { NuxtPage } from 'nuxt/schema'
|
import type { NuxtPage } from 'nuxt/schema'
|
||||||
import { generateRoutesFromFiles, normalizeRoutes, pathToNitroGlob } from '../src/pages/utils'
|
import { augmentPages, generateRoutesFromFiles, normalizeRoutes, pathToNitroGlob } from '../src/pages/utils'
|
||||||
import { generateRouteKey } from '../src/pages/runtime/utils'
|
import { generateRouteKey } from '../src/pages/runtime/utils'
|
||||||
|
|
||||||
describe('pages:generateRoutesFromFiles', () => {
|
describe('pages:generateRoutesFromFiles', () => {
|
||||||
@ -568,11 +568,12 @@ describe('pages:generateRoutesFromFiles', () => {
|
|||||||
) as Record<string, string>
|
) as Record<string, string>
|
||||||
|
|
||||||
try {
|
try {
|
||||||
result = await generateRoutesFromFiles(test.files.map(file => ({
|
result = generateRoutesFromFiles(test.files.map(file => ({
|
||||||
shouldUseServerComponents: true,
|
shouldUseServerComponents: true,
|
||||||
absolutePath: file.path,
|
absolutePath: file.path,
|
||||||
relativePath: file.path.replace(/^(pages|layer\/pages)\//, ''),
|
relativePath: file.path.replace(/^(pages|layer\/pages)\//, ''),
|
||||||
})), { shouldExtractBuildMeta: true, vfs })
|
})))
|
||||||
|
await augmentPages(result, vfs)
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
expect(error.message).toEqual(test.error)
|
expect(error.message).toEqual(test.error)
|
||||||
}
|
}
|
||||||
|
@ -378,7 +378,7 @@ export default defineUntypedSchema({
|
|||||||
*
|
*
|
||||||
* https://github.com/nuxt/nuxt/issues/24770
|
* https://github.com/nuxt/nuxt/issues/24770
|
||||||
*/
|
*/
|
||||||
scanPageMeta: false,
|
scanPageMeta: true,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Automatically share payload _data_ between pages that are prerendered. This can result in a significant
|
* Automatically share payload _data_ between pages that are prerendered. This can result in a significant
|
||||||
|
Loading…
Reference in New Issue
Block a user