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

fix: shouldHydrate error when null object is passed #2867

Open
wants to merge 3 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/nuxt/playground/pages/null-object-store.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script lang="ts" setup>
const store = useWithNullObjectStore()
</script>

<template>
<h2>null object</h2>
<p>{{ store.foo }}</p>
</template>
12 changes: 12 additions & 0 deletions packages/nuxt/playground/stores/with-null-οbject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const useWithNullObjectStore = defineStore('with-null-object', () => {
return {
text: ref(Object.create(null)),
foo: ref('bar'),
}
})

if (import.meta.hot) {
import.meta.hot.accept(
acceptHMRUpdate(useWithNullObjectStore, import.meta.hot)
)
}
9 changes: 9 additions & 0 deletions packages/nuxt/test/nuxt.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { fileURLToPath } from 'node:url'
import { describe, it, expect } from 'vitest'
import { setup, $fetch } from '@nuxt/test-utils/e2e'
import exp from 'node:constants'

describe('works with nuxt', async () => {
await setup({
Expand Down Expand Up @@ -28,6 +29,14 @@ describe('works with nuxt', async () => {
expect(html).toContain('Count: 101')
})

it('works with null objects', async () => {
expect(async () => {
const html = await $fetch('/null-object-store')
expect(html).toContain('null object')
expect(html).toContain('bar')
}).not.toThrow()
})

it('drops state that is marked with skipHydrate', async () => {
const html = await $fetch('/skip-hydrate')
expect(html).not.toContain('I should not be serialized or hydrated')
Expand Down
7 changes: 7 additions & 0 deletions packages/pinia/__tests__/ssr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ describe('SSR', () => {
expect(store.$state).toEqual({ start: 'start' })
})

it('accepts a store with no null state', () => {
const pinia = createPinia()
pinia.state.value.a = Object.create(null)
const store = defineStore('a', {})(pinia)
expect(store.$state).toEqual({})
})

describe('Setup Store', () => {
const useStore = defineStore('main', () => {
const count = ref(0)
Expand Down
5 changes: 4 additions & 1 deletion packages/pinia/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ export function skipHydrate<T = any>(obj: T): T {
* @returns true if `obj` should be hydrated
*/
export function shouldHydrate(obj: any) {
return !isPlainObject(obj) || !obj.hasOwnProperty(skipHydrateSymbol)
return (
!isPlainObject(obj) ||
!Object.prototype.hasOwnProperty.call(obj, skipHydrateSymbol)
)
}

const { assign } = Object
Expand Down