feat: add clearPayloadCache function for singleton cache man… #14358
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request: Fix locale caching in getPayload singleton
Description
Fixes an issue where the Payload singleton caches the locale from the first request and ignores subsequent locale changes, even when the
localeparameter is explicitly passed to queries in the Local API.Problem
When using Payload 3.x with Next.js and localization (e.g., next-intl), the
getPayload()function creates a singleton instance that persists across requests. This singleton maintains the locale context from its initialization and doesn't respect thelocaleparameter passed to subsequent queries likefind(),findByID(), etc.Reproduction:
locale: 'fr'Root Cause
The Payload singleton is stored at
(global as any)._payloadas a Map with key'default'. Once initialized, operations use this cached instance which may have stale locale context.Solution
Add a
clearCache()method to allow developers to manually flush the Payload singleton cache when locale changes or other scenarios require re-initialization.Changes
File:
packages/payload/src/index.tsAdd the following export after the
getPayloadfunction:File:
packages/payload/src/types/index.tsAdd type export for the new function (if needed for type definitions).
Usage Example
For developers using Payload with Next.js and i18n:
Testing
Manual Testing
Test Case Example
Breaking Changes
None. This is an additive change that introduces a new exported function.
Related Issues
Additional Notes
This fix provides a workaround for the locale caching issue. A more comprehensive solution might involve:
localeparameter is being ignored in queriesHowever,
clearPayloadCache()provides immediate relief for developers facing this issue and gives them control over cache lifecycle and can be used for other use cases than enforcing locale update