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

feat(nuxt): warn when page uses a layout without <NuxtLayout> #24116

Merged
merged 16 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 11 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
4 changes: 4 additions & 0 deletions packages/nuxt/src/app/components/nuxt-layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export default defineComponent({

const done = nuxtApp.deferHydration()

if (import.meta.dev) {
nuxtApp.payload._isNuxtLayoutUsed = true
}

return () => {
const hasLayout = layout.value && layout.value in layouts
if (import.meta.dev && layout.value && !hasLayout && layout.value !== 'default') {
Expand Down
18 changes: 18 additions & 0 deletions packages/nuxt/src/app/plugins/check-if-layout-used.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { defineNuxtPlugin } from '../nuxt'
import { onNuxtReady } from '../composables/ready'

// @ts-expect-error virtual file
import layouts from '#build/layouts'

export default defineNuxtPlugin({
name: 'nuxt:checkIfLayoutUsed',
enforce: 'post',
setup (nuxtApp) {
onNuxtReady(() => {
if (!nuxtApp.payload._isNuxtLayoutUsed && Object.keys(layouts).length > 0) {
// TODO: Use logger
danielroe marked this conversation as resolved.
Show resolved Hide resolved
console.warn('[nuxt] Your project has layouts but the `<NuxtLayout />` component has not been used.')
}
})
}
})
5 changes: 5 additions & 0 deletions packages/nuxt/src/core/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ async function initNuxt (nuxt: Nuxt) {
addWebpackPlugin(() => DevOnlyPlugin.webpack({ sourcemap: !!nuxt.options.sourcemap.server || !!nuxt.options.sourcemap.client }))
}

if (nuxt.options.dev) {
// Add plugin to check if layouts are defined without NuxtLayout being instantiated
addPlugin(resolve(nuxt.options.appDir, 'plugins/check-if-layout-used'))
}

// Transform initial composable call within `<script setup>` to preserve context
if (nuxt.options.experimental.asyncContext) {
addBuildPlugin(AsyncContextInjectionPlugin(nuxt))
Expand Down