How to get Echo to work with HTML5 pushState routing for web apps? #2662
-
A web app server should work as follows:
// Serve static files
e.Static("/", "public")
// API routes
e.GET("/api/hello", hello)
// Catch-all route to serve index.html for HTML5 pushstate
e.GET("/*", func(c echo.Context) error {
return c.File("public/index.html")
}) Unfortunately this doesn't work. Seems that Echo ignores the order of route definitions and the So how do we get Echo to be able to serve a web app with client side routing? |
Beta Was this translation helpful? Give feedback.
Answered by
aldas
Jul 31, 2024
Replies: 1 comment 1 reply
-
I fixed it by using // Catch-all for GET requests to handle 404 and redirect to public/index.html
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
err := next(c)
if he, ok := err.(*echo.HTTPError); ok {
if he.Code == http.StatusNotFound {
return c.File("public/index.html")
}
}
return err
}
}) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Static middleware is probably something that you seek. See this example: https://echo.labstack.com/docs/middleware/static#example-2