Fixes Response not being cloneable by middleware (#7623)
This commit is contained in:
parent
f0666b92c3
commit
86e19c7cf8
5 changed files with 37 additions and 0 deletions
5
.changeset/tasty-beans-give.md
Normal file
5
.changeset/tasty-beans-give.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Allow our Response wrapper to be cloneable
|
|
@ -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;
|
||||
|
|
|
@ -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');
|
||||
|
|
12
packages/astro/test/fixtures/middleware-dev/src/pages/clone.astro
vendored
Normal file
12
packages/astro/test/fixtures/middleware-dev/src/pages/clone.astro
vendored
Normal file
|
@ -0,0 +1,12 @@
|
|||
---
|
||||
|
||||
---
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>Testing</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>testing</h1>
|
||||
</body>
|
||||
</html>
|
|
@ -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', () => {
|
||||
|
|
Loading…
Reference in a new issue