tanc
tanc

Reputation: 321

How to add cache headers to Strapi API endpoint

I'd like to understand how to enable caching in Strapi for a specific (or any) API endpoint. At the moment when the browser hits my endpoint in the response headers I don't see any caching related headers. Is there a way to use etags and have a long cache time to allow the JSON response to be cached?

There is one mention of etags in the docs but I'm not sure how to implement. If anyone can provide more detailed information it would be appreciated.

Upvotes: 1

Views: 4317

Answers (2)

debite
debite

Reputation: 444

At least for static files this can be done within Strapi itself. The middlewares documentation suggests, that there is already a middleware called public, which sets a Cache-Control header with its maxAge when it serves the files from the public/ directory.

But if you want to get your uploaded files cached (i.e. files within the public/uploads/ directory), that’s not enough, because the middleware strapi-provider-upload-local (not yet documented) runs first.

A recently published package solves this issue:

npm i strapi-middleware-upload-plugin-cache

Simply activate it by adding or modifying the config/middleware.js file with the following content:

module.exports = ({ env }) => ({
  settings: {
    'upload-plugin-cache': {
      enabled: true,
      maxAge: 86400000
    }
  }
});

Upvotes: 3

Jim LAURIE
Jim LAURIE

Reputation: 4120

I suggest you manage this kind of thing outside of Strapi.

With another service. For example, if you host you app on AWS, you can use CloudFront.

Upvotes: -3

Related Questions