Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rewrite): copy body from the request #11182

Merged
merged 4 commits into from
Jun 5, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: copy body without using it
ematipico committed Jun 5, 2024

Verified

This commit was signed with the committer’s verified signature.
mcollina Matteo Collina
commit e6b975ef208d11bb054f04f0bc599b717100f222
8 changes: 3 additions & 5 deletions packages/astro/src/core/routing/request.ts
Original file line number Diff line number Diff line change
@@ -5,8 +5,6 @@
* @param oldRequest The old `Request`
*/
export async function copyRequest(newUrl: URL, oldRequest: Request): Promise<Request> {
const body = oldRequest.headers.get('content-type') ? oldRequest.blob() : Promise.resolve(undefined);
return body.then((requestBody) => {
return new Request(newUrl, {
method: oldRequest.method,
headers: oldRequest.headers,
@@ -20,8 +18,8 @@ export async function copyRequest(newUrl: URL, oldRequest: Request): Promise<Req
integrity: oldRequest.integrity,
signal: oldRequest.signal,
keepalive: oldRequest.keepalive,
// https://fetch.spec.whatwg.org/#dom-request-duplex
// @ts-expect-error It isn't part of the types, but undici accepts it and it allows to carry over the body to a new request
duplex: "half"
})
}

);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
---
let email = ''
if (Astro.request.method === 'POST') {
try {
const data = await Astro.request.formData();
email = data.get('email')?.toString().trim();
} catch {}
}
return Astro.rewrite('/post/post-b')
---

Original file line number Diff line number Diff line change
@@ -2,9 +2,11 @@
let email = ''
if (Astro.request.method === 'POST') {
try {
const data = await Astro.request.formData();
email = data.get('email')?.toString().trim();
} catch {}
const data = await Astro.request.json();
email = data.email?.toString().trim();
} catch (e) {
console.log(e)
}
}
---

10 changes: 5 additions & 5 deletions packages/astro/test/rewrite.test.js
Original file line number Diff line number Diff line change
@@ -190,17 +190,17 @@ describe('SSR reroute', () => {
});

it('should pass the POST data from one page to another', async () => {
const html = await fixture.fetch('/post/post-a', {
const request = new Request('http://example.com/post/post-a', {
method: "POST",
body: JSON.stringify({
email: "[email protected]",
title: "Fix my bugs",
completed: false
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
"content-type": "application/json"
}
}).then((res) => res.text());
});
const response = await app.render(request);
const html = await response.text();
const $ = cheerioLoad(html);

assert.equal($('h1').text(), 'Post B');