Serving a built app
GoSPA, the
gospa package, serves two kinds of files from your program:
- a built single-page app, such as an admin built with Vite
- a public folder, such as the files people upload
A build tool turns a single-page app into one page, index.html, and a
folder of scripts, styles and fonts. The browser loads the page once,
and the app’s own router shows a screen for each address. So every
address the app routes must get the page, not a 404.
Go’s http.FileServer sends files, but it lists folders, sends hidden
files such as .env and sets no cache rules. GoSPA adds those rules.
If you know Laravel, GoSPA does the job Nginx does in front of a Laravel app.
Serve a built app
Section titled “Serve a built app”//go:embed all:distvar dist embed.FS
build, err := fs.Sub(dist, "dist")if err != nil { return err}admin, err := gospa.App(build, gospa.AppOptions{})if err != nil { return err}mux.Handle("/admin/", http.StripPrefix("/admin", admin))App fails when the build holds no index.html. Then each path gets:
| The path | The answer |
|---|---|
| a file of the build | the file |
index.html |
the page |
under assets/, no file |
not found |
| anywhere else, no file | the page |
Only the page asks for the files under assets/, so a missing one is
an error. An old page that asks for a file a new build replaced gets a
404 it can handle, never HTML it would run as a script.
The page is read again each time it is sent. So a build read from
disk with os.DirFS shows a new build without a restart. An embedded
build, as above, changes only when the program is built again and
restarted.
How long a browser keeps a file
Section titled “How long a browser keeps a file”Vite names each file it builds under assets/ after a hash of its
content, such as assets/index-DWLUpakq.js. When the content changes,
the name does too. So a browser may keep such a file for a long time
without asking. GoSPA counts a file as hashed when it sits under
assets/ and its name ends in a dash, 8 letters, digits, dashes or
underscores, and an extension. HashedMaxAge sets how long, one year
when you leave it at zero.
The page and every other file get no-cache. The browser keeps them
but checks each one with your program on every load. While nothing
changed, the check costs a 304 with no body.
Pass values to the app
Section titled “Pass values to the app”Your app may need a value before it asks your API anything, such as
where the API lives. Give it as a Meta:
admin, err := gospa.App(build, gospa.AppOptions{ Meta: []gospa.Meta{{Name: "api-root", Content: "/api/"}},})GoSPA writes it into the page as a meta element, right where the head opens. The app reads it like this:
const apiRoot = document.querySelector('meta[name="api-root"]').contentServe a public folder
Section titled “Serve a public folder”root, err := os.OpenRoot("uploads")if err != nil { return err}mux.Handle("/media/", http.StripPrefix("/media", gospa.Files(root.FS(), gospa.FilesOptions{})))Files sends each file by its path. Everything else answers not found:
a missing file, a folder, or a folder’s index.html unless asked for
by name. Use os.OpenRoot for a folder on disk. Unlike os.DirFS, it
never follows a link out of the folder.
An uploaded HTML or SVG file can run scripts on your site. Serve uploads from another domain, or check what people upload.
Rules both share
Section titled “Rules both share”- Only GET and HEAD are answered.
- A path part that starts with a dot, such as
.envor.git, answers not found..well-knownis the one exception. - A path part with a tilde, or ending in a dot or a space, answers not
found too, since Windows reads
GIT~1as.git. - A file that is there but cannot be opened answers 403 when it may not be read, and 500 for any other failure. It never gets the page.
- The type of a file comes from its extension, the same on every system. An extension GoSPA does not know makes the browser download the file.
NotFoundin the options replaces the plain 404 answer.