Skip to content

Commit

Permalink
fix(nuxt): fix skipHydrate payload plugin and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
kabalage committed Oct 13, 2024
1 parent 4928109 commit 08257f9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
6 changes: 4 additions & 2 deletions packages/nuxt/__tests__/nuxt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ describe('works with nuxt', async () => {
expect(html).toContain('Count: 101')
})

it.todo('drop state that is marked with skipHydrate', async () => {
it('drops state that is marked with skipHydrate', async () => {
const html = await $fetch('/skip-hydrate')
// TODO:
console.log(html)
expect(html).not.toContain('I should not be serialized or hydrated')
expect(html).toContain('skipHydrate-wrapped state is correct')
})
})
7 changes: 6 additions & 1 deletion packages/nuxt/playground/pages/skip-hydrate.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<script lang="ts" setup>
const store = useWithSkipHydrateStore()
const skipHydrateState = computed(() => {
return store.skipped?.text === 'I should not be serialized or hydrated'
? 'skipHydrate-wrapped state is correct'
: 'skipHydrate-wrapped state is incorrect'
})
</script>

<template>
<h2>skipHydrate() test</h2>
<pre>{{ store.skipped }}</pre>
<pre>{{ skipHydrateState }}</pre>
</template>
3 changes: 3 additions & 0 deletions packages/nuxt/playground/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./.nuxt/tsconfig.json"
}
5 changes: 3 additions & 2 deletions packages/nuxt/src/runtime/payload-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import { shouldHydrate } from 'pinia'
const payloadPlugin = definePayloadPlugin(() => {
definePayloadReducer(
'skipHydrate',
(data: unknown) => !shouldHydrate(data) && undefined
// We need to return something truthy to be treated as a match
(data: unknown) => !shouldHydrate(data) && 1
)
definePayloadReviver('skipHydrate', (data: undefined) => data)
definePayloadReviver('skipHydrate', (_data: 1) => undefined)
})

export default payloadPlugin

0 comments on commit 08257f9

Please sign in to comment.