Skip to content

Commit

Permalink
in dev server, convert a simple endpoint result into a response object
Browse files Browse the repository at this point in the history
  • Loading branch information
AirBorne04 committed Oct 14, 2022
1 parent d74176e commit 1b441f6
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions packages/astro/src/vite-plugin-astro-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { AstroSettings, ManifestData } from '../@types/astro';
import { DevelopmentEnvironment, SSROptions } from '../core/render/dev/index';

import { Readable } from 'stream';
import { getSetCookiesFromResponse } from '../core/cookies/index.js';
import { attachToResponse, getSetCookiesFromResponse } from '../core/cookies/index.js';
import { call as callEndpoint } from '../core/endpoint/dev/index.js';
import {
collectErrorMetadata,
Expand Down Expand Up @@ -352,7 +352,6 @@ async function handleRoute(
// Route successfully matched! Render it.
if (route.type === 'endpoint') {
const result = await callEndpoint(options);

if (result.type === 'response') {
if (result.response.headers.get('X-Astro-Response') === 'Not-Found') {
const fourOhFourRoute = await matchRoute('/404', env, manifest);
Expand All @@ -379,22 +378,14 @@ async function handleRoute(
if (computedMimeType) {
contentType = computedMimeType;
}

const header: any = {
'Content-Type': `${contentType};charset=utf-8`
};

let cookieHeader = [];
for (let cok of result.cookies.headers()) {
cookieHeader.push(cok);
}

if (cookieHeader.length > 0) {
header['Set-Cookie'] = cookieHeader;
}

res.writeHead(200, header);
res.end(result.body);
const response = new Response(result.body, {
status: 200,
headers: {
'Content-Type': `${contentType};charset=utf-8`,
},
});
attachToResponse(response, result.cookies);
await writeWebResponse(res, response);
}
} else {
const result = await renderPage(options);
Expand Down

0 comments on commit 1b441f6

Please sign in to comment.