sveltejs / sveltejs/kit

Function `isInvalidating(url)` for `load`

Open
#6,495 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the problem

I asked this question elsewhere, and got an interesting answer(which I added as a proposed solution):

@icalvin102 I have a general question about depends and invalidate.

E.g. such code

/** @type {import('./$types').PageLoad} */
export async function load({ fetch }) {
  const url = `https://cms.example.com/articles.json`;
  const url2 = `https://cms.example.com/articles2.json`;
  const response = await fetch(url);
  const response2 = await fetch(url2);
 
  return {
    articles: await response.json(),
    articles2: await response2.json()
  };

And then invalidate:

invalidate((url) => url===`https://cms.example.com/articles.json`)`

Question: does it execute the entire load after invalidate?
Or does it execute the entire load, but bypassing the await fetch(url2)?

Because it is clearly stated in invalidate that only url is out of date.
From this it follows that url2 is up-to-date, so there is no need to perform await fetch(url2).
Is this how it works?
In this situation, does await fetch(url2) return the data from the cache, instead of executing itself?

It seems to me that this would make sense.
Just not sure where such a cache would be stored.


Oh, and it's hard for custom depends to handle cache.
So that you would have to somehow bind, for example, the api.client.get('/foo') function to the id used in depends() to be able to reach the cache that way.
Surely that wouldn't be so easy anymore?

Originally posted by @lukaszpolowczyk in https://github.com/sveltejs/kit/issues/6489#issuecomment-1233399585

Describe the proposed solution

@lukaszpolowczyk invalidate causes the entire load function to rerun. I does not perform any magic inside of the load function.

Automatic caching of individual resources in load would be really difficult because some resources may depend on each other.

However giving the user a way to determine which resource should be fetched again would theoretically be possible if load would provide something like isInvalidating(url)

export async function load({ fetch, isInvalidating }) {
  const url = `https://cms.example.com/articles.json`;
  const url2 = `https://cms.example.com/articles2.json`;
  let response, response2;
  
  if(isInvalidating(url)) {
    response = await fetch(url);
    cache.set(url, response);
  } else {
    response = cache.get(url);
  }
  
  if(isInvalidating(url2)) {
    response2 = await fetch(url);
    cache.set(url2, response2);
  } else {
    response2 = cache.get(url2);
  }
 
  return {
    articles: await response.json(),
    articles2: await response2.json()
  };
}

But this feature does currently not exist. Might be worth a feature request.

Originally posted by @icalvin102 in https://github.com/sveltejs/kit/issues/6489#issuecomment-1233484816

Alternatives considered

No response

Importance

nice to have

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the load and invalidate behavior described in the issue, especially how custom depends values are handled. Define the intended isInvalidating(url) API and its cache semantics, including dependent resources and response reuse. Done means the behavior and implementation scope are settled and covered by appropriate tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.