picosh / pico

hacker labs - open source and managed web services leveraging SSH
https://pico.sh
MIT License
741 stars 25 forks source link

pgs.sh - relative links in subfolders with and without trailing / #115

Closed josherrickson closed 3 months ago

josherrickson commented 3 months ago

I've worked up a sample project at https://josherrickson-test.pgs.sh

The project consists of the following folder structure:

index.html
cat.png
folder/
├─ index.html
├─ dog.jpg

Both index.html link to both images using relative paths:

    <p>
      Cat: <img src="cat.png">
    </p>

    <p>
      Dog: <img src="dog.jpg">
    </p>

If I access https://josherrickson-test.pgs.sh, I properly see the cat image and the dog image is missing.

If I access https://josherrickson-test.pgs.sh/folder/index.html, I properly see the dog image and the cat is missing.

If I access https://josherrickson-test.pgs.sh/folder/, I properly see the dog image and the cat is missing

However, if I access https://josherrickson-test.pgs.sh/folder (note the missing trailing "/"), I DO see the cat image but do NOT see the dog image.

I'm uncertain if this is an issue on my end or pgs.sh's end. This issue also arises for CSS files (that's where I originally noticed it) but made this example with images to eliminate being some CSS weirdness.

josherrickson commented 3 months ago

I should add, I've done some reading about how the presence/lack of trailing slash has a semantic meaning, but in previous hosts I've used, they've not been distinguishable.

josherrickson commented 3 months ago

I tried adding this to _redirects but it didn't appear to work.

/folder        /folder/

Additionally, it would be quite a pain to have to manually add redirects for every folder if this were the solution. If this could be made to work in _redirects, does _redirects support regex?

neurosnap commented 3 months ago

Hi! This is quite surprising behavior, but regardless of whether you add or remove the trailing slash to /folder, the page's content is identical, so this feels like what you mentioned: trailing slash is technically treated differently by the browser in regards to relative links.

I took at look at the default behavior for the python http.server using your site:

python3 -m http.server

The problem you described does not happen in it because when a user goes to: localhost:8000/folder it performs a 301 Moved Permanently to localhost:8000/folder/. It seems like even django enables this functionality by default: https://docs.djangoproject.com/en/1.10/ref/settings/#append-slash

Netlify also enabled this 301 redirect by default: https://docs.netlify.com/routing/redirects/redirect-options/#trailing-slash

The default behavior for pgs should not be surprising, so I have a feeling we are going to want to setup the 301 as well. Thoughts?

neurosnap commented 3 months ago

I have a PR up, gonna test today/tomorrow and then deploy.

josherrickson commented 3 months ago

Thanks - I agree that the 301 looks like the solution that most hosts land on. Appreciate the quick fix!