Share/Inject functions and object instances in universal loaders without globals/singletons
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 20.8k
- Forks
- 2.3k
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 156
Description
Describe the problem
When working with universal loaders (+page.js and +layout.js), there's (seemingly) no way to pass around functions or object instances without resorting to globals or singletons.
On the server-side, you can initialize objects in hooks.server.js and then pass them to server loaders via event.locals (which has it's own oddities of naming/documentation), but there doesn't seem to be an analogous way to pass these kinds of things in a client or universal context.
In components I can use stores/context, or just drill props, but there just doesn't seem to be a way to easily share complex or expensive to initialize objects in client/universal loaders without using globals/singletons.... which comes with all of the negatives of using globals.
Describe the proposed solution
I'd like to, generally, have a way to define a init function (or constructor?) or some way to define dependencies in loaders that isn't just importing globals.
If that's not something that is to be considered, then I'd like to at least see a way for client/universal loaders to share complex data that's similar to the server loaders capabilities.
Alternatives considered
Just using globals/singletons... but that complicates testing and comes with all of the drawbacks of globals/singletons.
Importance
would make my life easier
Additional Information
This may be similar to #6714 or #7107... but from a different functional perspective.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the universal loader entry points named in the report (+page.js and +layout.js), then compare the server-side path in hooks.server.js and event.locals with related issues #6714 and #7107. Because this is a general dependency-sharing design request rather than a specified edit or test, completion criteria would need to be defined before implementation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- backend, frontend, web-dev
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 22/100