Skip to content

Commit

Permalink
Merge pull request #236 from withastro/main
Browse files Browse the repository at this point in the history
a12111
  • Loading branch information
akshit20421 authored Oct 2, 2024
2 parents d5d73a8 + 42037f3 commit bac5b71
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-doors-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Returns custom statusText that has been set in a Response
5 changes: 5 additions & 0 deletions .changeset/rich-apes-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Server islands: changes the server island HTML placeholder comment so that it is much less likely to get removed by HTML minifiers.
3 changes: 2 additions & 1 deletion packages/astro/src/core/app/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export class NodeApp extends App {
* @param destination NodeJS ServerResponse
*/
static async writeResponse(source: Response, destination: ServerResponse) {
const { status, headers, body } = source;
const { status, headers, body, statusText } = source;
destination.statusMessage = statusText;
destination.writeHead(status, createOutgoingHttpHeaders(headers));
if (!body) return destination.end();
try {
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/runtime/server/render/server-islands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function renderServerIsland(
}
}

destination.write('<!--server-island-start-->');
destination.write('<!--[if astro]>server-island-start<![endif]-->');

// Render the slots
const renderedSlots: Record<string, string> = {};
Expand Down Expand Up @@ -88,7 +88,7 @@ if(response.status === 200 && response.headers.get('content-type') === 'text/htm
// Swap!
while(script.previousSibling &&
script.previousSibling.nodeType !== 8 &&
script.previousSibling.data !== 'server-island-start') {
script.previousSibling.data !== '[if astro]>server-island-start<![endif]') {
script.previousSibling.remove();
}
script.previousSibling?.remove();
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/vite-plugin-astro-server/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function writeHtmlResponse(res: http.ServerResponse, statusCode: number,
}

export async function writeWebResponse(res: http.ServerResponse, webResponse: Response) {
const { status, headers, body } = webResponse;
const { status, headers, body, statusText } = webResponse;

// Attach any set-cookie headers added via Astro.cookies.set()
const setCookieHeaders = Array.from(getSetCookiesFromResponse(webResponse));
Expand All @@ -67,7 +67,7 @@ export async function writeWebResponse(res: http.ServerResponse, webResponse: Re
if (headers.has('set-cookie')) {
_headers['set-cookie'] = headers.getSetCookie();
}

res.statusMessage = statusText;
res.writeHead(status, _headers);
if (body) {
if (Symbol.for('astro.responseBody') in webResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ export function GET() {
{ name: 'lettuce' },
{ name: 'broccoli' },
{ name: 'pizza' }
])
]), {
status: 200,
statusText: `tasty`,
}
)
}

export async function POST({ params, request }) {
const body = await request.text();
return new Response(body === `some data` ? `ok` : `not ok`, {
status: 200,
const ok = body === `some data`
return new Response( ok ? `ok` : `not ok`, {
status: ok ? 200 : 400,
statusText: ok ? `ok` : `not ok`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
Expand Down
12 changes: 12 additions & 0 deletions packages/astro/test/ssr-api-route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('API routes in SSR', () => {
const request = new Request('http://example.com/food.json');
const response = await app.render(request);
assert.equal(response.status, 200);
assert.equal(response.statusText, 'tasty');
const body = await response.json();
assert.equal(body.length, 3);
});
Expand Down Expand Up @@ -78,6 +79,17 @@ describe('API routes in SSR', () => {
assert.equal(text, 'ok');
});

it('Can read custom status text from API routes', async () => {
const response = await fixture.fetch('/food.json', {
method: 'POST',
body: `not some data`,
});
assert.equal(response.status, 400);
assert.equal(response.statusText, 'not ok');
const text = await response.text();
assert.equal(text, 'not ok');
});

it('Can be passed binary data from multipart formdata', async () => {
const formData = new FormData();
const raw = await fs.promises.readFile(
Expand Down

0 comments on commit bac5b71

Please sign in to comment.