[ci] format
This commit is contained in:
parent
4c25a1c2ea
commit
279774c48e
17 changed files with 84 additions and 101 deletions
|
@ -5,13 +5,13 @@ export default defineConfig({
|
||||||
vite: {
|
vite: {
|
||||||
server: {
|
server: {
|
||||||
cors: {
|
cors: {
|
||||||
credentials: true
|
credentials: true,
|
||||||
},
|
},
|
||||||
proxy: {
|
proxy: {
|
||||||
'/api': {
|
'/api': {
|
||||||
target: 'http://127.0.0.1:8085',
|
target: 'http://127.0.0.1:8085',
|
||||||
changeOrigin: true,
|
changeOrigin: true,
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -41,36 +41,36 @@ const routes = [
|
||||||
match: /\/api\/cart/,
|
match: /\/api\/cart/,
|
||||||
async handle(req, res) {
|
async handle(req, res) {
|
||||||
res.writeHead(200, {
|
res.writeHead(200, {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json',
|
||||||
});
|
});
|
||||||
let cookie = req.headers.cookie;
|
let cookie = req.headers.cookie;
|
||||||
let userId = cookie ? lightcookie.parse(cookie)['user-id'] : '1'; // default for testing
|
let userId = cookie ? lightcookie.parse(cookie)['user-id'] : '1'; // default for testing
|
||||||
if(!userId || !userCartItems.has(userId)) {
|
if (!userId || !userCartItems.has(userId)) {
|
||||||
res.end(JSON.stringify({ items: [] }));
|
res.end(JSON.stringify({ items: [] }));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let items = userCartItems.get(userId);
|
let items = userCartItems.get(userId);
|
||||||
let array = Array.from(items.values());
|
let array = Array.from(items.values());
|
||||||
res.end(JSON.stringify({ items: array }));
|
res.end(JSON.stringify({ items: array }));
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
match: /\/api\/add-to-cart/,
|
match: /\/api\/add-to-cart/,
|
||||||
async handle(req, res) {
|
async handle(req, res) {
|
||||||
let body = '';
|
let body = '';
|
||||||
req.on('data', chunk => body += chunk);
|
req.on('data', (chunk) => (body += chunk));
|
||||||
return new Promise(resolve => {
|
return new Promise((resolve) => {
|
||||||
req.on('end', () => {
|
req.on('end', () => {
|
||||||
let cookie = req.headers.cookie;
|
let cookie = req.headers.cookie;
|
||||||
let userId = lightcookie.parse(cookie)['user-id'];
|
let userId = lightcookie.parse(cookie)['user-id'];
|
||||||
let msg = JSON.parse(body);
|
let msg = JSON.parse(body);
|
||||||
|
|
||||||
if(!userCartItems.has(userId)) {
|
if (!userCartItems.has(userId)) {
|
||||||
userCartItems.set(userId, new Map());
|
userCartItems.set(userId, new Map());
|
||||||
}
|
}
|
||||||
|
|
||||||
let cart = userCartItems.get(userId);
|
let cart = userCartItems.get(userId);
|
||||||
if(cart.has(msg.id)) {
|
if (cart.has(msg.id)) {
|
||||||
cart.get(msg.id).count++;
|
cart.get(msg.id).count++;
|
||||||
} else {
|
} else {
|
||||||
cart.set(msg.id, { id: msg.id, name: msg.name, count: 1 });
|
cart.set(msg.id, { id: msg.id, name: msg.name, count: 1 });
|
||||||
|
@ -82,8 +82,8 @@ const routes = [
|
||||||
res.end(JSON.stringify({ ok: true }));
|
res.end(JSON.stringify({ ok: true }));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
export async function apiHandler(req, res) {
|
export async function apiHandler(req, res) {
|
||||||
|
|
|
@ -22,7 +22,7 @@ const origin = MODE === 'development' ? `http://127.0.0.1:3000` : `http://127.0.
|
||||||
|
|
||||||
async function get<T>(endpoint: string, cb: (response: Response) => Promise<T>): Promise<T> {
|
async function get<T>(endpoint: string, cb: (response: Response) => Promise<T>): Promise<T> {
|
||||||
const response = await fetch(`${origin}${endpoint}`, {
|
const response = await fetch(`${origin}${endpoint}`, {
|
||||||
credentials: 'same-origin'
|
credentials: 'same-origin',
|
||||||
});
|
});
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
// TODO make this better...
|
// TODO make this better...
|
||||||
|
@ -46,14 +46,14 @@ export async function getProduct(id: number): Promise<Product> {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getUser(): Promise<User> {
|
export async function getUser(): Promise<User> {
|
||||||
return get<User>(`/api/user`, async response => {
|
return get<User>(`/api/user`, async (response) => {
|
||||||
const user: User = await response.json();
|
const user: User = await response.json();
|
||||||
return user;
|
return user;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getCart(): Promise<Cart> {
|
export async function getCart(): Promise<Cart> {
|
||||||
return get<Cart>(`/api/cart`, async response => {
|
return get<Cart>(`/api/cart`, async (response) => {
|
||||||
const cart: Cart = await response.json();
|
const cart: Cart = await response.json();
|
||||||
return cart;
|
return cart;
|
||||||
});
|
});
|
||||||
|
@ -66,11 +66,11 @@ export async function addToUserCart(id: number | string, name: string): Promise<
|
||||||
mode: 'no-cors',
|
mode: 'no-cors',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Cache': 'no-cache'
|
Cache: 'no-cache',
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
id,
|
id,
|
||||||
name
|
name,
|
||||||
})
|
}),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import lightcookie from 'lightcookie';
|
import lightcookie from 'lightcookie';
|
||||||
|
|
||||||
|
|
||||||
export function isLoggedIn(request: Request): boolean {
|
export function isLoggedIn(request: Request): boolean {
|
||||||
const cookie = request.headers.get('cookie');
|
const cookie = request.headers.get('cookie');
|
||||||
const parsed = lightcookie.parse(cookie);
|
const parsed = lightcookie.parse(cookie);
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
|
|
||||||
export function post(params, request) {
|
export function post(params, request) {
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
status: 301,
|
status: 301,
|
||||||
headers: {
|
headers: {
|
||||||
'Location': '/',
|
Location: '/',
|
||||||
'Set-Cookie': 'user-id=1; Path=/; Max-Age=2592000'
|
'Set-Cookie': 'user-id=1; Path=/; Max-Age=2592000',
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,7 +37,7 @@ export class App {
|
||||||
if (!routeData) {
|
if (!routeData) {
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
status: 404,
|
status: 404,
|
||||||
statusText: 'Not found'
|
statusText: 'Not found',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,13 +75,13 @@ export class App {
|
||||||
headers: request.headers,
|
headers: request.headers,
|
||||||
});
|
});
|
||||||
|
|
||||||
if(result.type === 'response') {
|
if (result.type === 'response') {
|
||||||
return result.response;
|
return result.response;
|
||||||
}
|
}
|
||||||
|
|
||||||
let html = result.html;
|
let html = result.html;
|
||||||
return new Response(html, {
|
return new Response(html, {
|
||||||
status: 200
|
status: 200,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
async #loadRenderers(): Promise<Renderer[]> {
|
async #loadRenderers(): Promise<Renderer[]> {
|
||||||
|
|
|
@ -11,7 +11,7 @@ function createRequestFromNodeRequest(req: IncomingMessage): Request {
|
||||||
const entries = Object.entries(req.headers as Record<string, any>);
|
const entries = Object.entries(req.headers as Record<string, any>);
|
||||||
let request = new Request(url, {
|
let request = new Request(url, {
|
||||||
method: req.method || 'GET',
|
method: req.method || 'GET',
|
||||||
headers: new Headers(entries)
|
headers: new Headers(entries),
|
||||||
});
|
});
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
|
@ -402,23 +402,22 @@ async function generatePath(pathname: string, opts: StaticBuildOptions, gopts: G
|
||||||
route: pageData.route,
|
route: pageData.route,
|
||||||
routeCache,
|
routeCache,
|
||||||
site: astroConfig.buildOptions.site,
|
site: astroConfig.buildOptions.site,
|
||||||
ssr: opts.astroConfig.buildOptions.experimentalSsr
|
ssr: opts.astroConfig.buildOptions.experimentalSsr,
|
||||||
}
|
};
|
||||||
|
|
||||||
let body: string;
|
let body: string;
|
||||||
if(pageData.route.type === 'endpoint') {
|
if (pageData.route.type === 'endpoint') {
|
||||||
|
|
||||||
const result = await callEndpoint(mod as unknown as EndpointHandler, options);
|
const result = await callEndpoint(mod as unknown as EndpointHandler, options);
|
||||||
|
|
||||||
if(result.type === 'response') {
|
if (result.type === 'response') {
|
||||||
throw new Error(`Returning a Response from an endpoint is not supported in SSG mode.`)
|
throw new Error(`Returning a Response from an endpoint is not supported in SSG mode.`);
|
||||||
}
|
}
|
||||||
body = result.body;
|
body = result.body;
|
||||||
} else {
|
} else {
|
||||||
const result = await render(options);
|
const result = await render(options);
|
||||||
|
|
||||||
// If there's a redirect or something, just do nothing.
|
// If there's a redirect or something, just do nothing.
|
||||||
if(result.type !== 'html') {
|
if (result.type !== 'html') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
body = result.html;
|
body = result.html;
|
||||||
|
|
|
@ -7,15 +7,13 @@ import { call as callEndpoint } from '../index.js';
|
||||||
import { getParamsAndProps, GetParamsAndPropsError } from '../../render/core.js';
|
import { getParamsAndProps, GetParamsAndPropsError } from '../../render/core.js';
|
||||||
import { createRequest } from '../../render/request.js';
|
import { createRequest } from '../../render/request.js';
|
||||||
|
|
||||||
|
|
||||||
export async function call(ssrOpts: SSROptions) {
|
export async function call(ssrOpts: SSROptions) {
|
||||||
try {
|
try {
|
||||||
const [, mod] = await preload(ssrOpts);
|
const [, mod] = await preload(ssrOpts);
|
||||||
return await callEndpoint(mod as unknown as EndpointHandler, {
|
return await callEndpoint(mod as unknown as EndpointHandler, {
|
||||||
...ssrOpts,
|
...ssrOpts,
|
||||||
ssr: ssrOpts.astroConfig.buildOptions.experimentalSsr
|
ssr: ssrOpts.astroConfig.buildOptions.experimentalSsr,
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
await errorHandler(e, { viteServer: ssrOpts.viteServer, filePath: ssrOpts.filePath });
|
await errorHandler(e, { viteServer: ssrOpts.viteServer, filePath: ssrOpts.filePath });
|
||||||
throw e;
|
throw e;
|
||||||
|
|
|
@ -4,48 +4,38 @@ import { renderEndpoint } from '../../runtime/server/index.js';
|
||||||
import { getParamsAndProps, GetParamsAndPropsError } from '../render/core.js';
|
import { getParamsAndProps, GetParamsAndPropsError } from '../render/core.js';
|
||||||
import { createRequest } from '../render/request.js';
|
import { createRequest } from '../render/request.js';
|
||||||
|
|
||||||
export type EndpointOptions = Pick<RenderOptions,
|
export type EndpointOptions = Pick<RenderOptions, 'logging' | 'headers' | 'method' | 'origin' | 'route' | 'routeCache' | 'pathname' | 'route' | 'site' | 'ssr'>;
|
||||||
'logging' |
|
|
||||||
'headers' |
|
|
||||||
'method' |
|
|
||||||
'origin' |
|
|
||||||
'route' |
|
|
||||||
'routeCache' |
|
|
||||||
'pathname' |
|
|
||||||
'route' |
|
|
||||||
'site' |
|
|
||||||
'ssr'
|
|
||||||
>;
|
|
||||||
|
|
||||||
type EndpointCallResult = {
|
type EndpointCallResult =
|
||||||
type: 'simple',
|
| {
|
||||||
body: string
|
type: 'simple';
|
||||||
} | {
|
body: string;
|
||||||
type: 'response',
|
}
|
||||||
response: Response
|
| {
|
||||||
};
|
type: 'response';
|
||||||
|
response: Response;
|
||||||
|
};
|
||||||
|
|
||||||
export async function call(mod: EndpointHandler, opts: EndpointOptions): Promise<EndpointCallResult> {
|
export async function call(mod: EndpointHandler, opts: EndpointOptions): Promise<EndpointCallResult> {
|
||||||
const paramsAndPropsResp = await getParamsAndProps({ ...opts, mod: (mod as any) });
|
const paramsAndPropsResp = await getParamsAndProps({ ...opts, mod: mod as any });
|
||||||
|
|
||||||
if (paramsAndPropsResp === GetParamsAndPropsError.NoMatchingStaticPath) {
|
if (paramsAndPropsResp === GetParamsAndPropsError.NoMatchingStaticPath) {
|
||||||
throw new Error(`[getStaticPath] route pattern matched, but no matching static path found. (${opts.pathname})`);
|
throw new Error(`[getStaticPath] route pattern matched, but no matching static path found. (${opts.pathname})`);
|
||||||
}
|
}
|
||||||
const [params] = paramsAndPropsResp;
|
const [params] = paramsAndPropsResp;
|
||||||
const request = createRequest(opts.method, opts.pathname, opts.headers, opts.origin,
|
const request = createRequest(opts.method, opts.pathname, opts.headers, opts.origin, opts.site, opts.ssr);
|
||||||
opts.site, opts.ssr);
|
|
||||||
|
|
||||||
const response = await renderEndpoint(mod, request, params);
|
const response = await renderEndpoint(mod, request, params);
|
||||||
|
|
||||||
if(response instanceof Response) {
|
if (response instanceof Response) {
|
||||||
return {
|
return {
|
||||||
type: 'response',
|
type: 'response',
|
||||||
response
|
response,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
type: 'simple',
|
type: 'simple',
|
||||||
body: response.body
|
body: response.body,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,7 +74,7 @@ export interface RenderOptions {
|
||||||
headers: Headers;
|
headers: Headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function render(opts: RenderOptions): Promise<{ type: 'html', html: string } | { type: 'response', response: Response }> {
|
export async function render(opts: RenderOptions): Promise<{ type: 'html'; html: string } | { type: 'response'; response: Response }> {
|
||||||
const { headers, legacyBuild, links, logging, origin, markdownRender, method, mod, pathname, scripts, renderers, resolve, route, routeCache, site, ssr } = opts;
|
const { headers, legacyBuild, links, logging, origin, markdownRender, method, mod, pathname, scripts, renderers, resolve, route, routeCache, site, ssr } = opts;
|
||||||
|
|
||||||
const paramsAndPropsRes = await getParamsAndProps({
|
const paramsAndPropsRes = await getParamsAndProps({
|
||||||
|
@ -109,12 +109,12 @@ export async function render(opts: RenderOptions): Promise<{ type: 'html', html:
|
||||||
scripts,
|
scripts,
|
||||||
ssr,
|
ssr,
|
||||||
method,
|
method,
|
||||||
headers
|
headers,
|
||||||
});
|
});
|
||||||
|
|
||||||
let page = await renderPage(result, Component, pageProps, null);
|
let page = await renderPage(result, Component, pageProps, null);
|
||||||
|
|
||||||
if(page.type === 'response') {
|
if (page.type === 'response') {
|
||||||
return page;
|
return page;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,6 +133,6 @@ export async function render(opts: RenderOptions): Promise<{ type: 'html', html:
|
||||||
|
|
||||||
return {
|
return {
|
||||||
type: 'html',
|
type: 'html',
|
||||||
html
|
html,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,9 +41,7 @@ export interface SSROptions {
|
||||||
|
|
||||||
export type ComponentPreload = [Renderer[], ComponentInstance];
|
export type ComponentPreload = [Renderer[], ComponentInstance];
|
||||||
|
|
||||||
export type RenderResponse =
|
export type RenderResponse = { type: 'html'; html: string } | { type: 'response'; response: Response };
|
||||||
{ type: 'html', html: string } |
|
|
||||||
{ type: 'response', response: Response };
|
|
||||||
|
|
||||||
const svelteStylesRE = /svelte\?svelte&type=style/;
|
const svelteStylesRE = /svelte\?svelte&type=style/;
|
||||||
|
|
||||||
|
@ -186,7 +184,7 @@ export async function render(renderers: Renderer[], mod: ComponentInstance, ssrO
|
||||||
|
|
||||||
return {
|
return {
|
||||||
type: 'html',
|
type: 'html',
|
||||||
html
|
html,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,10 +18,9 @@ export interface AstroRequest {
|
||||||
method: string;
|
method: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type AstroRequestSSR = AstroRequest
|
export type AstroRequestSSR = AstroRequest;
|
||||||
|
|
||||||
export function createRequest(method: string, pathname: string, headers: Headers,
|
export function createRequest(method: string, pathname: string, headers: Headers, origin: string, site: Site, ssr: boolean): AstroRequest {
|
||||||
origin: string, site: Site, ssr: boolean): AstroRequest {
|
|
||||||
const url = new URL('.' + pathname, new URL(origin));
|
const url = new URL('.' + pathname, new URL(origin));
|
||||||
|
|
||||||
const canonicalURL = utilCanonicalURL('.' + pathname, site ?? url.origin);
|
const canonicalURL = utilCanonicalURL('.' + pathname, site ?? url.origin);
|
||||||
|
@ -31,10 +30,10 @@ export function createRequest(method: string, pathname: string, headers: Headers
|
||||||
canonicalURL,
|
canonicalURL,
|
||||||
params: {},
|
params: {},
|
||||||
headers,
|
headers,
|
||||||
method
|
method,
|
||||||
};
|
};
|
||||||
|
|
||||||
if(!ssr) {
|
if (!ssr) {
|
||||||
// Headers are only readable if using SSR-mode. If not, make it an empty headers
|
// Headers are only readable if using SSR-mode. If not, make it an empty headers
|
||||||
// object, so you can't do something bad.
|
// object, so you can't do something bad.
|
||||||
request.headers = new Headers();
|
request.headers = new Headers();
|
||||||
|
@ -42,7 +41,7 @@ export function createRequest(method: string, pathname: string, headers: Headers
|
||||||
// Disallow using query params.
|
// Disallow using query params.
|
||||||
request.url = new URL(request.url);
|
request.url = new URL(request.url);
|
||||||
|
|
||||||
for(const [key] of request.url.searchParams) {
|
for (const [key] of request.url.searchParams) {
|
||||||
request.url.searchParams.delete(key);
|
request.url.searchParams.delete(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,9 +9,9 @@ import { renderSlot } from '../../runtime/server/index.js';
|
||||||
import { warn, LogOptions } from '../logger.js';
|
import { warn, LogOptions } from '../logger.js';
|
||||||
|
|
||||||
function onlyAvailableInSSR(name: string) {
|
function onlyAvailableInSSR(name: string) {
|
||||||
return function() {
|
return function () {
|
||||||
// TODO add more guidance when we have docs and adapters.
|
// TODO add more guidance when we have docs and adapters.
|
||||||
throw new Error(`Oops, you are trying to use ${name}, which is only available with SSR.`)
|
throw new Error(`Oops, you are trying to use ${name}, which is only available with SSR.`);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,14 +94,16 @@ export function createResult(args: CreateResultArgs): SSRResult {
|
||||||
__proto__: astroGlobal,
|
__proto__: astroGlobal,
|
||||||
props,
|
props,
|
||||||
request,
|
request,
|
||||||
redirect: args.ssr ? (path: string) => {
|
redirect: args.ssr
|
||||||
return new Response(null, {
|
? (path: string) => {
|
||||||
status: 301,
|
return new Response(null, {
|
||||||
headers: {
|
status: 301,
|
||||||
Location: path
|
headers: {
|
||||||
}
|
Location: path,
|
||||||
});
|
},
|
||||||
} : onlyAvailableInSSR('Astro.redirect'),
|
});
|
||||||
|
}
|
||||||
|
: onlyAvailableInSSR('Astro.redirect'),
|
||||||
resolve(path: string) {
|
resolve(path: string) {
|
||||||
if (!legacyBuild) {
|
if (!legacyBuild) {
|
||||||
let extra = `This can be replaced with a dynamic import like so: await import("${path}")`;
|
let extra = `This can be replaced with a dynamic import like so: await import("${path}")`;
|
||||||
|
|
|
@ -434,10 +434,9 @@ async function replaceHeadInjection(result: SSRResult, html: string): Promise<st
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calls a component and renders it into a string of HTML
|
// Calls a component and renders it into a string of HTML
|
||||||
export async function renderToString(result: SSRResult, componentFactory: AstroComponentFactory,
|
export async function renderToString(result: SSRResult, componentFactory: AstroComponentFactory, props: any, children: any): Promise<string> {
|
||||||
props: any, children: any): Promise<string> {
|
|
||||||
const Component = await componentFactory(result, props, children);
|
const Component = await componentFactory(result, props, children);
|
||||||
if(!isAstroComponent(Component)) {
|
if (!isAstroComponent(Component)) {
|
||||||
throw new Error('Cannot return a Response from a nested component.');
|
throw new Error('Cannot return a Response from a nested component.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -450,20 +449,20 @@ export async function renderPage(
|
||||||
componentFactory: AstroComponentFactory,
|
componentFactory: AstroComponentFactory,
|
||||||
props: any,
|
props: any,
|
||||||
children: any
|
children: any
|
||||||
): Promise<{ type: 'html', html: string } | { type: 'response', response: Response }> {
|
): Promise<{ type: 'html'; html: string } | { type: 'response'; response: Response }> {
|
||||||
const response = await componentFactory(result, props, children);
|
const response = await componentFactory(result, props, children);
|
||||||
|
|
||||||
if(isAstroComponent(response)) {
|
if (isAstroComponent(response)) {
|
||||||
let template = await renderAstroComponent(response);
|
let template = await renderAstroComponent(response);
|
||||||
const html = await replaceHeadInjection(result, template);
|
const html = await replaceHeadInjection(result, template);
|
||||||
return {
|
return {
|
||||||
type: 'html',
|
type: 'html',
|
||||||
html
|
html,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
return {
|
return {
|
||||||
type: 'response',
|
type: 'response',
|
||||||
response
|
response,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,12 +43,12 @@ function writeHtmlResponse(res: http.ServerResponse, statusCode: number, html: s
|
||||||
async function writeWebResponse(res: http.ServerResponse, webResponse: Response) {
|
async function writeWebResponse(res: http.ServerResponse, webResponse: Response) {
|
||||||
const { status, headers, body } = webResponse;
|
const { status, headers, body } = webResponse;
|
||||||
res.writeHead(status, Object.fromEntries(headers.entries()));
|
res.writeHead(status, Object.fromEntries(headers.entries()));
|
||||||
if(body) {
|
if (body) {
|
||||||
const reader = body.getReader();
|
const reader = body.getReader();
|
||||||
while(true) {
|
while (true) {
|
||||||
const { done, value } = await reader.read();
|
const { done, value } = await reader.read();
|
||||||
if(done) break;
|
if (done) break;
|
||||||
if(value) {
|
if (value) {
|
||||||
res.write(value);
|
res.write(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,7 @@ async function writeWebResponse(res: http.ServerResponse, webResponse: Response)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function writeSSRResult(result: RenderResponse, res: http.ServerResponse, statusCode: 200 | 404) {
|
async function writeSSRResult(result: RenderResponse, res: http.ServerResponse, statusCode: 200 | 404) {
|
||||||
if(result.type === 'response') {
|
if (result.type === 'response') {
|
||||||
const { response } = result;
|
const { response } = result;
|
||||||
await writeWebResponse(res, response);
|
await writeWebResponse(res, response);
|
||||||
return;
|
return;
|
||||||
|
@ -194,9 +194,9 @@ async function handleRequest(
|
||||||
};
|
};
|
||||||
|
|
||||||
// Route successfully matched! Render it.
|
// Route successfully matched! Render it.
|
||||||
if(route.type === 'endpoint') {
|
if (route.type === 'endpoint') {
|
||||||
const result = await callEndpoint(options);
|
const result = await callEndpoint(options);
|
||||||
if(result.type === 'response') {
|
if (result.type === 'response') {
|
||||||
await writeWebResponse(res, result.response);
|
await writeWebResponse(res, result.response);
|
||||||
} else {
|
} else {
|
||||||
res.writeHead(200);
|
res.writeHead(200);
|
||||||
|
|
|
@ -97,7 +97,7 @@ export function rollupPluginAstroBuildHTML(options: PluginOptions): VitePlugin {
|
||||||
viteServer,
|
viteServer,
|
||||||
});
|
});
|
||||||
|
|
||||||
if(response.type !== 'html') {
|
if (response.type !== 'html') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue