Skip to content

Commit

Permalink
Fix ?css redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed May 10, 2023
1 parent 4d13c89 commit 44cae10
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/esm-worker/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/esm-worker/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "esm-worker",
"description": "A Cloudflare workers that handles all requests to the esm.sh origin server at the edge.",
"version": "0.120.0",
"version": "0.120.1",
"type": "module",
"module": "dist/index.js",
"types": "types/index.d.ts",
Expand Down
15 changes: 12 additions & 3 deletions packages/esm-worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ class ESMWorker {
}
}

if (pathname === "/" || pathname.startsWith("/embed/")) {
if (
pathname === "/" ||
pathname === "/build" ||
pathname.startsWith("/embed/")
) {
return fetchServerOrigin(
req,
ctx,
Expand Down Expand Up @@ -408,13 +412,18 @@ class ESMWorker {
const ua = req.headers.get("user-agent");
target = getEsmaVersionFromUA(ua);
}
headers.set("Cache-Control", "public, max-age=31536000, immutable");
const pined = hasBuildVerPrefix || hasBuildVerQuery;
if (pined) {
headers.set("Cache-Control", "public, max-age=31536000, immutable");
} else {
headers.set("Cache-Control", "public, max-age=86400");
}
return redirect(
new URL(
`${prefix}/${pkg}@${packageVersion}/${target}/${packageName}.css`,
url,
),
301,
pined ? 301 : 302,
headers,
);
}
Expand Down
6 changes: 5 additions & 1 deletion server/server_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,11 @@ func esmHandler() rex.Handle {
return rex.Status(404, "Package CSS not found")
}
url := fmt.Sprintf("%s%s/%s.css", cdnOrigin, cfg.BasePath, strings.TrimSuffix(taskID, path.Ext(taskID)))
return rex.Redirect(url, http.StatusMovedPermanently)
code := 302
if isPined {
code = 301
}
return rex.Redirect(url, code)
}

if isBarePath {
Expand Down

0 comments on commit 44cae10

Please sign in to comment.