question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add way to not fetch data twice in `load` when provided `fetch` cannot be used

See original GitHub issue

Describe the problem

A lot of services provide SDKs used to simplify fetching data from their APIs, however a decent amount of them don’t provide an option for a custom fetch function, so when you use that SDK in load() the data will be fetched twice, both on the server and the client. A library I’m having trouble with is Ghost CMS’s SDK, and I remember having this issue with something else too but I can’t remember what it was.

Describe the proposed solution

A function passed to load could allow you to pass in a unique ID and a callback — it runs that callback, caches the result as JSON and returns it. Once the app loads on the client-side and runs load again this function could check the cache and return it. For example:

export async function load({ cache, params }) {
  const post = cache(`post_${params.slug}`, async () => {
    try {
      return await someSdk.getPost(params.slug)
    } catch {
      return null
    }
  })
  // Do other things in load
  return { status: 200, props: { post } }
}

By caching I mean store the JSON string in the document the same way the custom fetch does - not sure if cache is the right term here

Alternatives considered

Using endpoints that run the SDK functions, but it’s a bit boilerplate and loses some type-safety

Importance

nice to have

Additional Information

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
jdgamble555commented, Feb 16, 2022

@Rich-Harris - Doesn’t Vercel use Serverless functions for each api call? This would dramatically increase the speed of your website. I think SvelteKit needs a transferState function so we have more control over what we can and can’t pass. Endpoints add more complexity. Plus, when you can avoid an endpoint, trying to pass a fetch function to apollo is a pain, Firebase doesn’t have one, and we have less control of our app. I also couldn’t figure out how to set-cookies to manually override this, as it seems impossible without an endpoint. I am a big fan of endpoints, but not with code that should not need the extra step. I personally prefer the returned value being set in the script, not the fetch value. You have way more control, and you don’t need an endpoint. I think NextJs’s getServerSideProps does this right.

<script type="application/json" data-type="svelte-data" data-url="/resources">
// json
</script>

Either way, not a fan of the required endpoint in this use case.

J

0reactions
kenfoocommented, Feb 22, 2022

Thanks @jdgamble555 I just found out about the new shadow endpoints and that seems to solve my use case perfectly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Avoid Fetching Data Twice When Using React Hooks
To fetch data once on component mount you can specify an empty dependency array. I also suggest moving the questions object into local ......
Read more >
Things You Should Know When Fetching Data for React ...
Suspense is not a data fetching library. It's a method for data fetching libraries to interact with React whether the data a component...
Read more >
Fetching Data in React with useEffect - Max Rozen
we're not doing anything with the data once we fetch it; we've hardcoded the URL to fetch data from. To make this useEffect...
Read more >
Why is my fetch getting called twice? : r/reactjs - Reddit
The easiest solution is to save some state in local storage and use that to run bits of code.
Read more >
NextJS / React SSR: 21 Universal Data Fetching Patterns ...
Another important requirement for data-fetching hooks is to debounce the execution of a Query. This is to avoid unnecessary requests to the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found