chore(nuxt): warn if NuxtPage is not used when pages enabled (#25490)

This commit is contained in:
Nozomu Ikuta 2024-01-30 01:52:03 +09:00 committed by GitHub
parent 407fde6765
commit 0d91e52211
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 39 additions and 0 deletions

View File

@ -70,6 +70,11 @@ export default defineNuxtModule({
}
nuxt.options.pages = await isPagesEnabled()
if (nuxt.options.dev && nuxt.options.pages) {
// Add plugin to check if pages are enabled without NuxtPage being instantiated
addPlugin(resolve(runtimeDir, 'plugins/check-if-page-unused'))
}
nuxt.hook('app:templates', async (app) => {
app.pages = await resolvePagesRoutes()
await nuxt.callHook('pages:extend', app.pages)

View File

@ -63,6 +63,10 @@ export default defineComponent({
})
}
if (import.meta.dev) {
nuxtApp._isNuxtPageUsed = true
}
return () => {
return h(RouterView, { name: props.name, route: props.route, ...attrs }, {
default: (routeProps: RouterViewSlotProps) => {

View File

@ -0,0 +1,30 @@
import { nextTick } from 'vue'
import { defineNuxtPlugin } from '#app/nuxt'
import { onNuxtReady } from '#app/composables/ready'
import { useError } from '#app/composables/error'
export default defineNuxtPlugin({
name: 'nuxt:checkIfPageUnused',
setup (nuxtApp) {
const error = useError()
function checkIfPageUnused () {
if (!error.value && !nuxtApp._isNuxtPageUsed) {
console.warn(
'[nuxt] Your project has pages but the `<NuxtPage />` component has not been used.'
+ ' You might be using the `<RouterView />` component instead, which will not work correctly in Nuxt.'
+ ' You can set `pages: false` in `nuxt.config` if you do not wish to use the Nuxt `vue-router` integration.'
)
}
}
if (import.meta.server) {
nuxtApp.hook('app:rendered', () => { nextTick(checkIfPageUnused) })
} else {
onNuxtReady(checkIfPageUnused)
}
},
env: {
islands: false
}
})