Skip to content

Commit

Permalink
fix(nuxt): prioritise later items in pages:routerOptions hook (#25509)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored and manniL committed Feb 18, 2024
1 parent c89d267 commit 2ff972d
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 9 deletions.
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 @@ -39,8 +39,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

0 comments on commit 2ff972d

Please sign in to comment.