-
|
I use the Static Middleware to serve a Vue Frontend which is embedded. Is there a way to add some data to the index.html? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
The static middleware serves files as-is, so it won't help with injecting dynamic data. What you want is to handle Here's how I'd approach it: //go:embed all:dist
var staticFiles embed.FS
func main() {
e := echo.New()
// Serve index.html with dynamic data
e.GET("/", func(c echo.Context) error {
// Read the embedded index.html
content, err := staticFiles.ReadFile("dist/index.html")
if err != nil {
return err
}
// Inject your data (e.g., replace a placeholder)
html := strings.Replace(
string(content),
"<!--APP_CONFIG-->",
`<script>window.APP_CONFIG = {"apiUrl": "/api", "version": "1.0.0"}</script>`,
1,
)
return c.HTML(http.StatusOK, html)
})
// Serve other static assets normally
assetHandler := echo.WrapHandler(
http.FileServer(http.FS(staticFiles)),
)
e.GET("/dist/*", assetHandler)
e.Start(":8080")
}In your Vue <head>
<!--APP_CONFIG-->
</head>Then in your Vue app, access it via Alternatively, if you need more complex templating, you could use Go's |
Beta Was this translation helpful? Give feedback.
Do you want to create SPA (single-page-application) where application checks if request path exists and serves that static file or if it does not will reserve index.html as fallback?
There are many ways to achieve this, one is to have Filesystem that falls back to index.html which is served from in-memory fs