Skip to content

Commit

Permalink
Fingerprint each spa file to break cache (#320).
Browse files Browse the repository at this point in the history
  • Loading branch information
jimafisk committed May 2, 2024
1 parent 8a2457c commit d84c8e0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
6 changes: 3 additions & 3 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,19 @@ func Build() error {
}

// Run Gopack (custom Snowpack alternative) on app for ESM support.
err = build.Gopack(buildPath, spaPath, spaPath+"core/main.js")
err = build.Gopack(buildPath, spaPath, spaPath+"core/main.js", siteConfig.Fingerprint)
if err != nil {
log.Fatal("\nError in Gopack main.js build step", err)
}

// Run Gopack (custom Snowpack alternative) on dynamically imported adminMenu.
err = build.Gopack(buildPath, spaPath, spaPath+"core/cms/admin_menu.js")
err = build.Gopack(buildPath, spaPath, spaPath+"core/cms/admin_menu.js", siteConfig.Fingerprint)
if err != nil {
log.Fatal("\nError in Gopack admin_menu.svelte build step", err)
}

// Run Gopack manually on dynamic imports
err = build.GopackDynamic(buildPath, spaPath)
err = build.GopackDynamic(buildPath, spaPath, siteConfig.Fingerprint)
if err != nil {
log.Fatal("\nError in GopackDynamic build step", err)
}
Expand Down
16 changes: 10 additions & 6 deletions cmd/build/gopack.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var (
var alreadyConvertedFiles []string

// Gopack ensures ESM support for NPM dependencies.
func Gopack(buildPath, spaPath, entrypoint string) error {
func Gopack(buildPath, spaPath, entrypoint, fingerprint string) error {

defer Benchmark(time.Now(), "Running Gopack")

Expand All @@ -54,18 +54,20 @@ func Gopack(buildPath, spaPath, entrypoint string) error {
alreadyConvertedFiles = []string{}

// Start at the entry point for the app
err := runPack(buildPath, spaPath, entrypoint)
err := runPack(buildPath, spaPath, entrypoint, fingerprint)
if err != nil {
return err
}

return nil
}

func runPack(buildPath, spaPath, convertPath string) error {
func runPack(buildPath, spaPath, convertPath, fingerprint string) error {
// Destination path for dependencies
gopackDir := spaPath + "web_modules"

// Remove query string to read file
convertPath = strings.Split(convertPath, "?")[0]
// Get the actual contents of the file we want to convert
contentBytes, err := ioutil.ReadFile(convertPath)
if err != nil {
Expand Down Expand Up @@ -97,6 +99,8 @@ func runPack(buildPath, spaPath, convertPath string) error {
pathStr := string(pathBytes)
// Remove single or double quotes around path.
pathStr = strings.Trim(pathStr, `'"`)
// Remove query string so path can be found
pathStr = strings.Split(pathStr, "?")[0]
// Intitialize the string that determines if we found the import path.
var foundPath string
// Initialize the full path of the import.
Expand Down Expand Up @@ -189,7 +193,7 @@ func runPack(buildPath, spaPath, convertPath string) error {
// Add the current file to list of already converted files.
alreadyConvertedFiles = append(alreadyConvertedFiles, fullPathStr)
// Use fullPathStr recursively to find its imports.
err = runPack(buildPath, spaPath, fullPathStr)
err = runPack(buildPath, spaPath, fullPathStr, fingerprint)
if err != nil {
return fmt.Errorf("\nCan't runPack on %s %w", fullPathStr, err)
}
Expand All @@ -198,8 +202,8 @@ func runPack(buildPath, spaPath, convertPath string) error {
if foundPath != "" {
// Remove "public" build dir from path.
replacePath := strings.Replace(foundPath, buildPath, "", 1)
// Wrap path in quotes.
replacePath = "'" + replacePath + "'"
// Wrap path in quotes and add query params to break cache.
replacePath = "'" + replacePath + "?" + fingerprint + "'"
// Convert string path to bytes.
replacePathBytes := []byte(replacePath)
// Actually replace the path to the dependency in the source content.
Expand Down
4 changes: 2 additions & 2 deletions cmd/build/gopack_dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// Create ESM support for files that don't have static imports in the app
func GopackDynamic(buildPath string, spaPath string) error {
func GopackDynamic(buildPath, spaPath, fingerprint string) error {

defer Benchmark(time.Now(), "Running GopackDynamic")

Expand All @@ -27,7 +27,7 @@ func GopackDynamic(buildPath string, spaPath string) error {
if info.IsDir() {
return nil
}
err = Gopack(buildPath, spaPath, spaPath+fieldWidgetPath+"/"+path)
err = Gopack(buildPath, spaPath, spaPath+fieldWidgetPath+"/"+path, fingerprint)
if err != nil {
return fmt.Errorf("\nError running Gopack for custom FieldWidget: %w", err)
}
Expand Down

0 comments on commit d84c8e0

Please sign in to comment.