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(nuxt): avoid calling runWithContext #334

Merged
merged 4 commits into from
Sep 16, 2024
Merged
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
41 changes: 1 addition & 40 deletions playground/app.vue
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
<script setup lang="ts">
const cookieStore = useCookieStore()
const localStorageStore = useLocalStorageStore()
const sessionStorageStore = useSessionStorageStore()
</script>

<template>
<main>
<label for="cookie">{{ cookieStore.$id }}</label>
<input id="cookie" v-model="cookieStore.username">
<label for="local">{{ localStorageStore.$id }}</label>
<input id="locale" v-model="localStorageStore.username">
<label for="session">{{ sessionStorageStore.$id }}</label>
<input id="session" v-model="sessionStorageStore.username">
</main>
<NuxtPage />
</template>

<style>
Expand All @@ -22,30 +9,4 @@ body {
background-color: #181825;
color: #cdd6f4;
}
main {
max-width: 600px;
padding: 10px;
display: grid;
grid-template-columns: auto 1fr;
gap: 10px;
}
label {
margin: auto 0 auto auto;
}
input {
border-radius: 5px;
border-width: 2px;
border-style: solid;
border-color: #585b70;
background-color: transparent;
color: inherit;
font-size: inherit;
}
input:focus-visible {
outline: #cba6f7 2px solid;
}
</style>
7 changes: 7 additions & 0 deletions playground/composables/token-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const useTokenStore = defineStore('token', () => {
const token = ref('')

return { token }
}, {
persist: true,
})
11 changes: 11 additions & 0 deletions playground/middleware/login.global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default defineNuxtRouteMiddleware((to) => {
const tokenStore = useTokenStore()

if (!tokenStore.token && to.path !== '/login') {
return navigateTo('/login')
}

if (tokenStore.token && to.path === '/login') {
return navigateTo('/page')
}
})
44 changes: 44 additions & 0 deletions playground/pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<script setup lang="ts">
const cookieStore = useCookieStore()
const localStorageStore = useLocalStorageStore()
const sessionStorageStore = useSessionStorageStore()
</script>

<template>
<main>
<label for="cookie">{{ cookieStore.$id }}</label>
<input id="cookie" v-model="cookieStore.username">
<label for="local">{{ localStorageStore.$id }}</label>
<input id="locale" v-model="localStorageStore.username">
<label for="session">{{ sessionStorageStore.$id }}</label>
<input id="session" v-model="sessionStorageStore.username">
</main>
</template>

<style scoped>
main {
max-width: 600px;
padding: 10px;
display: grid;
grid-template-columns: auto 1fr;
gap: 10px;
}
label {
margin: auto 0 auto auto;
}
input {
border-radius: 5px;
border-width: 2px;
border-style: solid;
border-color: #585b70;
background-color: transparent;
color: inherit;
font-size: inherit;
}
input:focus-visible {
outline: #cba6f7 2px solid;
}
</style>
17 changes: 17 additions & 0 deletions playground/pages/login.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script setup lang="ts">
const tokenStore = useTokenStore()
async function login() {
tokenStore.token = 'loginToken'
await navigateTo('/page')
}
</script>

<template>
<main>
<h1>Login page</h1>
<button @click="login">
Login
</button>
</main>
</template>
14 changes: 14 additions & 0 deletions playground/pages/page.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script setup lang="ts">
async function goToLogin() {
await navigateTo('/login')
}
</script>

<template>
<main>
<h1>home page</h1>
<button @click="goToLogin">
To Login Page
</button>
</main>
</template>
9 changes: 4 additions & 5 deletions src/runtime/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ function persistState(
export function createPersistence(
context: PiniaPluginContext,
optionsParser: (p: PersistenceOptions) => Persistence,
runWithContext: (fn: () => void) => void = fn => fn(),
) {
const { pinia, store, options: { persist } } = context

Expand All @@ -99,21 +98,21 @@ export function createPersistence(

store.$hydrate = ({ runHooks = true } = {}) => {
persistences.forEach((p) => {
runWithContext(() => hydrateStore(store, p, context, runHooks))
hydrateStore(store, p, context, runHooks)
})
}

store.$persist = () => {
persistences.forEach((p) => {
runWithContext(() => persistState(store.$state, p))
persistState(store.$state, p)
})
}

persistences.forEach((p) => {
runWithContext(() => hydrateStore(store, p, context))
hydrateStore(store, p, context)

store.$subscribe(
(_mutation, state) => runWithContext(() => persistState(state, p)),
(_mutation, state) => persistState(state, p),
{ detached: true },
)
})
Expand Down
5 changes: 2 additions & 3 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { Pinia, PiniaPluginContext } from 'pinia'
import { defineNuxtPlugin, useNuxtApp, useRuntimeConfig } from '#app'
import { defineNuxtPlugin, useRuntimeConfig } from '#app'
import { destr } from 'destr'
import { createPersistence } from './core'
import { storages } from './storages'

function piniaPlugin(context: PiniaPluginContext) {
const nuxtApp = useNuxtApp()
const config = useRuntimeConfig()
const options = config.public.piniaPluginPersistedstate

Expand All @@ -27,7 +26,7 @@ function piniaPlugin(context: PiniaPluginContext) {
afterHydrate: p.afterHydrate,
pick: p.pick,
omit: p.omit,
}), nuxtApp.runWithContext)
}))
}

export default defineNuxtPlugin(({ $pinia }) => {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/storages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PublicRuntimeConfig } from 'nuxt/schema'
import type { PublicRuntimeConfig } from '@nuxt/schema'
import type { StorageLike } from '../types'
import { useCookie } from '#app'

Expand Down