goStatic icon indicating copy to clipboard operation
goStatic copied to clipboard

Fallback for multiple subdirectories

Open stephanrenggli opened this issue 3 years ago • 4 comments

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?

stephanrenggli avatar Oct 14 '22 00:10 stephanrenggli

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

PierreZ avatar Oct 17 '22 07:10 PierreZ

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

stephanrenggli avatar Oct 17 '22 21:10 stephanrenggli

Feel free to ping me if needed

PierreZ avatar Oct 18 '22 07:10 PierreZ

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:

  • Add another parameter (--redirect) which would allow to redirect everything to another URL or just the root of the current URL. But since goStatic only serves static files, redirect doesn't seem like the right idea. Also there would be the issue of circular redirects resulting in a TOO_MANY_REDIRECTS error.
  • Modify the request path to always serve the right file. Here I had the same problem of infinite redirects as I can't really hardcode any paths (in case css/js/images had subdirectories).
  • One option I can think of which I haven't tried yet would be to specify a directory with --fallback, scan the directory and only rewrite requests which don't match that structure.

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.

stephanrenggli avatar Oct 20 '22 14:10 stephanrenggli