fix: never use global context on the server side
when store is accessed and injection context is not found it falls back to global active pinia object,
although it is safe for client, on the server side there are could be many requests with their own context,
falling back to global state leads to race conditions and undefined behaviour,
this should be fixed once and for all!
it is probably a breaking change but a good one, I haven't tested this locally, I appreciate any help to improve this PR in order to make it good looking
Deploy Preview for pinia-official canceled.
| Name | Link |
|---|---|
| Latest commit | 11be687ae70b0fe6ace9bc00ee73b939ad310483 |
| Latest deploy log | https://app.netlify.com/projects/pinia-official/deploys/690a1186762ddc00079c7438 |
@posva I removed throwing error, added console.error and fallback to createPinia() instead, please check.
Perfect @ivansky ! This would be great to have at least a warning. Would also have preferred the throw error but anything is better than nothing.
[!CAUTION]
Review failed
The pull request is closed.
Walkthrough
Adds exported getActivePinia() and setActivePinia() to the Pinia public API, generalizes test console mocks to support both warn and error, and updates server-side getActivePinia() to emit a dev-mode console error when invoked outside a Pinia context on the server.
Changes
| Cohort / File(s) | Summary |
|---|---|
Public API exportspackages/pinia/src/index.ts |
Exported new functions getActivePinia() and setActivePinia(pinia). |
Root store runtime checkpackages/pinia/src/rootStore.ts |
Imported IS_CLIENT; updated getActivePinia() to emit a development-mode console error when no Pinia is injected and running offโclient (server), preserving existing fallback to activePinia. |
Test mock utilitiespackages/pinia/__tests__/vitest-mock-warn.ts |
Reworked mock helper into createMockConsoleMethod(method) to support 'warn' and 'error'; added mockConsoleError() and matcher augmentations for errored assertions. |
SSR testspackages/pinia/__tests__/ssr.spec.ts |
Imported getActivePinia/setActivePinia; integrated mockWarn and mockConsoleError; added tests asserting error/warning when getActivePinia() is called outside Pinia context. |
Sequence Diagram(s)
sequenceDiagram
participant Test as Test Env
participant API as getActivePinia()
participant Inject as Injection Context
participant Fallback as activePinia
participant Console as console.error / console.warn
Test->>API: call getActivePinia()
API->>Inject: read injected pinia
alt injected pinia exists
API->>Test: return injected pinia
else no injected pinia
alt not IS_CLIENT and __DEV__
API->>Console: emit dev-mode error (cross-request risk)
end
API->>Fallback: return activePinia (could be undefined)
API->>Test: return fallback result
end
Estimated code review effort
๐ฏ 3 (Moderate) | โฑ๏ธ ~20 minutes
- Check
getActivePinia()dev-mode server condition and message correctness. - Verify new exports in
src/index.tsmatch typings and are exported from package entrypoints. - Confirm
createMockConsoleMethodcorrectly restores console state and that new matchers behave as intended.
Possibly related PRs
- vuejs/pinia#2983 โ Appears to modify the same API (add
getActivePinia/setActivePinia), runtime server check, and related SSR tests; likely directly related.
Poem
๐ I hopped through tests at break of day,
Found warnings where the Pinia slipped away,
I mocked the noise and set the scene,
Now errors and warns are neatly seen,
A rabbit's cheer for clearer Pinia play.
Pre-merge checks and finishing touches
โ Passed checks (2 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | โ Passed | Check skipped - CodeRabbitโs high-level summary is enabled. |
| Title check | โ Passed | The title clearly summarizes the main change: adding detection of global context usage on the server side. It accurately reflects the core objective of preventing server-side fallback to global Pinia state. |
๐ Recent review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
๐ฅ Commits
Reviewing files that changed from the base of the PR and between 4bcc07a11f5915a3dc068992bb333aea0f29016c and 11be687ae70b0fe6ace9bc00ee73b939ad310483.
๐ Files selected for processing (2)
packages/pinia/__tests__/ssr.spec.ts(2 hunks)packages/pinia/src/rootStore.ts(2 hunks)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.
npm i https://pkg.pr.new/@pinia/nuxt@2983
npm i https://pkg.pr.new/pinia@2983
npm i https://pkg.pr.new/@pinia/testing@2983
commit: 4bcc07a
Codecov Report
:x: Patch coverage is 76.92308% with 3 lines in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 91.16%. Comparing base (8a65eb7) to head (4bcc07a).
| Files with missing lines | Patch % | Lines |
|---|---|---|
| packages/pinia/src/rootStore.ts | 76.92% | 3 Missing :warning: |
Additional details and impacted files
@@ Coverage Diff @@
## v3 #2983 +/- ##
==========================================
- Coverage 91.28% 91.16% -0.13%
==========================================
Files 18 18
Lines 1618 1629 +11
Branches 231 235 +4
==========================================
+ Hits 1477 1485 +8
- Misses 139 142 +3
Partials 2 2
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.