Skip to content

Commit 4e9f34b

Browse files
committedDec 3, 2024··
fix(runtime-utils): suppress errors setting unneeded properties
resolves #744
1 parent f52dd26 commit 4e9f34b

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script setup lang="ts">
2+
const nuxtApp = useNuxtApp()
3+
</script>
4+
5+
<template>
6+
<span>{{ nuxtApp.isHydrating }}</span>
7+
</template>

‎examples/app-vitest-full/tests/nuxt/mount-suspended.spec.ts

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import ExportDefaultWithRenderComponent from '~/components/ExportDefaultWithRend
1515
import ExportDefaultReturnsRenderComponent from '~/components/ExportDefaultReturnsRenderComponent.vue'
1616
import OptionsApiPage from '~/pages/other/options-api.vue'
1717
import ComponentWithReservedProp from '~/components/ComponentWithReservedProp.vue'
18+
import ComponentWithReservedState from '~/components/ComponentWithReservedState.vue'
1819

1920
import { BoundAttrs } from '#components'
2021
import DirectiveComponent from '~/components/DirectiveComponent.vue'
@@ -139,6 +140,12 @@ describe('mountSuspended', () => {
139140
expect(span.text()).toBe('500')
140141
})
141142

143+
it('can handle reserved words in setup state', async () => {
144+
const comp = await mountSuspended(ComponentWithReservedState)
145+
const span = comp.find('span')
146+
expect(span.text()).toBe('false')
147+
})
148+
142149
describe('Options API', () => {
143150
beforeEach(() => {
144151
vi.spyOn(console, 'error').mockImplementation((message) => {

‎src/runtime-utils/mount.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,17 @@ export async function mountSuspended<T>(
127127
}
128128
}
129129
for (const key in setupState || {}) {
130-
renderContext[key] = isReadonly(setupState[key]) ? unref(setupState[key]) : setupState[key]
130+
const warn = console.warn
131+
console.warn = () => {}
132+
try {
133+
renderContext[key] = isReadonly(setupState[key]) ? unref(setupState[key]) : setupState[key]
134+
}
135+
catch {
136+
// ignore errors setting properties that are not exposed to template
137+
}
138+
finally {
139+
console.warn = warn
140+
}
131141
if (key === 'props') {
132142
renderContext[key] = cloneProps(renderContext[key] as Record<string, unknown>)
133143
}

‎src/runtime-utils/render.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,17 @@ export async function renderSuspended<T>(component: T, options?: RenderOptions<T
144144
}
145145
}
146146
for (const key in setupState || {}) {
147-
renderContext[key] = isReadonly(setupState[key]) ? unref(setupState[key]) : setupState[key]
147+
const warn = console.warn
148+
console.warn = () => {}
149+
try {
150+
renderContext[key] = isReadonly(setupState[key]) ? unref(setupState[key]) : setupState[key]
151+
}
152+
catch {
153+
// ignore errors setting properties that are not exposed to template
154+
}
155+
finally {
156+
console.warn = warn
157+
}
148158
if (key === 'props') {
149159
renderContext[key] = cloneProps(renderContext[key] as Record<string, unknown>)
150160
}

0 commit comments

Comments
 (0)
Please sign in to comment.