Skip to content

fix: never use global context on the server side #2983

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

Open
wants to merge 3 commits into
base: v3
Choose a base branch
from
Open
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
18 changes: 16 additions & 2 deletions packages/pinia/src/rootStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
InjectionKey,
Ref,
} from 'vue'
import { createPinia } from './createPinia'
import {
StateTree,
PiniaCustomProperties,
Expand Down Expand Up @@ -42,8 +43,21 @@ interface _SetActivePinia {
/**
* Get the currently active pinia if there is any.
*/
export const getActivePinia = () =>
(hasInjectionContext() && inject(piniaSymbol)) || 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
Expand Down