Socket
Socket
Sign inDemoInstall

next-middlewares

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

next-middlewares

A library that can use multiple middleware in Next


Version published
Weekly downloads
5
Maintainers
1
Weekly downloads
 
Created
Source

Next Middlewares

publish MIT License npm npm dependents npm downloads

This is a library that can use multiple middlewares in Next.

Introduction

Next's middleware cannot be configured with multiple middlewares. To solve this problem, we provide a library to allow Next to support multiple middlewares.

Supported Versions

next-middlewares is tested with:

  • next: 13.0.0 and above

Installation

First, install next-middlewares:

$ npm install next-middlewares

# OR

$ yarn add next-middlewares

# OR

$ pnpm install next-middlewares

Usage

The way to define middlewares is almost exactly the same as Next. For details, please refer to the Next document Middleware. The difference is that we only support config.match, other configurations will be ignored.

For example, you can create a new middlewares folder and create a new redirection middleware redirect.ts in it, the code is as follows

import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'

export function middleware(request: NextRequest): Promise<NextResponse> {
  return NextResponse.redirect(new URL('/home', request.url))
}

export const config = {
  matcher: '/about',
};

Then, we can create a new middleware.ts file in the app or src or pages directory of the project according to Next's specifications. And reference our middlewares, use createMiddleware to combine them. Code show as below:

import { createMiddleware, config } from 'next-middlewares';

import * as RedirectMiddleware from './middlewares/redirect';
import * as RSSMiddleware from './middlewares/rss';

export const middleware = createMiddleware([RedirectMiddleware, RSSMiddleware]);

export { config };

That's all there is to it, your app will support multiple middlewares.

License

MIT

Keywords

FAQs

Package last updated on 08 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc