Skip to content

chore: embed build info in the html to reduce requests #6605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<meta property="og:type" content="website" />
<meta property="csp-nonce" content="{{ .CSP.Nonce }}" />
<meta property="csrf-token" content="{{ .CSRF.Token }}" />
<meta property="build-info" content="{{ .BuildInfo }}" />
<!-- We need to set data-react-helmet to be able to override it in the workspace page -->
<link
rel="alternate icon"
Expand Down
32 changes: 24 additions & 8 deletions site/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"crypto/sha1" //#nosec // Not used for cryptography.
_ "embed"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"html"
htmltemplate "html/template"
"io"
"io/fs"
Expand All @@ -28,6 +30,7 @@ import (
"golang.org/x/sync/singleflight"
"golang.org/x/xerrors"

"github.com/coder/coder/buildinfo"
"github.com/coder/coder/coderd/httpapi"
"github.com/coder/coder/codersdk"
)
Expand Down Expand Up @@ -102,10 +105,20 @@ func Handler(siteFS fs.FS, binFS http.FileSystem, binHashes map[string]string) h
})))
mux.Handle("/", http.FileServer(http.FS(siteFS))) // All other non-html static files.

buildInfo := codersdk.BuildInfoResponse{
ExternalURL: buildinfo.ExternalURL(),
Version: buildinfo.Version(),
}
buildInfoResponse, err := json.Marshal(buildInfo)
if err != nil {
panic("failed to marshal build info: " + err.Error())
}

return secureHeaders(&handler{
fs: siteFS,
htmlFiles: files,
h: mux,
fs: siteFS,
htmlFiles: files,
h: mux,
buildInfoJSON: html.EscapeString(string(buildInfoResponse)),
})
}

Expand All @@ -117,8 +130,9 @@ type handler struct {
// scripts, and that nonce is passed through a template.
// We only do this for html files to reduce the amount of in memory caching
// of duplicate files as `fs`.
htmlFiles *htmlTemplates
h http.Handler
htmlFiles *htmlTemplates
h http.Handler
buildInfoJSON string
}

// filePath returns the filepath of the requested file.
Expand All @@ -138,8 +152,9 @@ func (h *handler) exists(filePath string) bool {
}

type htmlState struct {
CSP cspState
CSRF csrfState
CSP cspState
CSRF csrfState
BuildInfo string
}

type cspState struct {
Expand Down Expand Up @@ -184,7 +199,8 @@ func (h *handler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
reqFile := filePath(req.URL.Path)
state := htmlState{
// Token is the CSRF token for the given request
CSRF: csrfState{Token: nosurf.Token(req)},
CSRF: csrfState{Token: nosurf.Token(req)},
BuildInfo: h.buildInfoJSON,
}

// First check if it's a file we have in our templates
Expand Down
15 changes: 14 additions & 1 deletion site/src/xServices/buildInfo/buildInfoXService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,20 @@ export const buildInfoMachine = createMachine(
},
{
services: {
getBuildInfo: API.getBuildInfo,
getBuildInfo: async () => {
// Build info is injected by the Coder server into the HTML document.
const buildInfo = document.querySelector("meta[property=build-info]")
if (buildInfo) {
const rawContent = buildInfo.getAttribute("content")
try {
return JSON.parse(rawContent as string)
} catch (ex) {
// Ignore this and fetch as normal!
Copy link
Member

@Emyrk Emyrk Mar 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should you put some "unknown" value here? Or at least console.Log so we know something happened?

Ah we defer to api. Got it

}
}

return API.getBuildInfo()
},
},
actions: {
assignBuildInfo: assign({
Expand Down