Skip to content
On this page

Cache custom routes

By default the plugin registers a middleware to intercept all predefined routes, but you can also enable it on custom routes.

js
// file: /config/plugins.js

module.exports = ({ env }) => ({
  'rest-cache': {
    config: {,
      provider: {
        // ...
      },
      strategy: {
        contentTypes: [
          {
            contentType: "api::pages.pages",
            routes: /* @type {CacheRouteConfig[]} */ [
              {
                path: '/api/pages/slug/:slug+', // note that we set the /api prefix here
                method: 'GET', // can be omitted, defaults to GET
              },
            ],
          },
        ],
      },
    },
  },
});

WARNING

At this time a custom route can only be registered within a single Content-Type.

CacheRouteConfig reference

path

Refer to an existing route path in strapi on which the cache middleware will be registered.
A warning will be displayed if the path does not exist.

  • Type: string
  • Default: GET

method

Refer to an existing route method in strapi on which the cache middleware will be registered.
A warning will be displayed if the path does not exist.

  • Type: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'
  • Default: GET

TIP

Cache lookup is performed only on GET requests, and cache invalidation is performed on all other requests.

hitpass

When true, the cache plugin will not lookup for cache and serve fresh response from backend instead. Also, the response is not stored in the cache.

  • Type: (ctx: Context) => boolean | boolean
  • Default: (inherit from CacheContentTypeConfig if set)

keys

Options used to generate the cache keys.

maxAge

Default max age for cached entries.

  • Type: number
  • Default: (inherit from CacheContentTypeConfig if set)