This repository was archived by the owner on Feb 10, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed
Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ ' @astrojs/netlify ' : patch
3+ ---
4+
5+ Fixes ` context.rewrite ` in edge middleware
Original file line number Diff line number Diff line change @@ -294,6 +294,25 @@ export default function netlifyIntegration(
294294 params: {}
295295 });
296296 ctx.locals = { netlify: { context } }
297+ // https://docs.netlify.com/edge-functions/api/#return-a-rewrite
298+ ctx.rewrite = (target) => {
299+ if(target instanceof Request) {
300+ // We can only mutate headers, so if anything else is different, we need to fetch
301+ // the target URL instead.
302+ if(target.method !== request.method || target.body || target.url.origin !== request.url.origin) {
303+ return fetch(target);
304+ }
305+ // We can't replace the headers object, so we need to delete all headers and set them again
306+ request.headers.forEach((_value, key) => {
307+ request.headers.delete(key);
308+ });
309+ target.headers.forEach((value, key) => {
310+ request.headers.set(key, value);
311+ });
312+ return new URL(target.url);
313+ }
314+ return new URL(target, request.url);
315+ };
297316 const next = () => {
298317 const { netlify, ...otherLocals } = ctx.locals;
299318 request.headers.set("x-astro-locals", trySerializeLocals(otherLocals));
You can’t perform that action at this time.
0 commit comments