serve service-worker.js on page refresh
This commit is contained in:
parent
0826963742
commit
b36ccae974
1 changed files with 12 additions and 10 deletions
22
api/api.go
22
api/api.go
|
|
@ -2,6 +2,7 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -97,17 +98,18 @@ func corsConfig() *cors.Cors {
|
||||||
// SPAHandler serves the public Single Page Application.
|
// SPAHandler serves the public Single Page Application.
|
||||||
func SPAHandler(publicDir string) http.HandlerFunc {
|
func SPAHandler(publicDir string) http.HandlerFunc {
|
||||||
handler := http.FileServer(http.Dir(publicDir))
|
handler := http.FileServer(http.Dir(publicDir))
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
indexPage := path.Join(publicDir, "index.html")
|
||||||
|
serviceWorker := path.Join(publicDir, "service-worker.js")
|
||||||
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
requestedAsset := path.Join(publicDir, r.URL.Path)
|
||||||
url := r.URL.String()
|
if strings.Contains(requestedAsset, "service-worker.js") {
|
||||||
|
requestedAsset = serviceWorker
|
||||||
// serve static files
|
}
|
||||||
if strings.Contains(url, ".") || url == "/" {
|
if _, err := os.Stat(requestedAsset); err != nil {
|
||||||
handler.ServeHTTP(w, r)
|
http.ServeFile(w, r, indexPage)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
handler.ServeHTTP(w, r)
|
||||||
// otherwise always serve index.html
|
}
|
||||||
http.ServeFile(w, r, path.Join(publicDir, "/index.html"))
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue