tighten / ziggy

Use your Laravel routes in JavaScript.
MIT License
3.83k stars 247 forks source link

Add an option for strict route checking #758

Closed Esensats closed 3 months ago

Esensats commented 3 months ago

Description

When you type a route that doesn't exist it doesn't throw any errors nor warnings in any environment. Not even in runtime when the route is accessed. I use php artisan ziggy:generate --types-only each time the routes are updated in the backend (automatically, using a watcher). My environment: Laravel 11, PHP 8.3, Breeze Inertia Vue template, added Typescript, declared ziggy's global route

Example:

A vue page component:

<Link :href="route('invalid.route')"> <!-- No error by Typescript, nor in browser, nor in runtime (console), even though the route auto-complete works  -->
Dashboard
</Link>

When the page with this invalid link is requested (not even the route itself), the user only sees a blank white page with zero errors, not even in the Browser console nor in the Networking tab. The PHP logs emit no errors as well. I think it's critical to at least inform consumers about this potential debugging nightmare.

Suggestion

I suggest providing an option that makes Typescript yell at you when the string in the route function isn't declared by the ziggy.d.ts file. It can be named strictRoutes of type boolean for example.

Alternatives

Enforce a mechanism that makes the server throw an error to the user and logging it. Or update the Breeze Inertia template to have this error handling built-in. Or at least write in the README how to set error handling up manually (be it typescript or PHP runtime)

bakerkretzmar commented 3 months ago

Calling route('invalid.route') should definitely error and you should see it in the browser console. Is that exactly the code in your app? What page is it on in the Inertia template? Do you have anything like Sentry set up that might be swallowing errors? Are you having this issue in development mode or with a full Vite build, or both?

Making route() stricter about the route names passed into it is something I'm totally open to.

Esensats commented 3 months ago

Calling route('invalid.route') should definitely error and you should see it in the browser console. Is that exactly the code in your app? What page is it on in the Inertia template? Do you have anything like Sentry set up that might be swallowing errors? Are you having this issue in development mode or with a full Vite build, or both?

Making route() stricter about the route names passed into it is something I'm totally open to.

Oof, I was either extremely inattentive or it for some reason didn't show up in the browser console at the time I checked. It does indeed show up in the browser console now that I check again. Sorry for misinforming on that part.

Question, is it possible to have it check all the route calls at build-time? If not, then either way, I guess the simplest solution would be to provide an option to show the error in development time: optional Typescript error/warning.

If this solution is chosen, then the next natural question would be - where to put the option? In what configuration file? If I am not mistaken, the ziggy.js file is not made for that. I guess it'd be put as into some kind of an options param while calling app.use(ZiggyVue({strictRoutes: true})), making ZiggyVue callable, right?