Socket
Socket
Sign inDemoInstall

unflatten-next-routes

Package Overview
Dependencies
8
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    unflatten-next-routes

A CLI tool to simplify Next.js routing by allowing developers to work with a flat route structure


Version published
Maintainers
1
Created

Changelog

Source

v0.1.3

compare changes

🚀 Enhancements

  • Add support for .ts extension (00dc28c)

📖 Documentation

🏡 Chore

  • Add keywords to package.json (40baca9)

❤️ Contributors

Readme

Source

unflatten-next-routes

npm version Github Actions

Enabling flat routes for Next.js

Table of Contents


Introduction

With the introduction of Next.js 13, a new folder-based routing mechanism was unveiled. While this approach offers powerful and flexible routing capabilities, it brings with it the challenge of managing deeply nested route files. In large projects with a myriad of routes, locating a specific route or deciphering the intricate structure of the application becomes increasingly complex.

Enter unflatten-next-routes.

Designed specifically for Next.js 13, unflatten-next-routes is a CLI tool that allows developers to work with a flat route file structure that is easier to manage and understand. With unflatten-next-routes your routes can be structured like this:

app/
|-- shop/
    |-- routes/
        |-- basket.(page).tsx
        |-- product.(page).tsx
        |-- product.[id].(page).tsx

... and unflatten-next-routes will ensure that these routes are transformed into the nested format that Next.js expects.

Usage

To start using the CLI tool, run the following command in the Next.js project root:

npx unflatten-next-routes@latest

This will initiate the tool in watch mode. Then add flat route files within any /routes/ folder located within the app directory. As you add, rename, or remove these flat route files, the equivalent nested route file will be generated or updated within a parallel /(.routes)/ directory.

Example:

app/
|-- shop/
    |-- (.routes)/
    |   |-- basket/
    |   |   |-- page.tsx
    |   |-- product/
    |       |-- page.tsx
    |       |-- [id]/
    |           |-- page.tsx
    |-- routes/
        |-- basket.(page).tsx
        |-- product.(page).tsx
        |-- product.[id].(page).tsx

Note: The /(.routes)/ directory should be considered as "private", similar to the .next directory that Next.js uses for its build output. Files within this directory are auto-generated and should not be manually edited.

About flat routes

Flat routes can be created for page.tsx, layout.tsx, loading.tsx, error.tsx and route.tsx files. All that is required is for their flat route equivalent filenames to end with .(page).tsx, .(layout).tsx, .(loading).tsx, .(error).tsx and .(route).tsx .

Example:

/app/shop/routes/basket.(page).tsx
/app/shop/routes/product.(layout).tsx
/app/shop/routes/product.[productId].(page).tsx
/app/admin/routes/settings.(page).tsx

Index route files

For flat index route files, there's no need to prefix the route filename. For instance, the admin homepage will be:

/app/admin/(page).tsx
/app/admin/(error).tsx

Supported file extensions

The tool supports .tsx, .jsx, and .js file extensions for flat route files.

License

MIT

Keywords

FAQs

Last updated on 19 Aug 2023

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc