diff --git a/echokit/staticfiles.go b/echokit/staticfiles.go index c701e00..6813984 100644 --- a/echokit/staticfiles.go +++ b/echokit/staticfiles.go @@ -273,54 +273,139 @@ func (m *StaticFilesMiddleware) build() error { shouldFingerprint[ref] = true } } + // Import map entries use absolute paths + for _, ref := range extractImportMapRefs(content) { + if _, exists := rawFiles[ref]; exists { + shouldFingerprint[ref] = true + } + } } } - // Build fingerprint map for files that should be fingerprinted - fingerprints := make(map[string]string) - for urlPath := range shouldFingerprint { - content := rawFiles[urlPath] - ext := filepath.Ext(urlPath) - hash := md5.Sum(content) - shortHash := fmt.Sprintf("%x", hash[:6]) - base := strings.TrimSuffix(filepath.Base(urlPath), ext) - dir := filepath.Dir(urlPath) - fingerprintedPath := strings.TrimRight(dir, "/") + "/" + base + "." + shortHash + ext - fingerprints[urlPath] = fingerprintedPath - } + // Phase 3: Build dependency graph, topologically sort, rewrite and fingerprint + // bottom-up so that parent hashes reflect rewritten (fingerprinted) child paths. - // Phase 3: Rewrite references and register files - for urlPath, raw := range rawFiles { + // Build dependency graph: parent -> children + deps := make(map[string][]string) + for urlPath, content := range rawFiles { ext := filepath.Ext(urlPath) - content := raw dir := filepath.Dir(urlPath) - rewriteRelativePaths := func(re *regexp.Regexp, src []byte) []byte { - return re.ReplaceAllFunc(src, func(match []byte) []byte { - parts := re.FindSubmatch(match) - importPath := string(parts[2]) - resolved := resolveStaticImportPath(dir, importPath) - if fp, ok := fingerprints[resolved]; ok { - relFP := relativeStaticPath(dir, fp) - return []byte(string(parts[1]) + relFP + string(parts[3])) + addDeps := func(re *regexp.Regexp) { + for _, match := range re.FindAllSubmatch(content, -1) { + resolved := resolveStaticImportPath(dir, string(match[2])) + if _, exists := rawFiles[resolved]; exists { + deps[urlPath] = append(deps[urlPath], resolved) } - return match - }) + } + } + + switch ext { + case ".js": + addDeps(importFromRegex) + addDeps(importSideEffectRegex) + addDeps(dynamicImportRegex) + case ".css": + addDeps(cssURLRegex) + case ".html": + for _, match := range htmlSrcRegex.FindAllSubmatch(content, -1) { + ref := string(match[1]) + if _, exists := rawFiles[ref]; exists { + deps[urlPath] = append(deps[urlPath], ref) + } + } + for _, ref := range extractImportMapRefs(content) { + if _, exists := rawFiles[ref]; exists { + deps[urlPath] = append(deps[urlPath], ref) + } + } + } + } + + // Topological sort (Kahn's algorithm) + inDegree := make(map[string]int) + reverseDeps := make(map[string][]string) // child -> parents + for urlPath := range rawFiles { + if _, ok := inDegree[urlPath]; !ok { + inDegree[urlPath] = 0 + } + } + for parent, children := range deps { + for _, child := range children { + inDegree[parent]++ + reverseDeps[child] = append(reverseDeps[child], parent) + _ = inDegree[child] // ensure child is in map + } + } + + var order []string + var queue []string + for path, deg := range inDegree { + if deg == 0 { + queue = append(queue, path) } + } + for len(queue) > 0 { + path := queue[0] + queue = queue[1:] + order = append(order, path) + for _, parent := range reverseDeps[path] { + inDegree[parent]-- + if inDegree[parent] == 0 { + queue = append(queue, parent) + } + } + } + // Add any remaining files (cycles or disconnected) + for path := range rawFiles { + found := false + for _, p := range order { + if p == path { + found = true + break + } + } + if !found { + order = append(order, path) + } + } + + // Process files in topological order (leaves first). + // Rewrite references using already-computed fingerprints, then hash the result. + fingerprints := make(map[string]string) + rewrittenContent := make(map[string][]byte) + + rewriteRelativePaths := func(re *regexp.Regexp, src []byte, dir string) []byte { + return re.ReplaceAllFunc(src, func(match []byte) []byte { + parts := re.FindSubmatch(match) + importPath := string(parts[2]) + resolved := resolveStaticImportPath(dir, importPath) + if fp, ok := fingerprints[resolved]; ok { + relFP := relativeStaticPath(dir, fp) + return []byte(string(parts[1]) + relFP + string(parts[3])) + } + return match + }) + } + + for _, urlPath := range order { + ext := filepath.Ext(urlPath) + content := rawFiles[urlPath] + dir := filepath.Dir(urlPath) // Rewrite JS imports if ext == ".js" { - content = rewriteRelativePaths(importFromRegex, content) - content = rewriteRelativePaths(importSideEffectRegex, content) - content = rewriteRelativePaths(dynamicImportRegex, content) + content = rewriteRelativePaths(importFromRegex, content, dir) + content = rewriteRelativePaths(importSideEffectRegex, content, dir) + content = rewriteRelativePaths(dynamicImportRegex, content, dir) } // Rewrite CSS url() references if ext == ".css" { - content = rewriteRelativePaths(cssURLRegex, content) + content = rewriteRelativePaths(cssURLRegex, content, dir) } - // Rewrite HTML src/href attributes + // Rewrite HTML src/href attributes and import maps if ext == ".html" { content = rewriteStaticHTML(content, fingerprints) if m.devMode { @@ -328,7 +413,22 @@ func (m *StaticFilesMiddleware) build() error { } } - ct := staticContentType(ext) + rewrittenContent[urlPath] = content + + // Compute fingerprint from rewritten content (not raw) + if shouldFingerprint[urlPath] { + hash := md5.Sum(content) + shortHash := fmt.Sprintf("%x", hash[:6]) + base := strings.TrimSuffix(filepath.Base(urlPath), ext) + fpDir := filepath.Dir(urlPath) + fingerprintedPath := strings.TrimRight(fpDir, "/") + "/" + base + "." + shortHash + ext + fingerprints[urlPath] = fingerprintedPath + } + } + + // Register all files for serving + for urlPath, content := range rewrittenContent { + ct := staticContentType(filepath.Ext(urlPath)) hash := md5.Sum(content) e := &staticEntry{ content: content, @@ -337,11 +437,9 @@ func (m *StaticFilesMiddleware) build() error { } if fp, ok := fingerprints[urlPath]; ok { - // Fingerprinted file: only serve at the fingerprinted path e.fingerprinted = true files[fp] = e } else { - // Non-fingerprinted file: serve at the original path if strings.HasPrefix(urlPath, "/vendor/") { e.vendored = true } @@ -537,6 +635,31 @@ func rewriteImportMap(html string, fingerprints map[string]string) string { return html[:startIdx] + mapContent + html[endIdx:] } +// extractImportMapRefs extracts absolute paths referenced in an HTML import map. +func extractImportMapRefs(content []byte) []string { + importMapValueRegex := regexp.MustCompile(`":\s*"(/[^"]+)"`) + const startTag = `` + + s := string(content) + startIdx := strings.Index(s, startTag) + if startIdx < 0 { + return nil + } + startIdx += len(startTag) + endIdx := strings.Index(s[startIdx:], endTag) + if endIdx < 0 { + return nil + } + mapContent := s[startIdx : startIdx+endIdx] + + var refs []string + for _, match := range importMapValueRegex.FindAllStringSubmatch(mapContent, -1) { + refs = append(refs, match[1]) + } + return refs +} + func resolveStaticImportPath(dir, importPath string) string { resolved := filepath.Join(dir, importPath) return filepath.ToSlash(filepath.Clean(resolved)) diff --git a/echokit/staticfiles_test.go b/echokit/staticfiles_test.go index 2be52e5..6e939ad 100644 --- a/echokit/staticfiles_test.go +++ b/echokit/staticfiles_test.go @@ -266,6 +266,276 @@ func TestStaticFilesMiddleware(t *testing.T) { assert.NotContains(t, body, `href="/style.css"`) assert.Contains(t, body, `href="/style.`) }) + + t.Run("parent_fingerprint_changes_when_child_content_changes", func(t *testing.T) { + dir := t.TempDir() + os.MkdirAll(filepath.Join(dir, "lib"), 0755) + os.MkdirAll(filepath.Join(dir, "components"), 0755) + + os.WriteFile(filepath.Join(dir, "index.html"), []byte(`
`), 0644) + os.WriteFile(filepath.Join(dir, "lib", "utils.js"), []byte("export function hello() { return 'v1'; }"), 0644) + os.WriteFile(filepath.Join(dir, "components", "app.js"), []byte(`import { hello } from '../lib/utils.js'; console.log(hello());`), 0644) + + html1 := getHTML(t, dir) + + os.WriteFile(filepath.Join(dir, "lib", "utils.js"), []byte("export function hello() { return 'v2'; }"), 0644) + + html2 := getHTML(t, dir) + + assert.NotEqual(t, html1, html2, "index.html should reference different app.js fingerprints when child changes") + }) + + t.Run("grandchild_change_cascades_through_two_levels", func(t *testing.T) { + // index.html -> app.js -> view.js -> utils.js + // Changing utils.js should change all three fingerprinted paths. + dir := t.TempDir() + os.MkdirAll(filepath.Join(dir, "lib"), 0755) + os.MkdirAll(filepath.Join(dir, "components"), 0755) + + os.WriteFile(filepath.Join(dir, "index.html"), []byte(``), 0644) + os.WriteFile(filepath.Join(dir, "lib", "utils.js"), []byte("export const VERSION = 'v1';"), 0644) + os.WriteFile(filepath.Join(dir, "components", "view.js"), []byte(`import { VERSION } from '../lib/utils.js'; export const v = VERSION;`), 0644) + os.WriteFile(filepath.Join(dir, "components", "app.js"), []byte(`import { v } from './view.js'; console.log(v);`), 0644) + + html1 := getHTML(t, dir) + + os.WriteFile(filepath.Join(dir, "lib", "utils.js"), []byte("export const VERSION = 'v2';"), 0644) + + html2 := getHTML(t, dir) + + assert.NotEqual(t, html1, html2, "grandchild change should cascade to index.html") + }) + + t.Run("sibling_import_change_does_not_affect_unrelated_parent", func(t *testing.T) { + // index.html -> app.js (imports utils.js) + // index.html -> other.js (imports helper.js) + // Changing helper.js should NOT change app.js fingerprint, only other.js + dir := t.TempDir() + os.MkdirAll(filepath.Join(dir, "lib"), 0755) + os.MkdirAll(filepath.Join(dir, "components"), 0755) + + os.WriteFile(filepath.Join(dir, "index.html"), []byte(``), 0644) + os.WriteFile(filepath.Join(dir, "lib", "utils.js"), []byte("export const A = 1;"), 0644) + os.WriteFile(filepath.Join(dir, "lib", "helper.js"), []byte("export const B = 'v1';"), 0644) + os.WriteFile(filepath.Join(dir, "components", "app.js"), []byte(`import { A } from '../lib/utils.js';`), 0644) + os.WriteFile(filepath.Join(dir, "components", "other.js"), []byte(`import { B } from '../lib/helper.js';`), 0644) + + html1 := getHTML(t, dir) + appFP1 := extractFingerprintedPath(html1, "app.") + otherFP1 := extractFingerprintedPath(html1, "other.") + + os.WriteFile(filepath.Join(dir, "lib", "helper.js"), []byte("export const B = 'v2';"), 0644) + + html2 := getHTML(t, dir) + appFP2 := extractFingerprintedPath(html2, "app.") + otherFP2 := extractFingerprintedPath(html2, "other.") + + assert.Equal(t, appFP1, appFP2, "app.js fingerprint should not change when unrelated helper changes") + assert.NotEqual(t, otherFP1, otherFP2, "other.js fingerprint should change when its child helper changes") + }) + + t.Run("shared_dependency_change_affects_all_parents", func(t *testing.T) { + // index.html -> app.js -> shared.js + // index.html -> other.js -> shared.js + // Changing shared.js should change both app.js and other.js + dir := t.TempDir() + os.MkdirAll(filepath.Join(dir, "lib"), 0755) + os.MkdirAll(filepath.Join(dir, "components"), 0755) + + os.WriteFile(filepath.Join(dir, "index.html"), []byte(``), 0644) + os.WriteFile(filepath.Join(dir, "lib", "shared.js"), []byte("export const X = 'v1';"), 0644) + os.WriteFile(filepath.Join(dir, "components", "app.js"), []byte(`import { X } from '../lib/shared.js';`), 0644) + os.WriteFile(filepath.Join(dir, "components", "other.js"), []byte(`import { X } from '../lib/shared.js';`), 0644) + + html1 := getHTML(t, dir) + appFP1 := extractFingerprintedPath(html1, "app.") + otherFP1 := extractFingerprintedPath(html1, "other.") + + os.WriteFile(filepath.Join(dir, "lib", "shared.js"), []byte("export const X = 'v2';"), 0644) + + html2 := getHTML(t, dir) + appFP2 := extractFingerprintedPath(html2, "app.") + otherFP2 := extractFingerprintedPath(html2, "other.") + + assert.NotEqual(t, appFP1, appFP2, "app.js fingerprint should change when shared dep changes") + assert.NotEqual(t, otherFP1, otherFP2, "other.js fingerprint should change when shared dep changes") + }) + + t.Run("unchanged_files_keep_same_fingerprint", func(t *testing.T) { + dir := t.TempDir() + os.MkdirAll(filepath.Join(dir, "lib"), 0755) + + os.WriteFile(filepath.Join(dir, "index.html"), []byte(``), 0644) + os.WriteFile(filepath.Join(dir, "lib", "app.js"), []byte("console.log('stable');"), 0644) + + html1 := getHTML(t, dir) + html2 := getHTML(t, dir) + + assert.Equal(t, html1, html2, "same content should produce same fingerprints") + }) + + t.Run("import_side_effect_changes_cascade", func(t *testing.T) { + // import './side-effect.js'; (no from clause) + dir := t.TempDir() + os.MkdirAll(filepath.Join(dir, "lib"), 0755) + + os.WriteFile(filepath.Join(dir, "index.html"), []byte(``), 0644) + os.WriteFile(filepath.Join(dir, "lib", "side-effect.js"), []byte("document.title = 'v1';"), 0644) + os.WriteFile(filepath.Join(dir, "lib", "app.js"), []byte(`import './side-effect.js'; console.log('app');`), 0644) + + html1 := getHTML(t, dir) + + os.WriteFile(filepath.Join(dir, "lib", "side-effect.js"), []byte("document.title = 'v2';"), 0644) + + html2 := getHTML(t, dir) + + assert.NotEqual(t, html1, html2, "side-effect import change should cascade") + }) + + t.Run("dynamic_import_changes_cascade", func(t *testing.T) { + // import('./lazy.js') + dir := t.TempDir() + os.MkdirAll(filepath.Join(dir, "lib"), 0755) + + os.WriteFile(filepath.Join(dir, "index.html"), []byte(``), 0644) + os.WriteFile(filepath.Join(dir, "lib", "lazy.js"), []byte("export const LAZY = 'v1';"), 0644) + os.WriteFile(filepath.Join(dir, "lib", "app.js"), []byte(`const m = import('./lazy.js');`), 0644) + + html1 := getHTML(t, dir) + + os.WriteFile(filepath.Join(dir, "lib", "lazy.js"), []byte("export const LAZY = 'v2';"), 0644) + + html2 := getHTML(t, dir) + + assert.NotEqual(t, html1, html2, "dynamic import change should cascade") + }) + + t.Run("css_url_change_cascades_to_html", func(t *testing.T) { + dir := t.TempDir() + os.MkdirAll(filepath.Join(dir, "images"), 0755) + + os.WriteFile(filepath.Join(dir, "index.html"), []byte(``), 0644) + os.WriteFile(filepath.Join(dir, "style.css"), []byte(`body { background: url('./images/bg.png'); }`), 0644) + os.WriteFile(filepath.Join(dir, "images", "bg.png"), []byte("v1-png-data"), 0644) + + html1 := getHTML(t, dir) + + os.WriteFile(filepath.Join(dir, "images", "bg.png"), []byte("v2-png-data"), 0644) + + html2 := getHTML(t, dir) + + assert.NotEqual(t, html1, html2, "CSS url() image change should cascade to HTML") + }) + + t.Run("import_map_entries_are_rewritten", func(t *testing.T) { + dir := t.TempDir() + os.MkdirAll(filepath.Join(dir, "lib"), 0755) + + os.WriteFile(filepath.Join(dir, "index.html"), []byte(``), 0644) + os.WriteFile(filepath.Join(dir, "lib", "mylib.js"), []byte("export const LIB = 'v1';"), 0644) + + html1 := getHTML(t, dir) + assert.NotContains(t, html1, `"/lib/mylib.js"`, "import map should have fingerprinted path") + assert.Contains(t, html1, "/lib/mylib.", "import map should reference fingerprinted mylib") + + os.WriteFile(filepath.Join(dir, "lib", "mylib.js"), []byte("export const LIB = 'v2';"), 0644) + + html2 := getHTML(t, dir) + assert.NotEqual(t, html1, html2, "import map should reflect new fingerprint when lib changes") + }) + + t.Run("fingerprinted_file_content_matches_rewritten_version", func(t *testing.T) { + // Verify the actual served content of a fingerprinted file contains + // the rewritten import paths, not the original ones. + dir := t.TempDir() + os.MkdirAll(filepath.Join(dir, "lib"), 0755) + os.MkdirAll(filepath.Join(dir, "components"), 0755) + + os.WriteFile(filepath.Join(dir, "index.html"), []byte(``), 0644) + os.WriteFile(filepath.Join(dir, "lib", "utils.js"), []byte("export const U = 1;"), 0644) + os.WriteFile(filepath.Join(dir, "components", "app.js"), []byte(`import { U } from '../lib/utils.js'; console.log(U);`), 0644) + + m := NewStaticFilesMiddleware(dir, false) + defer m.Close() + e := echo.New() + e.Use(m.Handler()) + + req := httptest.NewRequest(http.MethodGet, "/", nil) + rec := httptest.NewRecorder() + e.ServeHTTP(rec, req) + html := rec.Body.String() + + appFP := extractFingerprintedPath(html, "app.") + assert.NotEmpty(t, appFP) + + // Fetch the fingerprinted app.js + req = httptest.NewRequest(http.MethodGet, appFP, nil) + rec = httptest.NewRecorder() + e.ServeHTTP(rec, req) + + assert.Equal(t, http.StatusOK, rec.Code) + jsContent := rec.Body.String() + + // Should NOT contain the original import path + assert.NotContains(t, jsContent, `'../lib/utils.js'`) + // Should contain a fingerprinted import path + assert.Contains(t, jsContent, "../lib/utils.") + }) + + t.Run("vendored_files_are_not_fingerprinted", func(t *testing.T) { + dir := t.TempDir() + os.MkdirAll(filepath.Join(dir, "vendor"), 0755) + + os.WriteFile(filepath.Join(dir, "index.html"), []byte(``), 0644) + os.WriteFile(filepath.Join(dir, "vendor", "lib.js"), []byte("var lib = {};"), 0644) + + m := NewStaticFilesMiddleware(dir, false) + defer m.Close() + e := echo.New() + e.Use(m.Handler()) + + // Vendored file should be served at its original path + req := httptest.NewRequest(http.MethodGet, "/vendor/lib.js", nil) + rec := httptest.NewRecorder() + e.ServeHTTP(rec, req) + + assert.Equal(t, http.StatusOK, rec.Code) + assert.Equal(t, "public, max-age=31536000, immutable", rec.Header().Get("Cache-Control")) + }) +} + +// getHTML builds a fresh StaticFilesMiddleware for the given directory and returns the index.html content. +func getHTML(t *testing.T, dir string) string { + t.Helper() + m := NewStaticFilesMiddleware(dir, false) + defer m.Close() + e := echo.New() + e.Use(m.Handler()) + req := httptest.NewRequest(http.MethodGet, "/", nil) + rec := httptest.NewRecorder() + e.ServeHTTP(rec, req) + assert.Equal(t, http.StatusOK, rec.Code) + return rec.Body.String() +} + +// extractFingerprintedPath finds a fingerprinted path in HTML matching the given prefix. +// e.g. extractFingerprintedPath(html, "app.") finds "/components/app.abc123.js" +func extractFingerprintedPath(html, prefix string) string { + idx := strings.Index(html, prefix) + if idx < 0 { + return "" + } + // Walk backwards to find the start (opening quote) + start := idx + for start > 0 && html[start-1] != '"' && html[start-1] != '\'' { + start-- + } + // Walk forward to find the end (closing quote) + end := idx + for end < len(html) && html[end] != '"' && html[end] != '\'' { + end++ + } + return html[start:end] } func TestStaticFilesMiddleware_LiveReload(t *testing.T) {