0.43.0 • Published 14 days ago

@lidofinance/next-cache-files-middleware v0.43.0

Weekly downloads
-
License
MIT
Repository
github
Last release
14 days ago

@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;
0.43.0

14 days ago

0.42.0

2 months ago

0.41.0

2 months ago

0.40.0

3 months ago

0.39.1

5 months ago

0.39.0

7 months ago

0.38.0

8 months ago

0.37.1

8 months ago

0.37.0

8 months ago

0.36.0

9 months ago

0.35.0

9 months ago

0.34.0

9 months ago

0.33.0

10 months ago

0.32.0

10 months ago

0.31.0

10 months ago

0.30.0

10 months ago

0.29.0

10 months ago

0.28.0

11 months ago

0.21.0

1 year ago

0.27.0

11 months ago

0.26.0

12 months ago

0.25.0

12 months ago

0.24.0

12 months ago

0.23.0

12 months ago

0.22.0

12 months ago

0.20.0

1 year ago

0.19.0

1 year ago

0.17.0

1 year ago

0.18.0

1 year ago

0.14.0

1 year ago

0.15.0

1 year ago

0.16.0

1 year ago

0.10.0

1 year ago

0.11.0

1 year ago

0.9.0

1 year ago

0.12.0

1 year ago

0.8.0

1 year ago

0.13.0

1 year ago

0.7.0

1 year ago

0.6.0

2 years ago

0.5.0

2 years ago

0.5.1

2 years ago

0.3.0

2 years ago