Repository navigation
Description
Problem to Solve
Currently, there is no built-in way to access client-specific request context throughout the application lifecycle. This makes implementing features that depend on per-request data, such as localization, user preferences, or configurations difficult.
For example, to implement translations, the application needs to determine the appropriate locale for each request. This now needs to be baked into the parameters received by the endpoint method.
Proposal
Make it possible to configure a client context that is reflected in all requests to the server. The context should be possible to update during the client's lifetime.
The context should have support for multiple values.
The framework should be agnostic to the used transport protocol when the context is communicated.
If more advanced contexts are to be supported, using Serverpod Models for these would be beneficial. These already provide great serialization support and would make the contexts typesafe by default.
Use Case
Communicate device language settings, user preferences or other configuration.
Alternatives
N/A
Additional context
This has been brought up several times in discussions:
- Does Serverpod support localization for email and web templates used for authentication, verrification, resets etc #3032
- Using headers to pass client-side information, such as device language, to the server. #3194
HTTP
For HTTP requests, the standard way to transfer request contexts is by using X-header fields. If that approached is used, special care might need to be taken into consideration for streams.
Since we automatically manage the WebSocket connection, and the WebSocket connection is the only "HTTP" request for the stream, context information would never be updated if all streams are not closed.
When the context for the client is updated, any open stream should also have its context updated.
How experienced are you with this library?
Expert - Experienced and comfortable with using this library in complex projects
Are you interested in working on a PR for this?
- I want to work on this
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsPlanned 🔭
Activity
christerswahn commentedon Feb 24, 2025
Would it be worth while to avoid the name
contextfor this, opting for something likeclient configurationmaybe?contextis already used in several circumstances.SandPod commentedon Feb 24, 2025
@christerswahn - Yes, that sounds like a good suggestion! The feature does not need to use
contextas the implementation name.x-api-keyin serverpodEndpoint? #4892 as a duplicate of this issue