Next.js Discord

Discord Forum

Seeking Efficient Next.js Routing Similar to Gatsby's Programmatic File-Based Approach

Unanswered
Dwarf Crocodile posted this in #help-forum
Open in Discord
Dwarf CrocodileOP
Hello, Next.js Community,

I am in the process of enhancing a Next.js application that employs the framework's app router with file-based routing. Our application needs to serve content dynamically based on a two-layer nested URL pattern (/{topic}/{subtopic}). The list of topics, which are drawn from a text file, includes entries like:

foo
bar
baz
qux-next

and is subject to updates.

The current setup processes every two-segment URL request (/first/second) to determine if it matches any topic from our list, which is not optimal for our site's scale.

The challenge:
We're aiming to refine this process. Specifically, we'd like to avoid checking every two-part URL against our topics list due to efficiency concerns. Moreover, it's crucial that certain paths (e.g., /customer/support) do not undergo this topic-related rendering, maintaining their original functionality untouched.

Inspired by Gatsby's programmatic file-based routing system, we're looking for a way to apply a similar approach within Next.js. Gatsby allows for dynamic route generation based on site content, which seems like a promising model for our needs. Our goal is to dynamically generate routes for valid topic and subtopic combinations without the need for preliminary checks for each request.

The question:
Does Next.js offer a method or best practice to dynamically register routes based on external data (like our text file), similar to Gatsby's programmatic file-based routing? How can we ensure that only URLs conforming to our /{topic}/{subtopic} pattern trigger the dynamic content rendering, while explicitly excluding non-related paths like /customer/support from this process?

Any guidance, insights, or examples would be deeply appreciated.

Thank you for your time and help!

1 Reply

Dwarf CrocodileOP
A Few Quick Follow-Up Points for Better Clarity:

We're exploring alternative approaches, such as implementing a root prefix like /topic/{topic}/{subtopic} to streamline our routing structure.

Switching away from Next.js or moving away from the app router isn't an option for us; the decision on tools is well beyond me or my team.