Skip to content
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

[feat] invalidate with predicate #4636

Merged

Conversation

icalvin102
Copy link
Contributor

@icalvin102 icalvin102 commented Apr 15, 2022

Closes #4540

Problem

The current implementation of invalidate is quite limiting because the provided argument has to match the URL of the fetched resource exactly. It's not possible to invalidate resources that match a certain pattern. (Take a look at #4540 for more detailed problem description)

Solution

Extend invalidate to take a predicate function as argument (similar to Array.prototype.some).
The predicate is run for each item of dependencies and will cause a rerun of load if true is returned.

Signature:

function invalidate(href: string): Promise<void>;
function invalidate(predicate: (dep: string) => boolean): Promise<void>;

Example usage:

<script context="module">
export async function load({fetch, url}) {
  const response = await fetch(`/collections${url.search}`);
  const data = await response.json();
  
  return { props: { data }};
}
</script>
<script>
  import { invalidate } from '$app/navigation';
  const isCollectionURL = (dep) => dep.includes('/collections');
</script>

<button on:click={() => invalidate(isCollectionURL)}>Reload collection</button>

This makes the invalidate function far more useful IMO and would not break how it's currently used.

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 pnpx changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

@changeset-bot
Copy link

changeset-bot bot commented Apr 15, 2022

🦋 Changeset detected

Latest commit: 3dadac6

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 Patch

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

@icalvin102
Copy link
Contributor Author

I have included a test. However the test fails because playwright is unable to serialize the predicate function and throws a Unexpeced value error in

invalidate: (url) => page.evaluate((/** @type {string} */ url) => invalidate(url), url),

This is a problem with playwright and not with the actual feature. I don't have much experience with playright and am not sure how to solve this. It would be nice if someone could take a look at this or guide me in the right direction.

@Rich-Harris Rich-Harris merged commit b4289d5 into sveltejs:master Apr 18, 2022
@Rich-Harris
Copy link
Member

thank you!

This was referenced Apr 18, 2022
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.

Add more flexibility to invalidate
2 participants