fix(nuxt): prioritise later items in pages:routerOptions hook (#25509)

This commit is contained in:
Daniel Roe 2024-01-30 13:55:18 +00:00 committed by GitHub
parent b96fe1ecec
commit f1fe97fc8a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 12 additions and 9 deletions

View File

@ -92,7 +92,7 @@ export default <RouterConfig> {
} }
``` ```
It is possible to add more router options files by adding files within the `pages:routerOptions` hook. It is possible to add more router options files by adding files within the `pages:routerOptions` hook. Later items in the array override earlier ones.
::alert ::alert
Adding a router options file in this hook will switch on page-based routing, unless `optional` is set, in which case it will only apply when page-based routing is already enabled. Adding a router options file in this hook will switch on page-based routing, unless `optional` is set, in which case it will only apply when page-based routing is already enabled.

View File

@ -50,7 +50,7 @@ Hook | Arguments | Description
`builder:generateApp` | `options` | Called before generating the app. `builder:generateApp` | `options` | Called before generating the app.
`builder:watch` | `event, path` | Called at build time in development when the watcher spots a change to a file or directory in the project. `builder:watch` | `event, path` | Called at build time in development when the watcher spots a change to a file or directory in the project.
`pages:extend` | `pages` | Called after pages routes are resolved. `pages:extend` | `pages` | Called after pages routes are resolved.
`pages:routerOptions` | `{ files: Array<{ path: string, optional?: boolean }> }` | Called when resolving `router.options` files. `pages:routerOptions` | `{ files: Array<{ path: string, optional?: boolean }> }` | Called when resolving `router.options` files. Later items in the array override earlier ones.
`server:devHandler` | `handler` | Called when the dev middleware is being registered on the Nitro dev server. `server:devHandler` | `handler` | Called when the dev middleware is being registered on the Nitro dev server.
`imports:sources` | `presets` | Called at setup allowing modules to extend sources. `imports:sources` | `presets` | Called at setup allowing modules to extend sources.
`imports:extend` | `imports` | Called at setup allowing modules to extend imports. `imports:extend` | `imports` | Called at setup allowing modules to extend imports.

View File

@ -35,14 +35,15 @@ export default defineNuxtModule({
const context = { const context = {
files: [] as Array<{ path: string, optional?: boolean }> files: [] as Array<{ path: string, optional?: boolean }>
} }
// Add default options
context.files.push({ path: resolve(runtimeDir, 'router.options'), optional: true })
for (const layer of nuxt.options._layers) { for (const layer of nuxt.options._layers) {
const path = await findPath(resolve(layer.config.srcDir, 'app/router.options')) const path = await findPath(resolve(layer.config.srcDir, 'app/router.options'))
if (path) { context.files.push({ path }) } if (path) { context.files.unshift({ path }) }
} }
// Add default options at beginning
context.files.unshift({ path: resolve(runtimeDir, 'router.options'), optional: true })
await nuxt.callHook('pages:routerOptions', context) await nuxt.callHook('pages:routerOptions', context)
return context.files return context.files
} }
@ -444,8 +445,7 @@ export default defineNuxtModule({
`const configRouterOptions = ${configRouterOptions}`, `const configRouterOptions = ${configRouterOptions}`,
'export default {', 'export default {',
'...configRouterOptions,', '...configRouterOptions,',
// We need to reverse spreading order to respect layers priority ...routerOptionsFiles.map((_, index) => `...routerOptions${index},`),
...routerOptionsFiles.map((_, index) => `...routerOptions${index},`).reverse(),
'}' '}'
].join('\n') ].join('\n')
} }

View File

@ -174,6 +174,8 @@ export interface NuxtHooks {
* Called when resolving `app/router.options` files. It allows modifying the detected router options files * Called when resolving `app/router.options` files. It allows modifying the detected router options files
* and adding new ones. * and adding new ones.
* *
* Later items in the array override earlier ones.
*
* Adding a router options file will switch on page-based routing, unless `optional` is set, in which case * Adding a router options file will switch on page-based routing, unless `optional` is set, in which case
* it will only apply when page-based routing is already enabled. * it will only apply when page-based routing is already enabled.
* @param context An object with `files` containing an array of router options files. * @param context An object with `files` containing an array of router options files.

View File

@ -1,3 +1,3 @@
export default defineNuxtRouteMiddleware((to) => { export default defineNuxtRouteMiddleware((to) => {
to.meta.override = 'This middleware should be overriden by bar' to.meta.override = 'This middleware should be overridden by bar'
}) })

View File

@ -37,8 +37,9 @@ export default defineNuxtConfig({
} }
] ]
}, },
theme: './extends/bar',
css: ['~/assets/global.css'], css: ['~/assets/global.css'],
// this produces an order of `~` > `~/extends/bar` > `~/extends/node_modules/foo`
theme: './extends/bar',
extends: [ extends: [
'./extends/node_modules/foo' './extends/node_modules/foo'
], ],