-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rewrite): copy body from the request
- Loading branch information
Showing
7 changed files
with
84 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Fixes an issue where `Astro.rewrite` wasn't carrying over the body of a `Request` in on-demand pages. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Utility function that creates a new `Request` with a new URL from an old `Request`. | ||
* | ||
* @param newUrl The new `URL` | ||
* @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, | ||
body: requestBody, | ||
referrer: oldRequest.referrer, | ||
referrerPolicy: oldRequest.referrerPolicy, | ||
mode: oldRequest.mode, | ||
credentials: oldRequest.credentials, | ||
cache: oldRequest.cache, | ||
redirect: oldRequest.redirect, | ||
integrity: oldRequest.integrity, | ||
signal: oldRequest.signal, | ||
keepalive: oldRequest.keepalive, | ||
}) | ||
} | ||
|
||
); | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/astro/test/fixtures/reroute/src/pages/post/post-a.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
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') | ||
--- | ||
|
||
<h1>Post A</h1> | ||
|
||
<h2>{email}</h2> |
13 changes: 13 additions & 0 deletions
13
packages/astro/test/fixtures/reroute/src/pages/post/post-b.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
let email = '' | ||
if (Astro.request.method === 'POST') { | ||
try { | ||
const data = await Astro.request.formData(); | ||
email = data.get('email')?.toString().trim(); | ||
} catch {} | ||
} | ||
--- | ||
|
||
<h1>Post B</h1> | ||
|
||
<h2>{email}</h2> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ import { load as cheerioLoad } from 'cheerio'; | |
import testAdapter from './test-adapter.js'; | ||
import { loadFixture } from './test-utils.js'; | ||
|
||
describe('Dev reroute', () => { | ||
describe.only('Dev reroute', () => { | ||
/** @type {import('./test-utils').Fixture} */ | ||
let fixture; | ||
let devServer; | ||
|
@@ -61,6 +61,7 @@ describe('Dev reroute', () => { | |
|
||
assert.equal($('h1').text(), '404: Not found'); | ||
}); | ||
|
||
}); | ||
|
||
describe('Build reroute', () => { | ||
|
@@ -112,7 +113,7 @@ describe('Build reroute', () => { | |
|
||
it('should render the 404 built-in page', async () => { | ||
try { | ||
const html = await fixture.readFile('/spread/oops/index.html'); | ||
await fixture.readFile('/spread/oops/index.html'); | ||
assert.fail('Not found'); | ||
} catch { | ||
assert.ok; | ||
|
@@ -187,6 +188,24 @@ describe('SSR reroute', () => { | |
const html = await response.text(); | ||
assert.equal(html, 'Not found'); | ||
}); | ||
|
||
it.only('should pass the POST data from one page to another', async () => { | ||
const html = await fixture.fetch('/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" | ||
} | ||
}).then((res) => res.text()); | ||
const $ = cheerioLoad(html); | ||
|
||
assert.equal($('h1').text(), 'Post B'); | ||
assert.match($('h2').text(), /example@example.com/); | ||
}); | ||
}); | ||
|
||
describe('Middleware', () => { | ||
|