Skip to content

Commit 76d401b

Browse files
authored
[Docs Site] Add dev-only middleware for /api/ links (#19668)
1 parent fcf1db4 commit 76d401b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/middleware/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { defineMiddleware } from "astro:middleware";
2+
3+
// `astro dev` only middleware so that `/api/...` links can be viewed.
4+
export const onRequest = defineMiddleware(async (context, next) => {
5+
if (import.meta.env.DEV) {
6+
if (context.url.pathname.startsWith("/api/")) {
7+
const url = new URL(context.url.pathname, import.meta.env.SITE);
8+
9+
return fetch(url, {
10+
headers: {
11+
"accept-encoding": "identity",
12+
},
13+
});
14+
}
15+
}
16+
17+
return next();
18+
});

0 commit comments

Comments
 (0)