@lidofinance/next-cache-files-middleware
TypeScript icon, indicating that this package has built-in type declarations

0.43.0 • Public • Published

@lidofinance/next-cache-files-middleware

Next.js middleware to add cache-control headers to file requests You can read about middleware there

Installation

yarn add @lidofinance/next-cache-files-middleware.

Getting started

Usage

  1. Create file middleware.ts in root folder
  2. Put the code from the example in this file
  3. Update configuration constants if necessary

About constant config

The matcher values need to be constants so they can be statically analyzed at build-time. Dynamic values such as variables will be ignored. Therefore, you need to manually register the specified paths from the example. Example there exampleMiddlewareConfig


import { cacheControlMiddlewareFactory } from '@lidofinance/next-cache-files-middleware'

export const middleware = cacheControlMiddlewareFactory()

export const config = {
  // paths where use middleware
  matcher: ['/manifest.json', '/favicon:size*'],
}

export default middleware

Use with your own allowed list

import {
  cacheControlMiddlewareFactory,
  CacheAllowedItem,
  CACHE_FILE_DEFAULT_HEADERS
} from '@lidofinance/next-cache-files-middleware'

// cache allowed list
const SomePathConfig: CacheAllowedItem[] = [{
   { path: /^\/someStaticFileRegExp.json?$/, headers: CACHE_FILE_DEFAULT_HEADERS },
   { path: '/someStaticFileString.json', headers: CACHE_FILE_DEFAULT_HEADERS },
}]

// Use allowed list
export const middleware = cacheControlMiddlewareFactory(SomePathConfig)

// Note: The matcher values need to be constants so they can be statically analyzed at build-time.
// Dynamic values such as variables will be ignored.
export const config = {
  // paths where use middleware
  matcher: ['/manifest.json', '/favicon:size*', '/someStaticFile.json']
}

export default middleware

Use with your own middleware

import {
  cacheControlMiddlewareFactory,
  CacheAllowedItem,
  CACHE_FILE_DEFAULT_HEADERS
} from '@lidofinance/next-cache-files-middleware'

// cache allowed list
const SomePathConfig: CacheAllowedItem[] = [{
   { path: /^\/someStaticFileRegExp.json?$/, headers: CACHE_FILE_DEFAULT_HEADERS },
   { path: '/someStaticFileString.json', headers: CACHE_FILE_DEFAULT_HEADERS },
}]

// Use allowed list
export const cacheControlMiddleware = cacheControlMiddlewareFactory(SomePathConfig)
// or
export const cacheControlMiddleware = cacheControlMiddlewareFactory()

// use only for cache files
const middleware = (req: NextRequest): NextResponse => {
  // add cache control middleware
  const response = cacheControlMiddleware(req);

  // some code

  return response;
}

// or use wrapper
export const middleware = cacheControlMiddlewareWrapper(SomePathConfig)((req, res) => {
  // some code

  return res;
});

export const config = {
  // paths where use middleware
  matcher: ['/manifest.json', '/favicon:size*', '/someUrl'],
};

export default middleware;

Readme

Keywords

none

Package Sidebar

Install

npm i @lidofinance/next-cache-files-middleware

Weekly Downloads

87

Version

0.43.0

License

MIT

Unpacked Size

28.6 kB

Total Files

11

Last publish

Collaborators

  • lidofinance-ui
  • lidofinance-group