npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

next-wayfinder

v0.3.0

Published

Apply multiple next.js middlewares with ease

Downloads

520

Readme

Introduction

next-wayfinder is a lightweight (~3kb minzipped) and flexible package that simplifies the organization of middleware in Next.js applications. With next-wayfinder, you can easily apply different middlewares based on the route or hostname, without having to use cumbersome and error-prone path checks.

Traditionally, managing middleware in Next.js can be challenging, especially when dealing with complex routing scenarios. For example, you may want to apply an authentication middleware only for certain paths or a subdomain-specific middleware for certain hostnames. With next-wayfinder, you can easily manage and maintain your middleware and keep your code clean and organized. next-wayfinder exports an handlePaths function that can be used as middleware entry point. It accepts an array of Middleware objects that match the route or hostname, and applies the first matching middleware. This allows you to easily handle complex routing scenarios and reduce the amount of code needed to manage your middleware.

Installation

  npm install next-wayfinder

Why

next-wayfinder was created based on this discussion that highlighted the difficulty of handling complex routing inside the Next.js middleware. Traditionally, if you want to apply different middlewares for different routes in Next.js, you have to use cumbersome and error-prone path checks. For instance, you might want to have an authentication middleware only for paths matching /dashboard/:path* and a subdomain-specific middleware on another set of routes. As of now, this can be achieved through ugly path checking inside a middleware that matches almost all the routes.

With next-wayfinder, you can declare sub-middlewares via path-regexp and custom rules in order to achieve a nice, clear middleware file where you can come back after months and instantly understand what's happening. This makes it easy to handle complex routing scenarios and keep your code organized. In summary, next-wayfinder simplifies middleware management in Next.js applications and reduces the amount of code needed to manage your middleware, making it easier to write and maintain clean, organized code.

Quick Start

next-wayfinder exports an handlePaths function that can be used as middleware entry point. It accepts an array of Middleware objects that match route or hostname, and applies the first matching middleware.

// middleware.ts

import { handlePaths } from "next-wayfinder";
import { NextResponse } from "next/server";

// the first matching middleware will be applied
export default handlePaths(
    [
        {
            path: "/dashboard/:lang/:path*",
            // We can filter this to apply only if some params matches exactly our needs
            guard: params => params.lang === "en",
            handler: async req => {
                // url params are injected by `handlePaths`
                // in addition to req.query
                // this is done because you might want to handle paths
                // that are not available under your `app` or `pages` directory.
                console.log(req.params);

                // do some checks
                if (!isAuthenticated(req)) {
                    return NextResponse.redirect("/");
                }

                // continue the request
                return NextResponse.next();
            },
        },
        {
            hostname: /^app\./,
            // rewrites all routes on hostname app.* to the `pages/app/<path>`
            handler: req =>
                NextResponse.rewrite(
                    new URL("/app${req.nextUrl.pathname}", req.url)
                ),
        },
        {
            // easy syntax for redirects
            path: "/blog/:slug/edit",
            // params will be replaced automatically
            redirectTo: "/dashboard/posts/:slug",
        },
    ],
    {
        // inject custom data, like session etc
        // it will be available inside `req.ctx`
        context: async req => ({ isLoggedIn: !!req.session }),
        debug: process.env.NODE_ENV !== "production",
    }
);

What if I want to check paths on subdomain?

In some cases, you might want to check paths on a subdomain (i.e., using the same project for handling both the public website and the dashboard). This can be easily achieved by passing an array of middleware as a handler. The handlePaths function iterates recursively over all the items provided (including nested ones), so a very high level of complexity can be "handled". However, to improve performance, I would recommend keeping it as simple as possible.

// middleware.ts

export default handlePaths([
    {
        hostname: /^app\./,
        handler: [
            {
                path: "/",
                handler: req =>
                    NextResponse.redirect(new URL("/dashboard", req.url)),
            },
            {
                path: "/:path*",
                handler: () => NextResponse.next(),
            },
        ],
    },
]);

Example - Supabase Authentication

// middleware.ts
import { NextResponse, NextRequest } from "next/server";
import { handlePaths, NextREquestWithParams } from "next-wayfinder";
import { createMiddlewareSupabaseClient } from "@supabase/auth-helpers-nextjs";

const getSession = async (req: NextRequestWithParams) => {
    const res = NextResponse.next();
    const supabase = createMiddlewareSupabaseClient({ req, res });

    const {
        data: { session },
        error,
    } = await supabase.auth.getSession();

    if (error) {
        console.error(`Auth Error: `, error);
        return null;
    }

    return session;
};

export default handlePaths(
    [
        {
            // auth guard
            path: "/dashboard/:path*",
            pre: req =>
                req.ctx?.session ? true : { redirectTo: "/auth/sign-in" },
            handler: (req, res) => {
                console.log("User authenticated: ", req.ctx?.session);

                // do your stuff here
                return res;
            },
        },
    ],
    {
        // this injects `session` property into the request object
        context: async req => {
            const session = await getSession(req);

            return { session };
        },
    }
);

Example - Filter by request method

This examples shows you how you can filter by request method

// middleware.ts
import { NextResponse, NextRequest } from "next/server";
import { handlePaths, NextREquestWithParams } from "next-wayfinder";
import { createMiddlewareSupabaseClient } from "@supabase/auth-helpers-nextjs";

const getSession = async (req: NextRequestWithParams) => {
    const res = NextResponse.next();
    const supabase = createMiddlewareSupabaseClient({ req, res });

    const {
        data: { session },
        error,
    } = await supabase.auth.getSession();

    if (error) {
        console.error(`Auth Error: `, error);
        return null;
    }

    return session;
};

export default handlePaths(
    [
        {
            // do not check auth on GET requests
            path: "/events/:id",
            method: "GET",
            handler: (_, res) => res,
        },
        {
            // auth guard on PATCH
            path: "/events/:id",
            method: "PATCH",
            pre: req => req.ctx?.session ? true : { redirectTo: "/auth/sign-in" },
            handler: (req, res) => {
                console.log("User authenticated: ", req.ctx?.session);
                // do your stuff here
                return res;
            },
        },
    ],
    {
        // this injects `session` property into the request object
        context: async req => {
            const session = await getSession(req);

            return { session };
        },
    }
);

Options

You can pass several options to configure your middleware

interface WayfinderOptions<T> {
  debug?: boolean;

  /**
   *
   * A function that returns the data to be injected into the request
   */
  context?: <T>((request: NextRequest) => T) | T;

  /**
   * Global middleware to be executed before all other middlewares
   * Useful if you want to set a cookie or apply some logic before each request
   * It receives the `options.response` (or `NextResponse.next()` if not provided) and `NextRequest` as params
   */
  beforeAll?: (request: NextRequest, response: NextResponse) => Promise<NextResponse> | NextResponse;

  /**
   *
   * A function to extract `hostname` and `pathname` from `NextRequest`
   */
  parser?: RequestParser;

  /**
   * The response to be used.
   * Useful when you want to chain other middlewares or return a custom response
   * Default to `NextResponse.next()`
   */
  response?: NextResponse | ((request: NextRequest) => NextResponse);
}

Authors

This library is created by Federico Vitale - (@rawnly)

License

The MIT License.