Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trailing / at the end of the url going to error page #127

Open
subhendukundu opened this issue Feb 18, 2021 · 5 comments
Open

Trailing / at the end of the url going to error page #127

subhendukundu opened this issue Feb 18, 2021 · 5 comments
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@subhendukundu
Copy link
Contributor

Describe the problem:

The slash at the end not auto resolving.
For example
https://cool-bio-flareact-styled.coolbio.workers.dev/subh/ will throw an error
but https://cool-bio-flareact-styled.coolbio.workers.dev/subh is accessible.

Link to a minimal reproduction:

https://github.com/subhendukundu/cool-bio-flareact-styled

@jplhomer jplhomer added good first issue Good for newcomers help wanted Extra attention is needed labels Feb 19, 2021
@iamspark1e
Copy link
Contributor

It seems a problem caused by @cloudflare/kv-asset-handler, at this line :
https://github.com/cloudflare/kv-asset-handler/blob/3228cd78de1f4f61d74cecb13a3b19054d67d501/src/index.ts#L18

Any pathname with trailing / will make kv-asset-handler to find the index.html with the pathname.

@traffisco
Copy link
Contributor

Trailing / could be auto stripped

url.pathname = url.pathname.replace(prefix, "/");

@cwalton
Copy link

cwalton commented Jun 18, 2021

@jplhomer i'm an amateur, so be gentle if this is wrong... this is also my first contribution? to an open source project...

In tracing the code, it looks like the bad assumption is definitely in kv-asset-handler as mentioned by @arctome , but the fix is elsewhere.

Moreover, I don't think the fix is in worker.js, as that really only fires if the page is not found... we need to make the page get found, so that kv-asset-handler never sees the request.

In order to do this, we need to update worker/index.js 's handleRequest function to strip trailing bad characters thus making the call succeed so that the fallback function is never called.

Since I'm such an amateur, I don't know how to do that, so I patched the input to that function in my root /index.js. The way I did it was to add a utlity function that strips slashes, which I used against the event<.request.url>, which I then passed to handleEvent as before.

It works with any number of trailing slashes on root and dynamic routes. There may be cases that don't work... and there may be other characters that should be stripped... The regex can be updated to deal with any other bad actors in the url.

here's my gist.
https://gist.github.com/cwalton/429bac899efa0a2a4cb85d3ef641fe1d

@cwalton
Copy link

cwalton commented Jun 18, 2021

Would need a similar fix for the API routes.

@joelnet
Copy link

joelnet commented Nov 2, 2021

Just ran into this. Intermittent. If I go to the URL with no slash, then back to the URL with a slash... it'll load fine. But initial load with / fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

6 participants