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.

Support `populateCache` as a function

See original GitHub issue

We added the populateCache option (as well as other options for optimistic UI) recently, a normal use case looks like the following:

mutate(addTodo(todo), {
  optimisticData: [...todos, todo],
  populateCache: true,
  rollbackOnError: true,
  revalidate: false,
})

Say that the addTodo calls the POST /api/todos endpoint, which returns the full, updated list.

Transform Before Populating the Cache

However, it’s also common that the POST /api/todos endpoint only returns the new added item, not the full list. So naturally ​the next step is to support populateCache as a synchronous function that transforms the returned data before writing back to the cache:

mutate(addTodo(todo), {
 ​optimisticData: [...todos, todo],
 ​populateCache: addedTodo => [...todos, addedTodo],
 ​rollbackOnError: true,
 ​revalidate: true,
})

Note that revalidate: true is needed now.

Also populateCache is synchronous only to avoid possible race conditions.

Opt-out

With this design, if an error is throw when calling populateCache, we can skip populating the cache for this specific mutation.

This enables a way to conditionally populating the cache.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
shudingcommented, Feb 6, 2022

I think then the populateCache option needs to accept the current data as well, something like:

mutate(addTodo(todo), {
  ​optimisticData: todos => [...todos, todo],
  ​populateCache: (addedTodo, todos) => [...todos, addedTodo],
  ​rollbackOnError: true,
  ​revalidate: true,
})

So this can be wrapped in an event callback which is totally pure and doesn’t depend on the hook state.

1reaction
ludovic-montelcommented, Feb 9, 2022

Thanks for your answers, I just added a feature request in the Discussion of the repository (https://github.com/vercel/swr/discussions/1850)

Read more comments on GitHub >

github_iconTop Results From Across the Web

PopulateCache policy | Apigee Edge
Configures how cached values should be written at runtime. The Populate Cache policy is designed for writing entries in a short-term general-purpose cache....
Read more >
PopulateCache policy | Apigee X - Google Cloud
The Populate Cache policy is designed for writing entries in a short-term general-purpose cache. It's used in conjunction with the Lookup Cache policy...
Read more >
Mutation & Revalidation - SWR
populateCache = true : should the result of the remote mutation be written to the cache, or a function that receives new result...
Read more >
Azure Functions and Caching - Stack Overflow
The service bus message will contain a image url and the function will resize the image to a predefined resolution and will upload...
Read more >
Using query caching - Apache Druid
Brokers support both segment-level and whole-query result level caching. To control segment caching on the Broker, set the useCache and populateCache runtime ...
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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found