Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nuxt): ensure later items in pages:routerOptions hook override earlier ones #25509

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/2.guide/3.going-further/8.custom-routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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.
Expand Down
2 changes: 1 addition & 1 deletion docs/3.api/6.advanced/1.hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Hook | Arguments | Description
`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.
`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.
`imports:sources` | `presets` | Called at setup allowing modules to extend sources.
`imports:extend` | `imports` | Called at setup allowing modules to extend imports.
Expand Down
10 changes: 5 additions & 5 deletions packages/nuxt/src/pages/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ export default defineNuxtModule({
const context = {
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) {
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)
return context.files
}
Expand Down Expand Up @@ -444,8 +445,7 @@ export default defineNuxtModule({
`const configRouterOptions = ${configRouterOptions}`,
'export default {',
'...configRouterOptions,',
// We need to reverse spreading order to respect layers priority
...routerOptionsFiles.map((_, index) => `...routerOptions${index},`).reverse(),
...routerOptionsFiles.map((_, index) => `...routerOptions${index},`),
'}'
].join('\n')
}
Expand Down
2 changes: 2 additions & 0 deletions packages/schema/src/types/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ export interface NuxtHooks {
* Called when resolving `app/router.options` files. It allows modifying the detected router options files
* 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
* it will only apply when page-based routing is already enabled.
* @param context An object with `files` containing an array of router options files.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion test/fixtures/basic/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ export default defineNuxtConfig({
}
]
},
theme: './extends/bar',
css: ['~/assets/global.css'],
// this produces an order of `~` > `~/extends/bar` > `~/extends/node_modules/foo`
theme: './extends/bar',
extends: [
'./extends/node_modules/foo'
],
Expand Down