Allow to dynamically set the cache strategy in withCache
#2558
chrisvltn
started this conversation in
Ideas + Feature Requests
Replies: 1 comment 1 reply
-
Cache expiration defined by response result is definitely something we didn't think about when designing @frandiox Thoughts on how we can incorporate this ability into the upcoming |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Current state
When calling
withCache
, we can define a Cache strategystrategy
which specifies for how long that promise will be cached.Problem
Some third parties might specify the data expiration time in the response itself, making us unaware of the ideal
strategy
before doing the actual HTTP request to the 3rd party.Solution
We could add a function
updateStrategy
along with the existingaddDebugData
to thewithCache
action.This function would allow to dynamically overwrite the
strategy
during theaction
execution.From what I see in the current
withCache
code, thestrategy
is only used for saving the action result into the cache, meaning it would be possible to have it dynamically generated: https://github.com/Shopify/hydrogen/blob/3ec315587861da27aded80eef45ad394e0cd1b40/packages/hydrogen/src/cache/run-with-cache.tsThe usage could look something like (adapting the current example in the docs):
In this example, the API returns a video URL that will expire at
expireAt
(included in the response). This means the server should generate a new URL when thisexpireAt
time comes.Allowing to dynamically update the cache strategy allows this to be easily done.
Beta Was this translation helpful? Give feedback.
All reactions