🐛 BUG: Inconsistent behaviour in `_routes.json`. Also, no way to specify index file.
What version of Wrangler are you using?
2.1.12
What operating system are you using?
Windows
Describe the Bug
When using a _routes.json, the following:
"include": [
"/*"
],
"exclude": [
"/"
]
–results in all paths being excluded.
What I expected to happen was / (index.html) would be excluded, while everything else was included. I imagine this isn't intentional, as this sounds like what should happen if both include and exclude were set to /*, except that Wrangler won't let you do that.
That being said, I cannot find a way to reference a root level index file, as / doesn't work.
I tested this both on Wrangler and deployed; the behaviour is the same.
Thanks for the report. Sounds like a bug to me! cc'ing in the experts: @jahands and @CarmenPopoviciu
@Mexican-Man thx so much for reporting this!
/ should indeed be the way to reference a root level index.
I tested this both on Wrangler and deployed
Can you please give me more details? Which wrangler commands did you try? What kind of project did you use for your test case - /functions or _worker.js? How did you deploy it?
I have tested (in MacOS) your _routes.json config in a Pages Functions project, by running:
-
wrangler pages dev[can confirm what you reported]NEEDS FIX -
wrangler pages publish[works as expected] -
git pushto GH project [works as expected]
I was using a SvelteKit project (_worker.js) trying to work on another issue with SvelteKit, where I mentioned this issue in.
Here was my process:
- Take the built file, add a
console.loginside the exported fetch. - Modify
_routes.json, load different pages/resources, see how many timesconsole.logis called. - Confirmed the behaviour using
wrangler pages dev
Of course I can't view logs once deployed, but when using wrangler pages publish, I noticed the same set of errors (in the browser) from adding and removing / from _routes.json. That being the case, I assumed that the same issue was present.
wrangler pages publish[works as expected]
@CarmenPopoviciu where you able to get / working when deployed?
EDIT: To clarify, the errors I get locally and remotely are 404 errors, implying the routes from the include rule are being overwritten by exclude.
@Mexican-Man thank you for all the details <3!
I've tested with a basic _worker.js project and I can still only confirm that wrangler pages dev is broken, but not wrangler pages publish. For your reference, you can find the test repo here: https://github.com/CarmenPopoviciu/pages-am. I'll have a stab at a SvelteKit project as well, but didn't get to it yet.
Is your project available somewhere for me to have a look? Or, compared to the test repo I mentioned above, is there anything in particular that stands out to you?
@Mexican-Man just FYI I pushed a fix for wrangler pages dev that should be available now if you update to latest wrangler version.
is wrangler pages publish still an issue for you?
Is your project available somewhere for me to have a look? Or, compared to the test repo I mentioned above, is there anything in particular that stands out to you?
Sorry. I don't have it available right now, but I could potentially put something up.
Also @CarmenPopoviciu, testing with wrangler@latest, / no longer breaks 😊. However, neither / or /index.html seem to catch requests to http://127.0.0.1:8788/ (in my case).
Hi any update on this, Im trying to deploy pages but its not respectin my _routes.json in the root directory
{
"version": 1,
"include": [
"/*"
],
"exclude": [
"/lib/*",
"/*.html",
"/index.html",
"/css/*",
"/js/*",
"/fonts/*",
"/data/*",
"/music/*",
"/libs/*",
"/img/*",
"/map42/*",
"/plugins/*",
"/pro/login3.html"
]
}
this file is generated where it removed directories until under the 99 limit theoretically there is a smarter way.
Anyway the problem is when i run the project
npx wrangler@ pages dev ./public --persist-to ./pages --ip 127.0.0.1 --port 8111 --kv kv --kv ski --log-level debug
it still runs my middleware for everything even my public assets which should be excluded
It seams easy enoug to implement
If the exclueded files bypas midalware then should work as intnteded on dev and production
psudo code
let routes = require("./_routes.json")
cloudflarePagesbackend.server() {
if(routes.excules.any(r=>matches(thisRequestUrl))) {
fallThroughToStaticPublic
}
handle functions or
fallThroughToStaticPublic
}