Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
fix(nuxt): don't try to override computed layouts in definePageMeta (
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-fadaei committed Jan 14, 2023
1 parent f4ba7ec commit 5b19a0d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
6 changes: 3 additions & 3 deletions packages/nuxt/src/app/plugins/router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { reactive, h } from 'vue'
import { reactive, h, isReadonly } from 'vue'
import { parseURL, stringifyParsedURL, parseQuery, stringifyQuery, withoutBase, isEqual, joinURL } from 'ufo'
import { createError } from 'h3'
import { defineNuxtPlugin, clearError, navigateTo, showError, useRuntimeConfig, useState } from '..'
Expand Down Expand Up @@ -222,8 +222,8 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>((nuxtApp) => {
nuxtApp.hooks.hookOnce('app:created', async () => {
router.beforeEach(async (to, from) => {
to.meta = reactive(to.meta || {})
if (nuxtApp.isHydrating) {
to.meta.layout = initialLayout.value ?? to.meta.layout
if (nuxtApp.isHydrating && initialLayout.value && !isReadonly(to.meta.layout)) {
to.meta.layout = initialLayout.value
}
nuxtApp._processingMiddleware = true

Expand Down
6 changes: 3 additions & 3 deletions packages/nuxt/src/pages/runtime/router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computed, reactive, shallowRef } from 'vue'
import { computed, isReadonly, reactive, shallowRef } from 'vue'
import type {
NavigationGuard,
RouteLocation
Expand Down Expand Up @@ -119,8 +119,8 @@ export default defineNuxtPlugin(async (nuxtApp) => {
const initialLayout = useState('_layout')
router.beforeEach(async (to, from) => {
to.meta = reactive(to.meta)
if (nuxtApp.isHydrating) {
to.meta.layout = initialLayout.value ?? to.meta.layout
if (nuxtApp.isHydrating && initialLayout.value && !isReadonly(to.meta.layout)) {
to.meta.layout = initialLayout.value
}
nuxtApp._processingMiddleware = true

Expand Down
10 changes: 10 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,16 @@ describe('layouts', () => {
expect(html).toContain('Custom Layout:')
await expectNoClientErrors('/with-dynamic-layout')
})
it('should work with a computed layout', async () => {
const html = await $fetch('/with-computed-layout')

// Snapshot
// expect(html).toMatchInlineSnapshot()

expect(html).toContain('with-computed-layout')
expect(html).toContain('Custom Layout')
await expectNoClientErrors('/with-computed-layout')
})
it('should allow passing custom props to a layout', async () => {
const html = await $fetch('/layouts/with-props')
expect(html).toContain('some prop was passed')
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/basic/pages/with-computed-layout.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script setup>
definePageMeta({
layout: computed(() => 'custom')
})
</script>

<template>
<div>
<div>with-computed-layout.vue</div>
</div>
</template>

0 comments on commit 5b19a0d

Please sign in to comment.