From a0a1ca3e58d98a38cfc84cc03cd09ce2c508911f Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Thu, 6 Jul 2023 20:29:57 +0800 Subject: [PATCH] Refactor passing compressHTML (#7585) --- packages/astro/src/@types/astro.ts | 1 + packages/astro/src/core/app/index.ts | 4 +--- packages/astro/src/core/app/types.ts | 2 +- packages/astro/src/core/build/generate.ts | 4 ++-- packages/astro/src/core/render/core.ts | 11 ++--------- packages/astro/src/core/render/dev/environment.ts | 1 + packages/astro/src/core/render/environment.ts | 1 + packages/astro/src/core/render/result.ts | 14 +++++++------- packages/astro/src/runtime/server/render/page.ts | 10 ++++------ 9 files changed, 20 insertions(+), 28 deletions(-) diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 83b426720..c25879806 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -1991,6 +1991,7 @@ export interface SSRResult { * Map of directive name (e.g. `load`) to the directive script code */ clientDirectives: Map; + compressHTML: boolean; /** * Only used for logging */ diff --git a/packages/astro/src/core/app/index.ts b/packages/astro/src/core/app/index.ts index a9586dd8b..3b82fa983 100644 --- a/packages/astro/src/core/app/index.ts +++ b/packages/astro/src/core/app/index.ts @@ -63,6 +63,7 @@ export class App { logging: this.#logging, markdown: manifest.markdown, mode: 'production', + compressHTML: manifest.compressHTML, renderers: manifest.renderers, clientDirectives: manifest.clientDirectives, async resolve(specifier: string) { @@ -204,7 +205,6 @@ export class App { const url = new URL(request.url); const pathname = prependForwardSlash(this.removeBase(url.pathname)); const info = this.#routeDataToRouteInfo.get(routeData)!; - const isCompressHTML = this.#manifest.compressHTML ?? false; // may be used in the future for handling rel=modulepreload, rel=icon, rel=manifest etc. const links = new Set(); const styles = createStylesheetElementSet(info.styles); @@ -257,7 +257,6 @@ export class App { renderContext, env: this.#env, cookies: apiContext.cookies, - isCompressHTML, }); } ); @@ -267,7 +266,6 @@ export class App { renderContext, env: this.#env, cookies: apiContext.cookies, - isCompressHTML, }); } Reflect.set(request, responseSentSymbol, true); diff --git a/packages/astro/src/core/app/types.ts b/packages/astro/src/core/app/types.ts index 266fa9488..67d16d457 100644 --- a/packages/astro/src/core/app/types.ts +++ b/packages/astro/src/core/app/types.ts @@ -38,7 +38,7 @@ export type SSRManifest = { routes: RouteInfo[]; site?: string; base: string; - compressHTML?: boolean; + compressHTML: boolean; assetsPrefix?: string; markdown: MarkdownRenderingOptions; renderers: SSRLoadedRenderer[]; diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index 6992e1d61..b0ea26ab1 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -519,6 +519,7 @@ async function generatePath( mode: opts.mode, renderers: manifest.renderers, clientDirectives: manifest.clientDirectives, + compressHTML: manifest.compressHTML, async resolve(specifier: string) { // NOTE: next PR, borrow logic from build manifest maybe? const hashedFilePath = internals.entrySpecifierToBundleMap.get(specifier); @@ -593,7 +594,6 @@ async function generatePath( mod, renderContext, env, - isCompressHTML: settings.config.compressHTML, cookies: apiContext.cookies, }); } @@ -603,7 +603,6 @@ async function generatePath( mod, renderContext, env, - isCompressHTML: settings.config.compressHTML, cookies: apiContext.cookies, }); } @@ -660,6 +659,7 @@ export function generateRuntimeManifest( adapterName: '', markdown: settings.config.markdown, clientDirectives: settings.clientDirectives, + compressHTML: settings.config.compressHTML, renderers, base: settings.config.base, assetsPrefix: settings.config.build.assetsPrefix, diff --git a/packages/astro/src/core/render/core.ts b/packages/astro/src/core/render/core.ts index 1c91cebea..25834357e 100644 --- a/packages/astro/src/core/render/core.ts +++ b/packages/astro/src/core/render/core.ts @@ -10,17 +10,10 @@ export type RenderPage = { mod: ComponentInstance; renderContext: RenderContext; env: Environment; - isCompressHTML?: boolean; cookies: AstroCookies; }; -export async function renderPage({ - mod, - renderContext, - env, - cookies, - isCompressHTML = false, -}: RenderPage) { +export async function renderPage({ mod, renderContext, env, cookies }: RenderPage) { if (routeIsRedirect(renderContext.route)) { return new Response(null, { status: redirectRouteStatus(renderContext.route, renderContext.request.method), @@ -47,6 +40,7 @@ export async function renderPage({ resolve: env.resolve, renderers: env.renderers, clientDirectives: env.clientDirectives, + compressHTML: env.compressHTML, request: renderContext.request, site: env.site, scripts: renderContext.scripts, @@ -67,7 +61,6 @@ export async function renderPage({ renderContext.props, null, env.streaming, - isCompressHTML, renderContext.route ); diff --git a/packages/astro/src/core/render/dev/environment.ts b/packages/astro/src/core/render/dev/environment.ts index ec7cd4534..e97b5df08 100644 --- a/packages/astro/src/core/render/dev/environment.ts +++ b/packages/astro/src/core/render/dev/environment.ts @@ -27,6 +27,7 @@ export function createDevelopmentEnvironment( // This will be overridden in the dev server renderers: [], clientDirectives: settings.clientDirectives, + compressHTML: settings.config.compressHTML, resolve: createResolve(loader, settings.config.root), routeCache: new RouteCache(logging, mode), site: settings.config.site, diff --git a/packages/astro/src/core/render/environment.ts b/packages/astro/src/core/render/environment.ts index 58f811c0b..f38773cfc 100644 --- a/packages/astro/src/core/render/environment.ts +++ b/packages/astro/src/core/render/environment.ts @@ -21,6 +21,7 @@ export interface Environment { markdown: MarkdownRenderingOptions; /** "development" or "production" */ mode: RuntimeMode; + compressHTML: boolean; renderers: SSRLoadedRenderer[]; clientDirectives: Map; resolve: (s: string) => Promise; diff --git a/packages/astro/src/core/render/result.ts b/packages/astro/src/core/render/result.ts index 9d72c455a..d86cce348 100644 --- a/packages/astro/src/core/render/result.ts +++ b/packages/astro/src/core/render/result.ts @@ -39,6 +39,7 @@ export interface CreateResultArgs { pathname: string; renderers: SSRLoadedRenderer[]; clientDirectives: Map; + compressHTML: boolean; resolve: (s: string) => Promise; /** * Used for `Astro.site` @@ -134,8 +135,7 @@ class Slots { let renderMarkdown: any = null; export function createResult(args: CreateResultArgs): SSRResult { - const { markdown, params, pathname, renderers, clientDirectives, request, resolve, locals } = - args; + const { markdown, params, request, resolve, locals } = args; const url = new URL(request.url); const headers = new Headers(); @@ -155,7 +155,6 @@ export function createResult(args: CreateResultArgs): SSRResult { // Astro.cookies is defined lazily to avoid the cost on pages that do not use it. let cookies: AstroCookies | undefined = args.cookies; - let componentMetadata = args.componentMetadata ?? new Map(); // Create the result object that will be passed into the render function. // This object starts here as an empty shell (not yet the result) but then @@ -164,10 +163,11 @@ export function createResult(args: CreateResultArgs): SSRResult { styles: args.styles ?? new Set(), scripts: args.scripts ?? new Set(), links: args.links ?? new Set(), - componentMetadata, - renderers, - clientDirectives, - pathname, + componentMetadata: args.componentMetadata ?? new Map(), + renderers: args.renderers, + clientDirectives: args.clientDirectives, + compressHTML: args.compressHTML, + pathname: args.pathname, cookies, /** This function returns the `Astro` faux-global */ createAstro( diff --git a/packages/astro/src/runtime/server/render/page.ts b/packages/astro/src/runtime/server/render/page.ts index ffbc11b9e..bd65484ad 100644 --- a/packages/astro/src/runtime/server/render/page.ts +++ b/packages/astro/src/runtime/server/render/page.ts @@ -30,7 +30,6 @@ function nonAstroPageNeedsHeadInjection(pageComponent: NonAstroPageComponent): b async function iterableToHTMLBytes( result: SSRResult, iterable: ComponentIterable, - isCompressHTML: boolean, onDocTypeInjection?: (parts: HTMLParts) => Promise ): Promise { const parts = new HTMLParts(); @@ -40,7 +39,7 @@ async function iterableToHTMLBytes( if (i === 0) { i++; if (!/' : '\n'}`, result); + parts.append(`${result.compressHTML ? '' : '\n'}`, result); if (onDocTypeInjection) { await onDocTypeInjection(parts); } @@ -74,7 +73,6 @@ export async function renderPage( props: any, children: any, streaming: boolean, - isCompressHTML: boolean, route?: RouteData | undefined ): Promise { if (!isAstroComponentFactory(componentFactory)) { @@ -115,7 +113,7 @@ export async function renderPage( } // Accumulate the HTML string and append the head if necessary. - const bytes = await iterableToHTMLBytes(result, output, isCompressHTML, async (parts) => { + const bytes = await iterableToHTMLBytes(result, output, async (parts) => { parts.append(head, result); }); @@ -156,7 +154,7 @@ export async function renderPage( if (!/' : '\n'}` + `${result.compressHTML ? '' : '\n'}` ) ); } @@ -193,7 +191,7 @@ export async function renderPage( }, }); } else { - body = await iterableToHTMLBytes(result, iterable, isCompressHTML); + body = await iterableToHTMLBytes(result, iterable); headers.set('Content-Length', body.byteLength.toString()); }