From 52d769d35c971bae39ee2bbbd27b130382555489 Mon Sep 17 00:00:00 2001 From: Mihai Parparita Date: Tue, 2 Aug 2022 15:02:26 -0700 Subject: [PATCH] cmd/tsconnect: prefetch main.wasm when serving Avoids waterfalling of requests from the file (its load is triggered from JavaScript). Also has other cleanups to index.html, adding a and moving the <script> to being loaded sooner (but still not delaying page rendering by using the defer attribute). Fixes #5141 Fixes #5135 Signed-off-by: Mihai Parparita <mihai@tailscale.com> --- cmd/tsconnect/common.go | 3 +++ cmd/tsconnect/index.html | 3 ++- cmd/tsconnect/serve.go | 13 +++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/cmd/tsconnect/common.go b/cmd/tsconnect/common.go index efe35460f..a3941e614 100644 --- a/cmd/tsconnect/common.go +++ b/cmd/tsconnect/common.go @@ -119,6 +119,9 @@ func runYarn(args ...string) error { // from entry points to hashed file names. type EsbuildMetadata struct { Outputs map[string]struct { + Inputs map[string]struct { + BytesInOutput int64 `json:"bytesInOutput"` + } `json:"inputs,omitempty"` EntryPoint string `json:"entryPoint,omitempty"` } `json:"outputs,omitempty"` } diff --git a/cmd/tsconnect/index.html b/cmd/tsconnect/index.html index ba9a530f6..0d78a1cec 100644 --- a/cmd/tsconnect/index.html +++ b/cmd/tsconnect/index.html @@ -3,7 +3,9 @@ <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> + <title>Tailscale Connect +
@@ -38,6 +40,5 @@ enabled. Give it a try!
- diff --git a/cmd/tsconnect/serve.go b/cmd/tsconnect/serve.go index da9878bf5..53100c090 100644 --- a/cmd/tsconnect/serve.go +++ b/cmd/tsconnect/serve.go @@ -83,10 +83,19 @@ func generateServeIndex(distFS fs.FS) ([]byte, error) { return nil, fmt.Errorf("Could not parse esbuild-metadata.json: %w", err) } entryPointsToHashedDistPaths := make(map[string]string) + mainWasmPath := "" for outputPath, output := range esbuildMetadata.Outputs { if output.EntryPoint != "" { entryPointsToHashedDistPaths[output.EntryPoint] = path.Join("dist", outputPath) } + if path.Ext(outputPath) == ".wasm" { + for input := range output.Inputs { + if input == "src/main.wasm" { + mainWasmPath = path.Join("dist", outputPath) + break + } + } + } } indexBytes := rawIndexBytes @@ -96,6 +105,10 @@ func generateServeIndex(distFS fs.FS) ([]byte, error) { indexBytes = bytes.ReplaceAll(indexBytes, []byte(defaultDistPath), []byte(hashedDistPath)) } } + if mainWasmPath != "" { + mainWasmPrefetch := fmt.Sprintf("\n", mainWasmPath) + indexBytes = bytes.ReplaceAll(indexBytes, []byte(""), []byte(mainWasmPrefetch)) + } return indexBytes, nil }