Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

next-nginx-routes

geops23.3kBSD-2-Clause1.3.0

Generate Nginx routing configuration for static Next.js projects.

export, Next.js, Nginx, configuration, routing, static

readme

next-nginx-routes

Generate Nginx routes configuration file for Next.js static HMTL export, also known as output: export.

This dependency-free NPM package provides a NPM script to convert all your Next.js routes like:

{
  "page": "/[foo]",
  "regex": "^/([^/]+?)(?:/)?$",
  "routeKeys": { "foo": "foo" },
  "namedRegex": "^/(?<foo>[^/]+?)(?:/)?$"
}

... into Nginx routes like:

location ~ ^/([^/]+?)(?:/)?$ {
  try_files /[foo].html /index.html;
}

Note: currently only Next.js pages router is fully supported.

Getting started

Installation

yarn add --dev next-nginx-routes

Make sure output: "export" is configured in your next.config.js file.

Generate Nginx configuration file

Add next-nginx-routes to your build script:

{
  "build": "next build && next-nginx-routes"
}

And run Next.js export:

yarn run build

Include Nginx configuration file

Finally include the generated configuration file next-routes.conf in your Nginx site configuration file and make the Next.js out directory available to Nginx.

Requirements

More

  • See example folder for a minimal Docker example.
  • Read more about this approach in our blog post.

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.3.0 (2024-10-22)

Features

  • next 15: expand peer dependencies to include next 15 (be2ff6f)

1.2.1 (2023-11-03)

Bug Fixes

  • generate correct routes for trailingSlash config (d98e351)

1.2.0 (2023-11-03)

Features

Bug Fixes

  • add Next.js 14 to peer dependencies (edf33c5)

1.1.1 (2022-10-28)

Bug Fixes

  • add Next.js 13 to peer dependencies (d8fe91b)

1.1.0 (2022-09-29)

Features

  • support static routes inside dynamic routes (6f775ce)

1.0.0 (2022-03-28)