From d2c0f04e8058e6953aa0f85793fd308e5207218b Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 24 Mar 2022 08:38:18 -0400 Subject: [PATCH] Updates based on initial code review --- packages/astro/src/core/app/index.ts | 15 ++++----------- packages/astro/src/core/app/node.ts | 2 +- packages/astro/src/core/build/static-build.ts | 2 +- packages/astro/src/core/build/vite-plugin-ssr.ts | 1 - packages/integrations/node/src/server.ts | 2 +- 5 files changed, 7 insertions(+), 15 deletions(-) diff --git a/packages/astro/src/core/app/index.ts b/packages/astro/src/core/app/index.ts index 9110e0206465..807f22336838 100644 --- a/packages/astro/src/core/app/index.ts +++ b/packages/astro/src/core/app/index.ts @@ -1,6 +1,7 @@ import type { ComponentInstance, EndpointHandler, ManifestData, RouteData } from '../../@types/astro'; import type { SSRManifest as Manifest, RouteInfo } from './types'; +import { getType as getMimeType } from 'mime'; import { defaultLogOptions } from '../logger.js'; export { deserializeManifest } from './common.js'; import { matchRoute } from '../routing/match.js'; @@ -10,10 +11,6 @@ import { RouteCache } from '../render/route-cache.js'; import { createLinkStylesheetElementSet, createModuleScriptElementWithSrcSet } from '../render/ssr-element.js'; import { prependForwardSlash } from '../path.js'; -const supportedFileNameToMimeTypes = new Map([ - ['json', 'application/json'] -]); - export class App { #manifest: Manifest; #manifestData: ManifestData; @@ -28,7 +25,6 @@ export class App { }; this.#routeDataToRouteInfo = new Map(manifest.routes.map((route) => [route.routeData, route])); this.#routeCache = new RouteCache(defaultLogOptions); - this } match(request: Request): RouteData | undefined { const url = new URL(request.url); @@ -121,13 +117,10 @@ export class App { return result.response; } else { const body = result.body; - const ext = /\.([a-z]+)/.exec(url.pathname); const headers = new Headers(); - if(ext) { - const mime = supportedFileNameToMimeTypes.get(ext[1]); - if(mime) { - headers.set('Content-Type', mime); - } + const mimeType = getMimeType(url.pathname); + if(mimeType) { + headers.set('Content-Type', mimeType); } const bytes = this.#encoder.encode(body); headers.set('Content-Length', bytes.byteLength.toString()); diff --git a/packages/astro/src/core/app/node.ts b/packages/astro/src/core/app/node.ts index 310244c7423d..8eee93d0f88d 100644 --- a/packages/astro/src/core/app/node.ts +++ b/packages/astro/src/core/app/node.ts @@ -33,5 +33,5 @@ export async function loadManifest(rootFolder: URL): Promise { export async function loadApp(rootFolder: URL): Promise { const manifest = await loadManifest(rootFolder); - return new NodeApp(manifest, rootFolder); + return new NodeApp(manifest); } diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index b109b1f019cf..217bf6c06280 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -90,7 +90,7 @@ export async function staticBuild(opts: StaticBuildOptions) { if (opts.buildConfig.staticMode) { await generatePages(ssrResult, opts, internals, facadeIdToPageDataMap); - //await cleanSsrOutput(opts); + await cleanSsrOutput(opts); } else { await ssrMoveAssets(opts); } diff --git a/packages/astro/src/core/build/vite-plugin-ssr.ts b/packages/astro/src/core/build/vite-plugin-ssr.ts index c6789ec96873..753d9b592a78 100644 --- a/packages/astro/src/core/build/vite-plugin-ssr.ts +++ b/packages/astro/src/core/build/vite-plugin-ssr.ts @@ -62,7 +62,6 @@ if(_start in adapter) { chunk.code = code.replace(exp, () => { return JSON.stringify(manifest); }); - chunk.fileName = 'entry.mjs'; } } }, diff --git a/packages/integrations/node/src/server.ts b/packages/integrations/node/src/server.ts index 3f015bc7d7f1..d643e83679cb 100644 --- a/packages/integrations/node/src/server.ts +++ b/packages/integrations/node/src/server.ts @@ -9,7 +9,7 @@ polyfill(globalThis, { }); export function createExports(manifest: SSRManifest) { - const app = new NodeApp(manifest, new URL(import.meta.url)); + const app = new NodeApp(manifest); return { async handler(req: IncomingMessage, res: ServerResponse, next?: (err?: unknown) => void) { const route = app.match(req);