Replies: 2 comments 2 replies
-
Agreed with this - I think the builder function API is easier to use, and also the Intellisense in-editor from it is much better than what you get from the current string interpolation system. |
Beta Was this translation helpful? Give feedback.
2 replies
-
I have created a discussion (#55499) with a broader scope, which is why I didn't extend this thread. For that discussion I've written a proof of concept that allows such a function to easily be created by both library authors and / or Feedback would be welcome. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Goals
Non-Goals
Background
In Next 13.2 we added https://nextjs.org/docs/app/api-reference/next-config-js/typedRoutes which enables type-safe routing. Next.js can statically type links to prevent typos and other errors. This is a nice addition.
Before it happen, I use https://github.com/Schniz/next-static-paths (which became deprecated because after
typeRoutes
was introduced). It allows me to define routes in a type-safe way (as long as you run the generate command before adding new pages). I like its interface - a functionpathFor
that accepts two params:For example:
pathFor("/[tenantId]/settings/branding", { tenantId })
will return/${tenantId}/settings/branding
string.It shines when we have multiple params and we don't really care about the path order structure as much as we care about navigating to the right route.
For example:
pathFor("/[tenantId]/bases/[baseId]/[tableId]/[viewId]", {tableId, tenantId, baseId, viewId,})
instead of/${tenantId}/bases/${baseId}/${tableId}/${viewId}
. The user may type it wrong like/${tenantId}/bases/${baseId}/${viewId}/${tableId}
and typedRoutes won't catch it.Typing
pathFor("/[tenantId]/bases/[baseId]/[tableId]/[viewId]", {tableId, tenantId, baseId: viewId, viewId: baseId,})
is more noticable.Also - when moving files to change route structure with dynamic params -
pathFor
static params make it easier to refactor. Just search the static string. We cannot search for/${tenantId}/bases/${baseId}/${viewId}/${tableId}
because no one guaranteed that thebaseId
dynamic path segment will be called in the same way as the variablebaseId
.Similar maintained projects are that tackles this issue are
The drawback of those libraries is that we need to either run a script to generate the
.d.ts
file manually, and we cannot be sure they won't break as next.js changes in the future.Proposal
Expose function from
next
(or nested packages) namedroute
/pathFor
.The function hsould return a string that represents a valid page route. Its type will be the same type as
Route
type fromtypedRoutes
feature.It will have a static string as a param - for better searchablility and an object for dynamic paths.
This function will be generated at the same time
typedRoutes
feature generate Routes type.route("/static")
will result"/static"
)[dynamicPart]
syntax. It's better to make it deterministic (only one valid syntax for a single param) and as close as possible to the next.js file structure (so it will be easy to understand). I suggest `route("/dynamic/[userId]", { userId: "123" }).Same as https://github.com/Schniz/next-static-paths, maybe a different name (
route
?), but exposed from next.js and support app router.Beta Was this translation helpful? Give feedback.
All reactions