Просмотр исходного кода

client/web: server /index.html on 404 requests

In production, the asset handler is receiving requests for pages like
/details, which results in a 404. Instead, if we know the requested file
does not exist, serve the main index page and let wouter route it
appropriately on the frontend.

Updates tailscale/corp/#14335

Signed-off-by: Will Norris <[email protected]>
Will Norris 2 лет назад
Родитель
Сommit
fb984c2b71
1 измененных файлов с 14 добавлено и 1 удалено
  1. 14 1
      client/web/assets.go

+ 14 - 1
client/web/assets.go

@@ -4,6 +4,7 @@
 package web
 
 import (
+	"io/fs"
 	"log"
 	"net/http"
 	"net/http/httputil"
@@ -22,7 +23,19 @@ func assetsHandler(devMode bool) (_ http.Handler, cleanup func()) {
 		cleanup := startDevServer()
 		return devServerProxy(), cleanup
 	}
-	return http.FileServer(http.FS(prebuilt.FS())), nil
+
+	fsys := prebuilt.FS()
+	fileserver := http.FileServer(http.FS(fsys))
+	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		_, err := fs.Stat(fsys, strings.TrimPrefix(r.URL.Path, "/"))
+		if os.IsNotExist(err) {
+			// rewrite request to just fetch /index.html and let
+			// the frontend router handle it.
+			r = r.Clone(r.Context())
+			r.URL.Path = "/"
+		}
+		fileserver.ServeHTTP(w, r)
+	}), nil
 }
 
 // startDevServer starts the JS dev server that does on-demand rebuilding