rest-cache is not aware of transformer plugin (or vice versa)
orvad opened this issue ยท 9 comments
Thanks for an awesome plugin :-)
When using strapi-plugin-transformer and rest-cache plugin only the first request from api is transformed. Subsequent and therefore cached api requests are not transformed.
Strapi 4.2.0
strapi-plugin-transformer 2.0.2 (Latest NPM)
strapi-plugin-rest-cache 4.2.4
strapi-plugin-rest-cache-provider-memory 4.2.4
Node 14 + 16
I have tried different orders in the plugins.js and different settings for each plugin, but it makes no difference. I have also tried disabling all other plugins (sentry, aws-upload, email).
It seems that somehow the rest-cache plugin is not aware of the transformer plugins transformations before storing it in cache.
Here are my rest-cache settings:
"rest-cache": {
config: {
provider: {
name: "memory",
getTimeout: 500,
options: {
max: 32767,
updateAgeOnGet: false,
// maxAge: env("MAX_AGE", 60000),
},
},
strategy: {
contentTypes: [
// list of Content-Types UID to cache
"api::competition.competition",
"api::photo.photo",
"api::photographer.photographer",
],
},
},
},
And the strapi-plugin-transformer settings:
transformer: {
enabled: true,
config: {
prefix: "/api/",
responseTransforms: {
removeAttributesKey: true,
removeDataKey: true,
},
},
},
Hello !
What is the load orders of your plugins ? (they are loaded sequentially, depending on how they are defined in config/plugins.js)
Hi
Thanks for getting back to me.
I have tried both rest-cache first and last. Makes no difference :-/
Different load orders I have tried:
transformer
sentry
upload
email
rest-cache
rest-cache
transformer
sentry
upload
email
transformer
rest-cache
# sentry
# upload
# email
rest-cache
transformer
# sentry
# upload
# email
Happens the same for me. Also tried changing the order of the plugins with no result. The request ends up transformed, but the rest cache keeps the original non transformed value, checked this using Redis.
I would expect that unless explicitly stated, rest cache should cache the final response value, even after custom middlewares.
Just thinking outloud, to my understanding, when a request is received it should first look if there's a cache entry, if yes respond with it, therefore saving unnecessary function executions and else. Otherwise execute all middleware functions and at the end (unless you say so and prefer to store the original data) make a new cache record.
Still don't fully understand Koa's middleware execution order, but I suspect (might be wrong) that this is due to the 'use' middlewares vs the route function middlewares.
Anyone had any luck with this one?
EDIT: There is light at the end of the tunnel.
Was just about to reply, saw your edit and good catch! Was looking into this issue as well. So basically it is the transformer which has issues because it cannot detect a cached call? Is that right?
Exactly - cached results return completely different context data. Checking the possible results now ...
Thinking out loud: would it be possible to change the response of the cache so the transformer understands?
Should be resolved by #35 and available on 4.2.5
Do not hesitate to reopen the issue if needed!