Skip to content

Commit deac048

Browse files
committed
Support dynamic redirects
1 parent 95f58f5 commit deac048

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

site/content/artifacts/_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ date: 2022-01-02T13:00:18-05:00
44
images: []
55
aliases:
66
- "/archive"
7+
- "/artifact"
78
---

site/layouts/index.redirects

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
{
99
"from": {{ $absAlias | path.Clean | jsonify }},
1010
"to": {{ $page.RelPermalink | jsonify }},
11-
"status": 301
11+
"status": 301,
12+
"kind": "static"
1213
},
1314
{
1415
"from": {{ $absAlias | path.Clean | printf "%s/" | jsonify }},
1516
"to": {{ $page.RelPermalink | jsonify }},
16-
"status": 301
17+
"status": 301,
18+
"kind": {{ if (eq $page.Kind "page" "home") }}"static"{{ else }}"dynamic"{{ end }}
1719
},
1820
{{ end }}
1921
{{ end }}
@@ -24,6 +26,7 @@
2426
"from": {{ $redirect.from | path.Clean | jsonify }},
2527
"to": {{ $redirect.to | jsonify }},
2628
"status": {{ $redirect.status | jsonify }},
29+
"kind": "static"
2730
}{{ if (eq $index (sub $numRedirects 1)) }},{{ end }}
2831
{{ end }}
2932
]

worker/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ interface Redirect {
88
from: string;
99
to: string;
1010
status: number;
11+
kind: "static" | "dynamic";
1112
};
1213

1314
type Redirects = ReadonlyArray<Redirect>;
@@ -72,7 +73,10 @@ export default {
7273
const redirects: Redirects = await redirectsResponse.json();
7374

7475
for (const redirect of redirects) {
75-
if (requestUrl.pathname === redirect.from) {
76+
if (
77+
(redirect.kind === "static" && requestUrl.pathname === redirect.from)
78+
|| (redirect.kind === "dynamic" && requestUrl.pathname.startsWith(redirect.from))
79+
) {
7680
const url = new URL(request.url);
7781
url.pathname = redirect.to;
7882
return Response.redirect(url.toString(), redirect.status);

0 commit comments

Comments
 (0)