Fixes Response not being cloneable by middleware (#7623)

This commit is contained in:
Matthew Phillips 2023-07-11 14:21:05 -04:00 committed by GitHub
parent f0666b92c3
commit 86e19c7cf8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Allow our Response wrapper to be cloneable

View file

@ -52,6 +52,14 @@ function createResponseClass() {
}
return super.arrayBuffer();
}
clone() {
return new StreamingCompatibleResponse!(this.#body, {
status: this.status,
statusText: this.statusText,
headers: this.headers
});
}
};
return StreamingCompatibleResponse;

View file

@ -18,6 +18,12 @@ const first = defineMiddleware(async (context, next) => {
return new Response(JSON.stringify(object), {
headers: response.headers,
});
} else if(context.url.pathname === '/clone') {
const response = await next();
const newResponse = response.clone();
const /** @type {string} */ html = await newResponse.text();
const newhtml = html.replace('<h1>testing</h1>', '<h1>it works</h1>');
return new Response(newhtml, { status: 200, headers: response.headers });
} else {
if(context.url.pathname === '/') {
context.cookies.set('foo', 'bar');

View file

@ -0,0 +1,12 @@
---
---
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>testing</h1>
</body>
</html>

View file

@ -73,6 +73,12 @@ describe('Middleware in DEV mode', () => {
let res = await fixture.fetch('/');
expect(res.headers.get('set-cookie')).to.equal('foo=bar');
});
it('should be able to clone the response', async () => {
let res = await fixture.fetch('/clone');
let html = await res.text();
expect(html).to.contain('<h1>it works</h1>');
});
});
describe('Middleware in PROD mode, SSG', () => {