Next.js Discord

Discord Forum

Need help with custom Catchall Route Handling on App Router.

Unanswered
American Crocodile posted this in #help-forum
Open in Discord
American CrocodileOP
I'm looking into migrating an old .net mvc4 project that uses routes file and a custom route controller to handle all routes at the root without prefix segments. I'm having a hard time understanding App Router routing fully.

I need to create a full custom routing that handles the following:

Parameter Trimming and Splitting:

The parameters string is trimmed of trailing slashes.
It is then split into a list using the forward slash '/' as a delimiter.
Example:
If parameters is "/example/param", after trimming and splitting, splitParameters would be a list containing ["example", "param"].

Handling Single Parameters:

If there's only one parameter in the splitParameters list (Count == 1), it checks for specific cases.
If the parameter corresponds to a static .html file, it returns a view.
If the parameter is "robots.txt", it returns a file depending on the domain.
If the parameter contains "sitemap", it returns a file from a specific directory.
If the parameter matches a list of slugs (around 1M) it processes and returns data related to that entity.
Example:
If splitParameters contains ["person-name"], it may return a view for the "person-name" page.

Handling Search:

If none of the specific cases match and there are search keywords present in splitParameters (I need to check against a predefined array) it prepares and returns a search result.
Example:
If splitParameters contains ["cityName", "CountryName"], it proceeds to handle a search operation based on those parameters.

Error Handling:

If none of the above conditions match, it throws an HTTP 404 "Page not found" exception.
Example:
If splitParameters doesn't match any of the expected cases, it raises a 404 error.

Any points on the direction I need to handle this rounting would be appreciated.

0 Replies