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.
This adds the following functions (they behave like cache in React and across sub-requests using the framework fetch function):
dedupe
- Creates a deduplicated function. This means that within a request, if multiple calls are made with the same arguments, the underlying function will only be called once and the result will be cached and returned for all subsequent calls.getUnderlyingDedupeFunction
- Gets the underlying function that was turned into a proxy.There is also
DedupeCache
to purge function calls from the cache. It uses the namededupe
since whilst the functionality is well received in React server components, the name has caused some confusion. I can't remember the origin of this name, but I think it was one of Theo's streams (?), and its the name I personally like the best.Right now, if you have a function that is used in multiple places in Svelte, there is a lot of complexity in getting type safe shared values that can work across multiple requests. For example, there are hooks, but these require pre-knowledge of how you would like to execute your actions data flow, separating the data fetching layer from the data handling itself. Additionally, this locals object gets killed off when you make a sub-request using the internal fetch client. But what if you want to persist your cache for the whole web request lifecycle?
Say for example I have a
getUserByToken
function. The contents of this function are not super important, but it takes in a token and returns a user.So now I have a function I can share across my codebases API, pages, and layouts. This is a useful abstraction, but there are a few ways this can become problematic:
The
dedupe
function fixes all of this by creating a per-request function cache and then shares said cache with sub-requests. Using this function is simple, you simply wrap the function:The type of the function will be identical and it will be functionally identical to use, except if called with the same arguments in a (sub-)request, it will return the same results. This allows a simple abstraction for a issue that is complex to solve right now in SvelteKit.
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. Changesets that add features should beminor
and those that fix bugs should bepatch
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.Edits