Skip to content

feat: Add dedupe functions #13758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

feat: Add dedupe functions #13758

wants to merge 4 commits into from

Conversation

IAmJSD
Copy link

@IAmJSD IAmJSD commented Apr 30, 2025

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 name dedupe 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.

export const getUserbyToken = async (token: string) => {
    // TODO: Whatever here
};

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:

  • What if I want to quickly use a function from my layout and page? I can use hooks, but this makes this much more complex, especially if its a function type where I might want multiple calls with different arguments (which this handles).
  • What if this is a sub-request? The locals are not persisted, so I have to make multiple calls even though I already fetched this data.

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:

export const getUserbyToken = dedupe(async (token: string) => {
    // TODO: Whatever here
});

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:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

Copy link

changeset-bot bot commented Apr 30, 2025

🦋 Changeset detected

Latest commit: dd2188f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@svelte-docs-bot
Copy link

@IAmJSD IAmJSD changed the title Add dedupe funcitons feat: Add dedupe functions Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant