Expansion of route strings during build #784
Radiergummi
started this conversation in
Ideas
Replies: 1 comment
-
This is so cool, thanks for sharing! |
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
-
Description
I only discovered Ziggy after building pretty much the same thing for our application on my own. However, our routing bridge has one feature that I thought may be valuable for others too; namely, we have a Vite plugin that statically replaces all route function invocations at build time with the full URL string.
Specifically, any occurrence of
import.meta.route("venues.events.show", [1, eventId])
will be replaced with`https://ziggy.test/venues/1/events/${eventId}`
. As you can see, it renders a template string with the dynamic segments as placeholders, and the static segments inlined.What looks relatively straightforward requires a complex AST transformation step; on the other hand, that avoids having to ship additional code to the client (it's a zero-cost abstraction), validates all routes at build-time, and has no DX downsides thanks to type stubs, which provide autocompletion for
import.meta.route
with all application routes.I thought I'd just leave the source code of our plugin here, should the Ziggy maintainers be interested in adding such a Vite plugin; feel free to disregard it otherwise.
https://gist.github.com/Radiergummi/198ae14450a947a45b7cadaeab6ccc61
Beta Was this translation helpful? Give feedback.
All reactions