Skip to content

Commit 7c95e0b

Browse files
authored
Expose and fix bug with assumption of removable 2-char prefix, seemingly meant for ./ (#1345)
1 parent 923cdfd commit 7c95e0b

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

server/router.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,7 @@ func esmRouter(esmStorage storage.Storage, logger *log.Logger) rex.Handle {
13041304
}
13051305

13061306
if buildMeta.CSSEntry != "" {
1307-
url := strings.Join([]string{origin, esmPath.PackageId(), buildMeta.CSSEntry[2:]}, "/")
1307+
url := getCSSEntryRedirectURL(origin, esmPath, buildMeta.CSSEntry)
13081308
return redirect(ctx, url, isExactVersion)
13091309
}
13101310

@@ -1617,3 +1617,7 @@ func errorJS(ctx *rex.Context, message string) any {
16171617
ctx.SetHeader("Cache-Control", ccImmutable)
16181618
return buf.Bytes()
16191619
}
1620+
1621+
func getCSSEntryRedirectURL(origin string, esmPath EsmPath, cssEntry string) string {
1622+
return origin + "/" + esmPath.PackageId() + utils.NormalizePathname(cssEntry)
1623+
}

server/router_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package server
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestCSSEntryRedirectURL(t *testing.T) {
8+
origin := "https://esm.sh"
9+
esmPath := EsmPath{
10+
PkgName: "@material/web",
11+
PkgVersion: "2.4.2-nightly.95dd57c.0",
12+
}
13+
14+
tests := []struct {
15+
cssEntry string
16+
expected string
17+
}{
18+
{
19+
cssEntry: "./labs/gb/components/ripple/ripple.css",
20+
expected: "https://esm.sh/@material/web@2.4.2-nightly.95dd57c.0/labs/gb/components/ripple/ripple.css",
21+
},
22+
{
23+
cssEntry: "labs/gb/components/ripple/ripple.css",
24+
expected: "https://esm.sh/@material/web@2.4.2-nightly.95dd57c.0/labs/gb/components/ripple/ripple.css",
25+
},
26+
}
27+
28+
for _, test := range tests {
29+
// Calling the actual helper function from router.go
30+
url := getCSSEntryRedirectURL(origin, esmPath, test.cssEntry)
31+
if url != test.expected {
32+
t.Errorf("For CSSEntry %q, expected %q, but got %q", test.cssEntry, test.expected, url)
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)