|
| 1 | +# Setting Up Your Frontend |
| 2 | + |
| 3 | +You have two options for integrating your frontend with the Go backend: |
| 4 | + |
| 5 | +1. Keep frontend inside this repo |
| 6 | + |
| 7 | +- Create your frontend project inside the `web` folder. |
| 8 | +- Do not modify `web/dist` directly - Go requires it to exist. |
| 9 | +- Configure your frontend to output static files to `web/dist`, replacing the default one. |
| 10 | +- If your build output directory is not `dist`, update it in `build_prod.go`. |
| 11 | + |
| 12 | +2. Keep frontend in a separate repo |
| 13 | + |
| 14 | +- Build the frontend in a CI pipeline (examples given below). |
| 15 | +- Merge the generated `dist` folder into `web/dist`. |
| 16 | + |
| 17 | +## Examples |
| 18 | +- [Basic Github Actions](#) |
| 19 | +- [Docker Github Actions](#) |
| 20 | +- [Dockerfile](#) |
| 21 | + |
| 22 | +> **TODO: will update later.** |
| 23 | +
|
| 24 | +# Serving a Separate Frontend with Go |
| 25 | + |
| 26 | +Frontend frameworks like React, Vue, Svelte, etc. generate static assets (HTML, CSS, JS), which can be served directly from a Go backend. This simplifies deployment and avoids CORS issues. |
| 27 | + |
| 28 | +# How It Works |
| 29 | + |
| 30 | +- The frontend build (`build`/`dist` folder) is embedded in Go using `embed.FS`. |
| 31 | +- If a requested file exists, it's served normally. |
| 32 | +- If not, Go serves `index.html` (for SPAs) or a dedicated error page if available (for SSGs). |
| 33 | + |
| 34 | +# SPA vs. SSG Behavior |
| 35 | + |
| 36 | +- **SPA (Single Page App)**: Always fallbacks to `index.html`, and routing/errors are handled in JavaScript. |
| 37 | +- **SSG (Static Site Generation)**: If a `404.html` or similar page exists, serve that instead of `index.html`. |
| 38 | + |
| 39 | +# Configuring Fallback Pages (if needed) |
| 40 | + |
| 41 | +Some SSG frameworks may generate a `404.html` for handling missing pages. To serve it properly: |
| 42 | + |
| 43 | +**Echo Example** |
| 44 | +```go |
| 45 | +// Todo: will fix this later. Echo doesn't have an option to specify a fallback. |
| 46 | +e.Use(middleware.StaticWithConfig(middleware.StaticConfig{ |
| 47 | + Root: "web/dist", |
| 48 | + Index: "index.html", |
| 49 | + Filesystem: http.FS(web), |
| 50 | +})) |
| 51 | +``` |
| 52 | + |
| 53 | +**Fiber Example** |
| 54 | +```go |
| 55 | +app.Use(filesystem.New(filesystem.Config{ |
| 56 | + Root: http.FS(subFS), |
| 57 | + Browse: false, |
| 58 | + Index: "index.html", |
| 59 | + NotFoundFile: "404.html", // Change this |
| 60 | +})) |
| 61 | +``` |
| 62 | + |
| 63 | +**Chi Example** |
| 64 | +```go |
| 65 | +mux.Handle("/*", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 66 | + path := strings.TrimPrefix(r.URL.Path, "/") |
| 67 | + |
| 68 | + // Check if the requested file exists |
| 69 | + _, err := subFS.Open(path) |
| 70 | + if err != nil { |
| 71 | + // If not found, serve fallback page. |
| 72 | + http.ServeFileFS(w, r, subFS, "404.html") // Change this |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + fs.ServeHTTP(w, r) |
| 77 | +})) |
| 78 | +``` |
0 commit comments