- With issues:
- Use the search tool before opening a new issue.
- Please provide source code and commit sha if you found a bug.
- Review existing issues and provide feedback or react to them.
Description
Whenever I reload my web app that is being served from Gin, using React and react router, it always returns "404 not found".
How to reproduce
package main
import (
"github.com/gin-gonic/gin"
)
//go:embed dist
var web embed.FS
func main() {
// ...
// WEB ---------
viteStaticFS, err := fs.Sub(web, "dist")
if err != nil {
panic(err)
}
s.engine.NoRoute(func(context *gin.Context) {
if !strings.HasPrefix(context.Request.URL.Path, "/api") {
context.FileFromFS(context.Request.URL.Path, http.FS(viteStaticFS))
}
})
}
Expectations
It should refresh and the web app content should show.
## Actual result
"404 not found", but the url is atleast correct.
## Environment
- go version: 1.19
- gin version (or commit ref): v1.9.1
- operating system: MacOS
**Comment From: zanmato**
Your `NoRoute` should serve `index.html`, because some virtual route like `/hello/world` doesn't exist in your `dist` folder, but it might exist in your react router. In addition you should serve a StaticFS for your `/static` (or whatever it is called) folder that contains the compiled js and css
**Comment From: itschip**
Yeah, this did the trick
```go
s.engine.NoRoute(func(c *gin.Context) {
if strings.HasPrefix(c.Request.RequestURI, "/assets") {
c.FileFromFS(c.Request.URL.Path, http.FS(viteStaticFS))
return
}
if !strings.HasPrefix(c.Request.RequestURI, "/api") {
c.FileFromFS("", http.FS(viteStaticFS))
return
}
})
Comment From: zanmato
Great! Don't forget to close the issue