Skip to content

Commit

Permalink
feat: add caching to application js bundle since it has a unique name (
Browse files Browse the repository at this point in the history
…#9008)

* feat: add caching to application js bundle since it has a unique name

Signed-off-by: Jonah Back <[email protected]>

* chore: simplify check and add tests

Signed-off-by: Michael Crenshaw <[email protected]>

Co-authored-by: Michael Crenshaw <[email protected]>
  • Loading branch information
backjo and crenshaw-dev authored Apr 7, 2022
1 parent 5c2391f commit 14cdfc7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
11 changes: 11 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/url"
"os"
"os/exec"
"path"
"reflect"
"regexp"
go_runtime "runtime"
Expand Down Expand Up @@ -910,11 +911,21 @@ func (server *ArgoCDServer) newStaticAssetsHandler() func(http.ResponseWriter, *
}
http.ServeContent(w, r, "index.html", modTime, io.NewByteReadSeeker(data))
} else {
if isMainJsBundle(r.URL) {
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
}
http.FileServer(server.staticAssets).ServeHTTP(w, r)
}
}
}

var mainJsBundleRegex = regexp.MustCompile(`^main\.[0-9a-f]{20}\.js$`)

func isMainJsBundle(url *url.URL) bool {
filename := path.Base(url.Path)
return mainJsBundleRegex.Match([]byte(filename))
}

type registerFunc func(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) error

// mustRegisterGWHandler is a convenience function to register a gateway handler
Expand Down
49 changes: 49 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -673,3 +674,51 @@ func TestOIDCConfigChangeDetection_NoChange(t *testing.T) {
//Then
assert.Equal(t, result, false, "no error since no config change")
}

func TestIsMainJsBundle(t *testing.T) {
testCases := []struct{
name string
url string
isMainJsBundle bool
}{
{
name: "localhost with valid main bundle",
url: "https://localhost:8080/main.e4188e5adc97bbfc00c3.js",
isMainJsBundle: true,
},
{
name: "localhost and deep path with valid main bundle",
url: "https://localhost:8080/some/argo-cd-instance/main.e4188e5adc97bbfc00c3.js",
isMainJsBundle: true,
},
{
name: "font file",
url: "https://localhost:8080/assets/fonts/google-fonts/Heebo-Bols.woff2",
isMainJsBundle: false,
},
{
name: "no dot after main",
url: "https://localhost:8080/main/e4188e5adc97bbfc00c3.js",
isMainJsBundle: false,
},
{
name: "wrong extension character",
url: "https://localhost:8080/main.e4188e5adc97bbfc00c3/js",
isMainJsBundle: false,
},
{
name: "wrong hash length",
url: "https://localhost:8080/main.e4188e5adc97bbfc00c3abcdefg.js",
isMainJsBundle: false,
},
}
for _, testCase := range testCases {
testCaseCopy := testCase
t.Run(testCaseCopy.name, func(t *testing.T) {
t.Parallel()
testUrl, _ := url.Parse(testCaseCopy.url)
isMainJsBundle := isMainJsBundle(testUrl)
assert.Equal(t, testCaseCopy.isMainJsBundle, isMainJsBundle)
})
}
}

0 comments on commit 14cdfc7

Please sign in to comment.