feat(nuxt): add dev warnings when setPageLayout is used incorrectly (#8464)

This commit is contained in:
Daniel Roe 2022-10-25 14:25:49 +02:00 committed by GitHub
parent 12c8949af9
commit 91eab1b312
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -119,9 +119,15 @@ export const abortNavigation = (err?: string | Partial<NuxtError>) => {
export const setPageLayout = (layout: string) => { export const setPageLayout = (layout: string) => {
if (process.server) { if (process.server) {
if (process.dev && getCurrentInstance() && useState('_layout').value !== layout) {
console.warn('[warn] [nuxt] `setPageLayout` should not be called to change the layout on the server within a component as this will cause hydration errors.')
}
useState('_layout').value = layout useState('_layout').value = layout
} }
const nuxtApp = useNuxtApp() const nuxtApp = useNuxtApp()
if (process.dev && nuxtApp.isHydrating && useState('_layout').value !== layout) {
console.warn('[warn] [nuxt] `setPageLayout` should not be called to change the layout during hydration as this will cause hydration errors.')
}
const inMiddleware = isProcessingMiddleware() const inMiddleware = isProcessingMiddleware()
if (inMiddleware || process.server || nuxtApp.isHydrating) { if (inMiddleware || process.server || nuxtApp.isHydrating) {
const unsubscribe = useRouter().beforeResolve((to) => { const unsubscribe = useRouter().beforeResolve((to) => {