Set cache tags for unstable cache based on its result
Unanswered
Large oak-apple gall posted this in #help-forum
Large oak-apple gallOP
I need to revalidate unstable cache by tags.
Originally my workflow required list of item ids as input and tags were formed as
Now I modified this function to accept second list of parameters, so list of item ids can now be empty.
Which leads to problem: forming list of cache tags is no longer possible by directly using provided ids.
Although result data would contain all ids that are needed to form tags. But currently it is not working even when storing ids from result as outside of unstable cache.
Is there better way to do it?
Cache keys seem to be useless here. I expect to pass same arguments into top function and only get new results if something from outside called tag revalidation for item/id
Originally my workflow required list of item ids as input and tags were formed as
items/id for each id in list.Now I modified this function to accept second list of parameters, so list of item ids can now be empty.
Which leads to problem: forming list of cache tags is no longer possible by directly using provided ids.
Although result data would contain all ids that are needed to form tags. But currently it is not working even when storing ids from result as outside of unstable cache.
Is there better way to do it?
export function cachedOptions(ids: string[] = [], marks: string[] = []) {
const idsForCacheTags: string[] = []
return unstable_cache(
async (ids: string[], marks: string[]) => {
data = // fetch some data base on ids and marks
idsForCacheTags.push(...data.map((item) => item.id))
return data
},
[],
{
tags: idsForCacheTags.map((id) => `items/${id}`),
},
)(ids, marks)
}Cache keys seem to be useless here. I expect to pass same arguments into top function and only get new results if something from outside called tag revalidation for item/id