-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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 (#11182)
* fix(rewrite): copy body from the request * wip * chore: copy body without using it * chore: remove new file
- Loading branch information
Showing
5 changed files
with
80 additions
and
3 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
8 changes: 8 additions & 0 deletions
8
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,8 @@ | ||
--- | ||
let email = '' | ||
return Astro.rewrite('/post/post-b') | ||
--- | ||
|
||
<h1>Post A</h1> | ||
|
||
<h2>{email}</h2> |
15 changes: 15 additions & 0 deletions
15
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,15 @@ | ||
--- | ||
let email = '' | ||
if (Astro.request.method === 'POST') { | ||
try { | ||
const data = await Astro.request.json(); | ||
email = data.email?.toString().trim(); | ||
} catch (e) { | ||
console.log(e) | ||
} | ||
} | ||
--- | ||
|
||
<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 |
---|---|---|
|
@@ -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('should pass the POST data from one page to another', async () => { | ||
const request = new Request('http://example.com/post/post-a', { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
email: "[email protected]", | ||
}), | ||
headers: { | ||
"content-type": "application/json" | ||
} | ||
}); | ||
const response = await app.render(request); | ||
const html = await response.text(); | ||
const $ = cheerioLoad(html); | ||
|
||
assert.equal($('h1').text(), 'Post B'); | ||
assert.match($('h2').text(), /[email protected]/); | ||
}); | ||
}); | ||
|
||
describe('Middleware', () => { | ||
|