From e1419d7c33e8155237d8eaa7c46d63a9784936cb Mon Sep 17 00:00:00 2001 From: Ivan Martianov Date: Tue, 27 May 2025 11:39:02 +0200 Subject: [PATCH 1/3] fix: never use global context on the server side --- packages/pinia/src/rootStore.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pinia/src/rootStore.ts b/packages/pinia/src/rootStore.ts index 2de789f34d..0a100cf628 100644 --- a/packages/pinia/src/rootStore.ts +++ b/packages/pinia/src/rootStore.ts @@ -43,7 +43,7 @@ interface _SetActivePinia { * Get the currently active pinia if there is any. */ export const getActivePinia = () => - (hasInjectionContext() && inject(piniaSymbol)) || activePinia + (hasInjectionContext() && inject(piniaSymbol)) || (import.meta.server ? throw new Error("Cannot get active pinia as it does not find context") : activePinia) /** * Every application must own its own pinia to be able to create stores From d33bd376a624c9081e8fb343c4e2cec97d3e62b3 Mon Sep 17 00:00:00 2001 From: Ivan Martianov Date: Tue, 27 May 2025 15:44:29 +0200 Subject: [PATCH 2/3] Improve error message --- packages/pinia/src/rootStore.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/pinia/src/rootStore.ts b/packages/pinia/src/rootStore.ts index 0a100cf628..b4ce2c71a5 100644 --- a/packages/pinia/src/rootStore.ts +++ b/packages/pinia/src/rootStore.ts @@ -43,8 +43,12 @@ interface _SetActivePinia { * Get the currently active pinia if there is any. */ export const getActivePinia = () => - (hasInjectionContext() && inject(piniaSymbol)) || (import.meta.server ? throw new Error("Cannot get active pinia as it does not find context") : activePinia) - + (hasInjectionContext() && inject(piniaSymbol)) || + (import.meta.server + ? throw new Error( + 'Pinia instance not found in context and cannot fall back to global activePinia on server - this would lead to shared state between requests' + ) + : activePinia) /** * Every application must own its own pinia to be able to create stores */ From 56d62d001bcf2bb363aa2cf8a7c3899410a24448 Mon Sep 17 00:00:00 2001 From: Ivan Martianov Date: Tue, 27 May 2025 20:01:49 +0200 Subject: [PATCH 3/3] Update rootStore.ts --- packages/pinia/src/rootStore.ts | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/packages/pinia/src/rootStore.ts b/packages/pinia/src/rootStore.ts index b4ce2c71a5..fea2bcfb8c 100644 --- a/packages/pinia/src/rootStore.ts +++ b/packages/pinia/src/rootStore.ts @@ -6,6 +6,7 @@ import { InjectionKey, Ref, } from 'vue' +import { createPinia } from './createPinia' import { StateTree, PiniaCustomProperties, @@ -42,13 +43,22 @@ interface _SetActivePinia { /** * Get the currently active pinia if there is any. */ -export const getActivePinia = () => - (hasInjectionContext() && inject(piniaSymbol)) || - (import.meta.server - ? throw new Error( - 'Pinia instance not found in context and cannot fall back to global activePinia on server - this would lead to shared state between requests' - ) - : activePinia) +export const getActivePinia = () => { + const pinia = (hasInjectionContext() && inject(piniaSymbol)) + + if (pinia) return pinia + + if (import.meta.server) { + console.error( + 'Pinia instance not found in context and cannot fall back to global activePinia on server - this would lead to shared state between requests, instead it falls back to new pinia' + ) + + return createPinia() + } + + return activePinia +} + /** * Every application must own its own pinia to be able to create stores */