Skip to content

Commit

Permalink
fix(nuxt): allow pages:extend to enable pages module (#20806)
Browse files Browse the repository at this point in the history
  • Loading branch information
darioferderber committed May 15, 2023
1 parent ce84c9b commit ec9dcdb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions docs/2.guide/3.going-further/3.modules.md
Expand Up @@ -414,8 +414,8 @@ export default defineNuxtModule({
}
},
setup (options, nuxt) {
// Programmatically hook to the `page:extend` hook
nuxt.hook('page:extend', (pages) => {
// Programmatically hook to the `pages:extend` hook
nuxt.hook('pages:extend', (pages) => {
console.info(`Discovered ${pages.length} pages`);
})
}
Expand Down
13 changes: 9 additions & 4 deletions packages/nuxt/src/pages/module.ts
Expand Up @@ -29,7 +29,7 @@ export default defineNuxtModule({
// Disable module (and use universal router) if pages dir do not exists or user has disabled it
const isNonEmptyDir = (dir: string) => existsSync(dir) && readdirSync(dir).length
const userPreference = nuxt.options.pages
const isPagesEnabled = () => {
const isPagesEnabled = async () => {
if (typeof userPreference === 'boolean') {
return userPreference
}
Expand All @@ -39,19 +39,24 @@ export default defineNuxtModule({
if (pagesDirs.some(dir => isNonEmptyDir(dir))) {
return true
}

const pages = await resolvePagesRoutes()
await nuxt.callHook('pages:extend', pages)
if (pages.length) { return true }

return false
}
nuxt.options.pages = isPagesEnabled()
nuxt.options.pages = await isPagesEnabled()

// Restart Nuxt when pages dir is added or removed
const restartPaths = nuxt.options._layers.flatMap(layer => [
join(layer.config.srcDir, 'app/router.options.ts'),
join(layer.config.srcDir, layer.config.dir?.pages || 'pages')
])
nuxt.hooks.hook('builder:watch', (event, path) => {
nuxt.hooks.hook('builder:watch', async (event, path) => {
const fullPath = join(nuxt.options.srcDir, path)
if (restartPaths.some(path => path === fullPath || fullPath.startsWith(path + '/'))) {
const newSetting = isPagesEnabled()
const newSetting = await isPagesEnabled()
if (nuxt.options.pages !== newSetting) {
console.info('Pages', newSetting ? 'enabled' : 'disabled')
return nuxt.callHook('restart')
Expand Down

0 comments on commit ec9dcdb

Please sign in to comment.