Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
unflatten-next-routes
Advanced tools
Changelog
v0.1.3
.ts
extension (00dc28c)Readme
Enabling flat routes for Next.js
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.
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.
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
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
The tool supports .tsx, .jsx, and .js file extensions for flat route files.
FAQs
A CLI tool to simplify Next.js routing by allowing developers to work with a flat route structure
We found that unflatten-next-routes demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.