mirror of
https://github.com/withastro/astro.git
synced 2025-03-03 22:57:08 -05:00
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();
|
return super.arrayBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clone() {
|
||||||
|
return new StreamingCompatibleResponse!(this.#body, {
|
||||||
|
status: this.status,
|
||||||
|
statusText: this.statusText,
|
||||||
|
headers: this.headers
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return StreamingCompatibleResponse;
|
return StreamingCompatibleResponse;
|
||||||
|
|
|
@ -18,6 +18,12 @@ const first = defineMiddleware(async (context, next) => {
|
||||||
return new Response(JSON.stringify(object), {
|
return new Response(JSON.stringify(object), {
|
||||||
headers: response.headers,
|
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 {
|
} else {
|
||||||
if(context.url.pathname === '/') {
|
if(context.url.pathname === '/') {
|
||||||
context.cookies.set('foo', 'bar');
|
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('/');
|
let res = await fixture.fetch('/');
|
||||||
expect(res.headers.get('set-cookie')).to.equal('foo=bar');
|
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', () => {
|
describe('Middleware in PROD mode, SSG', () => {
|
||||||
|
|
Loading…
Add table
Reference in a new issue