JSPM

strapi-middleware-upload-plugin-cache

1.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 55
  • Score
    100M100P100Q81046F
  • License MIT

Adds middleware for caching uploaded assets when using strapi-provider-upload-local

Package Exports

  • strapi-middleware-upload-plugin-cache

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (strapi-middleware-upload-plugin-cache) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

strapi-middleware-upload-plugin-cache

Use case

  • 'strapi-provider-upload-local' is in use for uploading assets via Media Library in Strapi
  • the need for configurable cache-control HTTP response headers (e.g. cache-control: max-age=1234) + ETag HTTP response header for each asset
  • more information and differences between koa-static (used in 'strapi-provider-upload-local') and koa-static-cache (used in this middleware-plugin) can be found here

Installing

Using npm

npm install --save strapi-middleware-upload-plugin-cache

Using yarn

yarn add strapi-middleware-upload-plugin-cache

Setup

For Strapi, add a middleware.js file within the config folder

e.g.

touch config/middleware.js

containing

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

Starting Strapi in dev-mode should log the following:

[2020-12-30T21:37:57.560Z] debug [UploadCache] Initializing middleware ...
[2020-12-30T21:37:57.561Z] debug [UploadCache] Middleware initialized for endpoint='/uploads/(.*)' [maxAge=86400000]

Configuration Options

With the option dynamic: true files which are not cached on initializations are dynamically loaded. To avoid OOM errors a LRU-cache can be used. For config options of lru-cache use these docs and insert them at lruCache.

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

Resources