🚀 Launch Week Day 3:Introducing Supply Chain Attack Campaigns Tracking.Learn More →
Socket
Book a DemoInstallSign in
Socket

@next/routing

Package Overview
Dependencies
Maintainers
5
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@next/routing

Next.js shared route resolving

latest
Source
npmnpm
Version
16.1.1-canary.7
Version published
Maintainers
5
Created
Source

@next/routing

Shared route resolving package for Next.js.

NOTE: This package is experimental and will become stable along with adapters API

Overview

This package provides a comprehensive route resolution system that handles rewrites, redirects, middleware invocation, and dynamic route matching with support for conditional routing based on headers, cookies, queries, and host.

Installation

npm install @next/routing

Usage

import { resolveRoutes } from '@next/routing'

const result = await resolveRoutes({
  url: new URL('https://example.com/api/users'),
  basePath: '',
  requestBody: readableStream,
  headers: new Headers(),
  pathnames: ['/api/users', '/api/posts'],
  routes: {
    beforeMiddleware: [],
    beforeFiles: [],
    afterFiles: [],
    dynamicRoutes: [],
    onMatch: [],
    fallback: [],
  },
  invokeMiddleware: async (ctx) => {
    // Your middleware logic
    return {}
  },
})

if (result.matchedPathname) {
  console.log('Matched:', result.matchedPathname)
}

Route Resolution Flow

  • beforeMiddleware routes - Applied before middleware execution
  • invokeMiddleware - Custom middleware logic
  • beforeFiles routes - Applied before checking filesystem
  • Static pathname matching - Check against provided pathnames
  • afterFiles routes - Applied after filesystem checks
  • dynamicRoutes - Dynamic route matching with parameter extraction
  • fallback routes - Final fallback routes

Route Configuration

Each route can have:

  • sourceRegex - Regular expression to match against pathname
  • destination - Destination path with support for replacements ($1, $name)
  • headers - Headers to apply on match
  • has - Conditions that must match
  • missing - Conditions that must not match
  • status - HTTP status code (3xx for redirects)

Redirects

When a route has:

  • A redirect status code (300-399)
  • Headers containing Location or Refresh

The routing will end immediately and return a redirect result with the destination URL and status code.

Has/Missing Conditions

Conditions support:

  • header - Match HTTP headers
  • cookie - Match cookies
  • query - Match query parameters
  • host - Match hostname

Values can be:

  • undefined - Match if key exists
  • String - Direct string match
  • Regex string - Match against regex pattern

Keywords

react

FAQs

Package last updated on 28 Dec 2025

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