[feat] Added more general invalidate with uses(resource)#1709
[feat] Added more general invalidate with uses(resource)#1709icalvin102 wants to merge 12 commits intosveltejs:masterfrom
Conversation
🦋 Changeset detectedLatest commit: c551006 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
|
Thank you! Do we need the second argument? I feel like At the same time I can't help but wonder if this issue reveals a more significant gap. Ideally you'd want |
packages/kit/test/apps/basics/src/routes/load/change-detection/__layout.svelte
Outdated
Show resolved
Hide resolved
|
@Rich-Harris thanks for the review. I think the second argument (or alternatively a dedicated function to invalidate dependencies that where registered with As A generic invalidation key could be used to not only identify a single resource but a group of resources or a hole route/load function as well. This can be helpful in cases where the caller of I have no idea how to solve the |
documentation/docs/05-modules.md
Outdated
| - `keepfocus` (boolean, default `false`) If `true`, the currently focused element will retain focus after navigation. Otherwise, focus will be reset to the body | ||
| - `state` (object, default `{}`) The state of the new/updated history entry | ||
| - `invalidate(href)` causes any `load` functions belonging to the currently active page to re-run if they `fetch` the resource in question. It returns a `Promise` that resolves when the page is subsequently updated. | ||
| - `invalidate(resource, custom)` causes any `load` functions belonging to the currently active page to re-run if they `fetch` or `use` the resource in question. If `custom` is `false` (default) `resource` is a URL that was requested with `fetch`. If `true` resource is a custom identifier registered with `uses(resource)` inside `load`. `invalidate` returns a `Promise` that resolves when the page is subsequently updated. |
There was a problem hiding this comment.
would it be accurate to change it like this?
"any load functions belonging to the currently active page" -> "the load functions belonging to the currently active page and layouts"
|
I don't love the two API parameters either. We could create two methods like |
They don't. The invalidation key is an arbitrary string that just happens to be a valid URI in the |
@Rich-Harris that's true the actual invalidation key is just a string. Sorry that I did not explain further what I meant with "
Example Consider following example where We have a If we now navigate to That is the reason why I added the second argument. I don't like the second argument either but without it the One solution would be to remove the conversion step from |
|
I have a question. Calling Is this supposed to be happening? Note: I'm new to Svelte(Kit) and I'm trying to do exactly what this PR allows but I can't seem to do that with the current |
|
@ejdrien the original |
|
I removed the URL expansion from |
|
✔️ Deploy Preview for kit-demo canceled. 🔨 Explore the source changes: c551006 🔍 Inspect the deploy log: https://app.netlify.com/sites/kit-demo/deploys/61e9d6a3a8490700085076e4 |
|
Finally getting back to this — have updated it such that it's in a mergable state. I'm still a little iffy about adding this API, beyond the usual reticence about adding API surface area (which is easy to add but incredibly hard to remove) because to me it's indicative of a deeper problem. We don't really want to encourage non- There's a proposal in #2979 that would make it easier to create clients that used If we do end up needing a way to invalidate resources outside of -export async function load({ params, uses }) {
- uses(`/dependencies/${params.id}`);
+export async function load({ params }) {
const props = await client.get(params);
return {
+ dependencies: [`/dependencies/${params.id}`],
props
};
} |
|
@Rich-Harris thanks for your response. I really like the idea of returning the I'd imagine the implementation would be as simple as adding the following code to master/packages/kit/src/runtime/client/renderer.js#L683 node.loaded = normalize(loaded);
if (node.loaded.stuff) node.stuff = node.loaded.stuff;
+if (node.loaded.dependencies) {
+ node.loaded.dependencies.forEach((dep) => {
+ const { href } = new URL(dep, url);
+ node.uses.dependencies.add(href);
+ });
+}Prefixing the custom dependencies with I will make a new PR with your proposed solution. This PR can be closed IMO. Regarding the proposal in #2979 : I would really like this to work but I think it has some major issues in it's current state. I'll comment on that in the issue itself. |
Fixes #1684
Problem
The
invalidatefunction only re-runsloadif fetch is used. This is suitable for many case but it does not work with apiClientWrapper libraries or other forms of loading data that do not utilize thefetchoption ofload.Solution
Introduce a new
uses(resource)function as option ofload.usesregisters a arbitrary string that can be used as an (resource) identifier in theinvalidatefunction to trigger a re-run of allload's with a matching registered identifier.invalidatestill defaults to reload URLs but iftrueis given as a second argument a custom resource is invalidated.Example Usage
Before submitting the PR, please make sure you do the following
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpx changesetand following the prompts