workers-sdk
workers-sdk copied to clipboard
๐ BUG: Wildcard is used after return of a more specific route
What version of Wrangler are you using?
2.6.1
What operating system are you using?
MacOS
Describe the Bug
Using wrangler pages dev . and having the following file structure:
โโ functions
โโโ [[path]].js
โโโ _middleware.js
โโโ index.js
Consider index.js doing this:
export async function onRequest(context) {
return new Response('Doing something on root index');
}
and [[path]].js this:
export async function onRequest(context) {
return new Response('Doing something instead of 404 :-)');
}
Opening the root http://127.0.0.1:8788/ will return the wildcard match (print: 'Doing something instead of 404 :-)').
I would expect to let the more specific route to "return its new Response()" but it's completely lost. This behavior should only be if one would call context.next() from index.js.
It appears that the /** route is even preferred over anything in the asset folder (e.g. /dist).
So there is no way to just make a "woah, not found? Let's try this instead" handler when no more specific routes matched.
The doc (https://developers.cloudflare.com/pages/platform/functions/) about cloudflare pages functions says:
Route specificity More specific routes (that is, those with fewer wildcards) take precedence over less specific routes.
For me this is not true at all, as it would discard functions/index.js as well as dist/logo.svg which both I would consider more specific than /**. In the second case, one would always have to redirect via env.ASSETS - wasting precious (in some cases even paid) worker route calls. That's a pitty.
I don't know at the moment if that's a wrangler issue or if cloudlfare itself behaves differently but it makes my - and probably many others' - development more cumbersome.
I'm also encountering this. It's definitely a bug because the wildcard route doesn't match other functions as far as I can tell.
E.g. with 3 defined functions:
functions/index.jsfunctions/about.jsfunctions/[[path]].js
GET /about->about.js(skips[[path]].jsentirely as expected)GET /->[[path]].js(skipsindex.jsentirely, quite unexpectedly ๐ )
Seems like there's a weird edge-case in the /->index.js mapping.
This is with [email protected] on macOS Monterey.
thank you for your report. Unfortunately this will have to wait until we have some sort of versioning mechanism in Pages Functions