Refactor passing compressHTML (#7585)

This commit is contained in:
Bjorn Lu 2023-07-06 20:29:57 +08:00 committed by GitHub
parent 2d9c621c77
commit a0a1ca3e58
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 20 additions and 28 deletions

View file

@ -1991,6 +1991,7 @@ export interface SSRResult {
* Map of directive name (e.g. `load`) to the directive script code
*/
clientDirectives: Map<string, string>;
compressHTML: boolean;
/**
* Only used for logging
*/

View file

@ -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<never>();
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);

View file

@ -38,7 +38,7 @@ export type SSRManifest = {
routes: RouteInfo[];
site?: string;
base: string;
compressHTML?: boolean;
compressHTML: boolean;
assetsPrefix?: string;
markdown: MarkdownRenderingOptions;
renderers: SSRLoadedRenderer[];

View file

@ -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,

View file

@ -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
);

View file

@ -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,

View file

@ -21,6 +21,7 @@ export interface Environment {
markdown: MarkdownRenderingOptions;
/** "development" or "production" */
mode: RuntimeMode;
compressHTML: boolean;
renderers: SSRLoadedRenderer[];
clientDirectives: Map<string, string>;
resolve: (s: string) => Promise<string>;

View file

@ -39,6 +39,7 @@ export interface CreateResultArgs {
pathname: string;
renderers: SSRLoadedRenderer[];
clientDirectives: Map<string, string>;
compressHTML: boolean;
resolve: (s: string) => Promise<string>;
/**
* 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<SSRElement>(),
scripts: args.scripts ?? new Set<SSRElement>(),
links: args.links ?? new Set<SSRElement>(),
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(

View file

@ -30,7 +30,6 @@ function nonAstroPageNeedsHeadInjection(pageComponent: NonAstroPageComponent): b
async function iterableToHTMLBytes(
result: SSRResult,
iterable: ComponentIterable,
isCompressHTML: boolean,
onDocTypeInjection?: (parts: HTMLParts) => Promise<void>
): Promise<Uint8Array> {
const parts = new HTMLParts();
@ -40,7 +39,7 @@ async function iterableToHTMLBytes(
if (i === 0) {
i++;
if (!/<!doctype html/i.test(String(chunk))) {
parts.append(`${isCompressHTML ? '<!DOCTYPE html>' : '<!DOCTYPE html>\n'}`, result);
parts.append(`${result.compressHTML ? '<!DOCTYPE html>' : '<!DOCTYPE html>\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<Response> {
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 (!/<!doctype html/i.test(String(chunk))) {
controller.enqueue(
encoder.encode(
`${isCompressHTML ? '<!DOCTYPE html>' : '<!DOCTYPE html>\n'}`
`${result.compressHTML ? '<!DOCTYPE html>' : '<!DOCTYPE html>\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());
}