PierreZ / goStatic

A really small static web server for Docker
GNU General Public License v2.0
387 stars 76 forks source link

Fallback for multiple subdirectories #56

Open stephanrenggli opened 1 year ago

stephanrenggli commented 1 year ago

I use goStatic in a docker/traefik setup where goStatic serves as errorpages and a catch-all. So any route that isn't matched by traefik will get routed to goStatic. The setup works well.

Traefik will handle: doesnotexist.example.com The goStatic fallback will handle: doesnotexist.example.com/doesnotexist

The only case which currently isn't being handled nicely is: doesnotexist.example.com/doesnotexist/doesnotexist (or even more subdirs). If there are multiple subdirectories the html page will be shown, but any css/js/images will fail to load.

Refused to apply style from 'https://doesnotexist.example.com/doesnotexist/css/404.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
Refused to execute script from 'https://doesnotexist.example.com/doesnotexist/js/tsparticles.bundle.min.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
Refused to execute script from 'https://doesnotexist.example.com/doesnotexist/js/404.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
Refused to apply style from 'https://doesnotexist.example.com/doesnotexist/css/404.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

I have tried both setting -fallback /index.html and -fallback index.html, the result seems to be the same. My file structure inside the container looks as follows:

/srv/
├─ http/
│  ├─ css/
│  │  ├─ 404.css
│  ├─ images/
│  │  ├─ image.svg
│  ├─ js/
│  │  ├─ 404.js
│  │  ├─ tsparticles.bundle.min.js
│  ├─ index.html

Is there a way to redirect all requests to a specified path and not just one level of subdirectories? Am I missing something else?

PierreZ commented 1 year ago

Hi :wave: Thanks for the report, I don't think that is supported (yet) in goStatic. Would you like to contribute back this feature?

stephanrenggli commented 1 year ago

Yes, will try to get it working on the weekend.

PierreZ commented 1 year ago

Feel free to ping me if needed

stephanrenggli commented 1 year ago

So I tried a few things but I haven't found a solution that I think is good/works yet. Here's the ideas I had:

But I feel like I may be way off path, so hints would be appreciated.

Edit: Might have a new idea:

Split requestPath at '/' and remove parts of the requestPath until the requestPath exists on the filesystem. This way nothing would have to be hardcoded and and as soon as the requested file exists it could be served.

Lets say the request is example.com/test/doesnotexist/css/404.css. On the first iteration the file requested would be /test/doesnotexist/css/404.css. On the second iteration the file requested would be /doesnotexist/css/404.css. On the third iteration the file requested would be /css/404.css. This file actually exists and could be served.