Replies: 4 comments 3 replies
-
Calling Making |
Beta Was this translation helpful? Give feedback.
-
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 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 |
Beta Was this translation helpful? Give feedback.
-
Here, I made a wrapper: /**
* A wrapper for Ziggy's route helper with error handling and strict route names.
*/
// Called with no arguments - returns a Router instance
export function appRoute(): Router;
// Called with a route name and optional additional arguments - returns a URL string
export function appRoute<T extends KnownRouteName>(
name: T,
params?: RouteParams<T> | undefined,
absolute?: boolean,
errCallback?: (e: unknown) => string,
config?: Config
): string;
export function appRoute<T extends KnownRouteName>(
name: T,
params?: ParameterValue | undefined,
absolute?: boolean,
errCallback?: (e: unknown) => string,
config?: Config
): string;
// Called with configuration arguments only - returns a configured Router instance
export function appRoute(
name: undefined,
params: undefined,
absolute?: boolean,
errCallback?: (e: unknown) => string,
config?: Config
): Router;
export function appRoute(
name?: KnownRouteName | undefined,
params?: RouteParams<KnownRouteName> | ParameterValue | undefined,
absolute?: boolean,
errCallback: (e: unknown) => string = (e) => {
console.error(e);
return "#";
},
config?: Config
): string | Router {
try {
if (
name === undefined &&
params === undefined &&
absolute === undefined &&
config === undefined
) {
return route() as unknown as Router;
}
if (name === undefined) {
return route(
name,
params as undefined,
absolute,
config
) as unknown as Router;
}
return route(
name,
params as ParameterValue,
absolute,
config
) as unknown as string;
} catch (e) {
// Possibly redirect to error page or show a toast
return `${errCallback(e)}` || "#";
}
} Used like this: <Link :href="appRoute('dashboard')"> <!-- if invalid then typescript error, autocomplete still works -->
Dashboard
</Link> If the route doesn't exist, typescript shows an error. Basically it just uses |
Beta Was this translation helpful? Give feedback.
-
Hi @bakerkretzmar I looked into this and found a simple solution to allow people to enable strict mode if they want it Library index.d.ts
The same way as the RouteList, the TypeConfig interface can be overridden User code ziggy-strict.d.ts
When the override is not present, it currently ends up being any. So with a slight adjustment, it works when no strict key is set
|
Beta Was this translation helpful? Give feedback.
-
Description
When you type a route that doesn't exist it doesn't show any errors nor warnings in
any environmentIDE.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:
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 theziggy.d.ts
file. It can be namedstrictRoutes
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 manuallyBeta Was this translation helpful? Give feedback.
All reactions