-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nuxt): do not serialize skipHydrate properties
Co-authored-by: Eduardo San Martin Morote <[email protected]>
- Loading branch information
Showing
12 changed files
with
99 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,3 @@ | ||
<script lang="ts" setup> | ||
// import {useCounter }from '~/stores/counter' | ||
const counter = useCounter() | ||
useTestStore() | ||
useSomeStoreStore() | ||
// await useAsyncData('counter', () => counter.asyncIncrement().then(() => true)) | ||
if (import.meta.server) { | ||
counter.increment() | ||
} | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<p>Count: {{ counter.$state.count }}</p> | ||
<button @click="counter.increment()">+</button> | ||
</div> | ||
<NuxtPage /> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<script lang="ts" setup> | ||
// import {useCounter }from '~/stores/counter' | ||
const counter = useCounter() | ||
useTestStore() | ||
useSomeStoreStore() | ||
// await useAsyncData('counter', () => counter.asyncIncrement().then(() => true)) | ||
if (import.meta.server) { | ||
counter.increment() | ||
} | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<p>Count: {{ counter.$state.count }}</p> | ||
<button @click="counter.increment()">+</button> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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> | ||
<p>{{ skipHydrateState }}</p> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { skipHydrate } from 'pinia' | ||
|
||
export const useWithSkipHydrateStore = defineStore('with-skip-hydrate', () => { | ||
const skipped = skipHydrate( | ||
ref({ | ||
text: 'I should not be serialized or hydrated', | ||
}) | ||
) | ||
return { skipped } | ||
}) | ||
|
||
if (import.meta.hot) { | ||
import.meta.hot.accept( | ||
acceptHMRUpdate(useWithSkipHydrateStore, import.meta.hot) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "./.nuxt/tsconfig.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { | ||
definePayloadPlugin, | ||
definePayloadReducer, | ||
definePayloadReviver, | ||
} from '#imports' | ||
import { shouldHydrate } from 'pinia' | ||
|
||
/** | ||
* Removes properties marked with `skipHydrate()` to avoid sending unused data to the client. | ||
*/ | ||
const payloadPlugin = definePayloadPlugin(() => { | ||
definePayloadReducer( | ||
'skipHydrate', | ||
// We need to return something truthy to be treated as a match | ||
(data: unknown) => !shouldHydrate(data) && 1 | ||
) | ||
definePayloadReviver('skipHydrate', (_data: 1) => undefined) | ||
}) | ||
|
||
export default payloadPlugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters