Closed oddnavy closed 5 years ago
Correct, transformers are only run when not pulling from cache.
Set the headers from your main endpoint config factory instead:
'api/pages/<id>' => function ($id) {
Craft::$app->response->headers
->set('Access-Control-Allow-Origin', '*')
->set('Cache-Control', 's-maxage=30, stale-while-revalidate=86400, stale-if-error=86400');
return [
'cache' => 'PT30S',
'criteria' => ['id' => $id],
'one' => true,
'transformer' => new PageTransformer(),
];
},
Thank you! This will be handy
If we add headers via a TransformAbstract, these headers are lost from the request when the PHP cache is enabled in the element-api plugin. Assuming this could be due to the transformer not being called when the request is served from cache.
For example:
Has the following response headers:
But if we enable the cache flag in the element-api config:
The response headers are:
How can we ensure cache headers are always set? We want to cache api requests at a CDN level too.