Skip to content

[feat] custom load dependencies#4536

Merged
Rich-Harris merged 7 commits intosveltejs:masterfrom
icalvin102:feature/custom-load-dependencies
Apr 14, 2022
Merged

[feat] custom load dependencies#4536
Rich-Harris merged 7 commits intosveltejs:masterfrom
icalvin102:feature/custom-load-dependencies

Conversation

@icalvin102
Copy link
Contributor

Closes #1684

Problem

The invalidate function only re-runs load if fetch is used. This is not possible in cases where an APIClient does not allow for customization of the fetching process or when using non REST APIs like GraphQL.

Solution

Add a dependencies array to the LoadOutput (suggested by @Rich-Harris in #1709).
The dependencies will be registered the in the same way fetch does it. This allows not only to invalidate real resources but entire pages or groups of pages.

Example Usage

<script context="module">
import { api } from '$lib/myapi';
export async function load({params}){
    const item = await api.getItem(params.id);
    return { 
        props: { item },
        dependencies: [`/item-${params.id}`, 'custom:item-page']
    }
}
</script>

<script>
import Item from '$lib/Item.svelte';
import { invalidate } from '$app/navigation';
export let item;
</script>

<Item {item} />
<button on:click={()=> invalidate('custom:item-page') }>Reload Page</button>
<button on:click={()=> invalidate(`/item-${item.id}`) }>Reload Item</button>

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 6, 2022

🦋 Changeset detected

Latest commit: 652f432

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 icalvin102 changed the title Feature/custom load dependencies [feat] custom load dependencies Apr 6, 2022
@dreitzner
Copy link
Contributor

dreitzner commented Apr 13, 2022

@icalvin102 just a quick question....
Is this problem not solved by page endpoints?
To the best of my understanding this should generate a load function on build.
By moving the $lib/myapi integration to the endpoint there should be no trouble on calling the load again 😉

@icalvin102
Copy link
Contributor Author

@dreitzner page endpoints will only work if your API is written in sveltekit AFAIK.
Page endpoints wont work in SPA mode and/or when using third party APIs that rely on browser state like cookies that are not accessible from the sveltekit server.

@Rich-Harris Rich-Harris merged commit 86ef2e2 into sveltejs:master Apr 14, 2022
@Rich-Harris
Copy link
Member

thank you!

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.

(re)trigger the load function

3 participants

Comments