Deploy on GitHub Pages
Intro
GitHub Pages is a pretty easy way to have a running and available website. The limitation is that only static websites can be hosted.
PWAs created with go-app are usually served by a standard Go HTTP server. That said, it is possible to generate them as completely static websites in order to have them running from hosts like GitHub Pages.
Generate a Static Website
A static website can be generated with the help of the GenerateStaticWebsite() function:
func main() {
app.Route("/", &hello{})
app.Route("/hello"), &hello{})
app.RunWhenOnBrowser()
err := app.GenerateStaticWebsite(".", &app.Handler{
Name: "Hello",
Description: "An Hello World! example",
})
if err != nil {
log.Fatal(err)
}
}
It can then be generated by building and running the go executable:
# Build app.wasm:
GOARCH=wasm GOOS=js go build -o /test-app/web/app.wasm
# Build and generate static website:
go build
./hello
The current directory will then have all the required files to run the PWA as a static website:
. # Current dir.
├── app-worker.js # Service-worker file (Generated).
├── app.js # Js support file (Generated).
├── index.html # Index page (Generated from "/").
├── hello.html # hello page (Generated from "/hello").
├── manifest.webmanifest # PWA manifest (Generated).
├── wasm_exec.js # Wasm support file (Generated).
└── web # Web directory.
└── app.wasm # Wasm app (Manually built).
Deployment
Once generated, the static website can directly be dropped in a GitHub repository, either in the root or in the docs
directory depending on how GitHub Pages is configured.
Domainless Repository
By default, no domain is associated with a GitHub repository. The website is accessed from a GitHub address that looks like this:
https://USERNAME.github.io/REPOSITORY_NAME
In that scenario, the Handler resource provider must be changed to be a GitHubPages provider:
func main() {
app.Route("/", &hello{})
app.Route("/hello"), &hello{})
app.RunWhenOnBrowser()
err := app.GenerateStaticWebsite(".", &app.Handler{
Name: "Hello",
Description: "An Hello World! example",
Resources: app.GitHubPages("REPOSITORY_NAME"),
})
if err != nil {
log.Fatal(err)
}
}
This will fix static resources issues caused by GitHub adding the repository name as an URL path prefix.