-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
301 status and ERR_TOO_MANY_REDIRECTS for static dir #20
Comments
You can verify this by changing this line in file-server example: bunrouter/example/file-server/main.go Line 26 in 4026a11
to router.GET("/*path", bunrouter.HTTPHandler(fileServer)) |
Please try v1.0.2 and the updated example. |
It works but I'm getting this error now with v1.0.2:
I think because of these lines: router.GET("/", func(w http.ResponseWriter, r bunrouter.Request) error {
serveHTMLFallback(w, r.Request, fsys)
return nil
})
const indexHTML = "index.html"
func serveHTMLFallback(w http.ResponseWriter, r *http.Request, fsys fs.FS) {
indexHTMLFile, err := fsys.Open(indexHTML)
if err != nil {
panic(err)
}
indexHTMLFileStat, err := indexHTMLFile.Stat()
if err != nil {
panic(err)
}
indexHTMLModTime := indexHTMLFileStat.ModTime()
if err = indexHTMLFile.Close(); err != nil {
panic(err)
}
http.ServeContent(w, r, indexHTML, indexHTMLModTime, indexHTMLFile.(io.ReadSeekCloser))
}
func fileServer(router *bunrouter.Router) {
fsys := getStaticDirAsFileSystem()
router.GET("/", func(w http.ResponseWriter, r bunrouter.Request) error {
serveHTMLFallback(w, r.Request, fsys)
return nil
})
router.GET("/*path", func(w http.ResponseWriter, r bunrouter.Request) error {
// From https://www.alexedwards.net/blog/disable-http-fileserver-directory-listings#using-middleware
if r.URL.Path != "/" && r.URL.Path[len(r.URL.Path)-1] == '/' {
serveHTMLFallback(w, r.Request, fsys)
return nil
}
if _, err := fs.Stat(fsys, r.URL.Path[1:]); err != nil {
serveHTMLFallback(w, r.Request, fsys)
return err
}
http.FileServer(http.FS(fsys)).ServeHTTP(w, r.Request)
return nil
})
}
router := bunrouter.New()
fileServer(router) |
I will fix this shortly. |
Ok. I'm interested in your opinion on that code to serve the files of a SPA (single page application). |
I think it is ok without that first |
If you think about it, we don't need two routes that handle the same calls:
This is redundant! Only the latter is ok: I personally don't like writing |
Okay, let's keep this behavior. I can imagine arguments against such routing too, but perhaps it is fine.
That is is needed so you can retrieve the param via |
Looks good enough. I will try to add an example later. |
Yeah I think would be awesome to have a SPA file server example. |
I have these dirs:
and I'm using the code listed here: https://bunrouter.uptrace.dev/guide/serving-static-files.html#fs-sub.
Single file retrieving is working fine (eg.
static/js/main.js
).The issue I'm having is when I try to open
static/
. I get in browser a 301 forstatic/
tostatic
and vice-versa and thenERR_TOO_MANY_REDIRECTS
in Chrome.Is this my fault?
The text was updated successfully, but these errors were encountered: