Refactor passing compressHTML (#7585)
This commit is contained in:
parent
2d9c621c77
commit
a0a1ca3e58
9 changed files with 20 additions and 28 deletions
|
@ -1991,6 +1991,7 @@ export interface SSRResult {
|
||||||
* Map of directive name (e.g. `load`) to the directive script code
|
* Map of directive name (e.g. `load`) to the directive script code
|
||||||
*/
|
*/
|
||||||
clientDirectives: Map<string, string>;
|
clientDirectives: Map<string, string>;
|
||||||
|
compressHTML: boolean;
|
||||||
/**
|
/**
|
||||||
* Only used for logging
|
* Only used for logging
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -63,6 +63,7 @@ export class App {
|
||||||
logging: this.#logging,
|
logging: this.#logging,
|
||||||
markdown: manifest.markdown,
|
markdown: manifest.markdown,
|
||||||
mode: 'production',
|
mode: 'production',
|
||||||
|
compressHTML: manifest.compressHTML,
|
||||||
renderers: manifest.renderers,
|
renderers: manifest.renderers,
|
||||||
clientDirectives: manifest.clientDirectives,
|
clientDirectives: manifest.clientDirectives,
|
||||||
async resolve(specifier: string) {
|
async resolve(specifier: string) {
|
||||||
|
@ -204,7 +205,6 @@ export class App {
|
||||||
const url = new URL(request.url);
|
const url = new URL(request.url);
|
||||||
const pathname = prependForwardSlash(this.removeBase(url.pathname));
|
const pathname = prependForwardSlash(this.removeBase(url.pathname));
|
||||||
const info = this.#routeDataToRouteInfo.get(routeData)!;
|
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.
|
// may be used in the future for handling rel=modulepreload, rel=icon, rel=manifest etc.
|
||||||
const links = new Set<never>();
|
const links = new Set<never>();
|
||||||
const styles = createStylesheetElementSet(info.styles);
|
const styles = createStylesheetElementSet(info.styles);
|
||||||
|
@ -257,7 +257,6 @@ export class App {
|
||||||
renderContext,
|
renderContext,
|
||||||
env: this.#env,
|
env: this.#env,
|
||||||
cookies: apiContext.cookies,
|
cookies: apiContext.cookies,
|
||||||
isCompressHTML,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -267,7 +266,6 @@ export class App {
|
||||||
renderContext,
|
renderContext,
|
||||||
env: this.#env,
|
env: this.#env,
|
||||||
cookies: apiContext.cookies,
|
cookies: apiContext.cookies,
|
||||||
isCompressHTML,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Reflect.set(request, responseSentSymbol, true);
|
Reflect.set(request, responseSentSymbol, true);
|
||||||
|
|
|
@ -38,7 +38,7 @@ export type SSRManifest = {
|
||||||
routes: RouteInfo[];
|
routes: RouteInfo[];
|
||||||
site?: string;
|
site?: string;
|
||||||
base: string;
|
base: string;
|
||||||
compressHTML?: boolean;
|
compressHTML: boolean;
|
||||||
assetsPrefix?: string;
|
assetsPrefix?: string;
|
||||||
markdown: MarkdownRenderingOptions;
|
markdown: MarkdownRenderingOptions;
|
||||||
renderers: SSRLoadedRenderer[];
|
renderers: SSRLoadedRenderer[];
|
||||||
|
|
|
@ -519,6 +519,7 @@ async function generatePath(
|
||||||
mode: opts.mode,
|
mode: opts.mode,
|
||||||
renderers: manifest.renderers,
|
renderers: manifest.renderers,
|
||||||
clientDirectives: manifest.clientDirectives,
|
clientDirectives: manifest.clientDirectives,
|
||||||
|
compressHTML: manifest.compressHTML,
|
||||||
async resolve(specifier: string) {
|
async resolve(specifier: string) {
|
||||||
// NOTE: next PR, borrow logic from build manifest maybe?
|
// NOTE: next PR, borrow logic from build manifest maybe?
|
||||||
const hashedFilePath = internals.entrySpecifierToBundleMap.get(specifier);
|
const hashedFilePath = internals.entrySpecifierToBundleMap.get(specifier);
|
||||||
|
@ -593,7 +594,6 @@ async function generatePath(
|
||||||
mod,
|
mod,
|
||||||
renderContext,
|
renderContext,
|
||||||
env,
|
env,
|
||||||
isCompressHTML: settings.config.compressHTML,
|
|
||||||
cookies: apiContext.cookies,
|
cookies: apiContext.cookies,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -603,7 +603,6 @@ async function generatePath(
|
||||||
mod,
|
mod,
|
||||||
renderContext,
|
renderContext,
|
||||||
env,
|
env,
|
||||||
isCompressHTML: settings.config.compressHTML,
|
|
||||||
cookies: apiContext.cookies,
|
cookies: apiContext.cookies,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -660,6 +659,7 @@ export function generateRuntimeManifest(
|
||||||
adapterName: '',
|
adapterName: '',
|
||||||
markdown: settings.config.markdown,
|
markdown: settings.config.markdown,
|
||||||
clientDirectives: settings.clientDirectives,
|
clientDirectives: settings.clientDirectives,
|
||||||
|
compressHTML: settings.config.compressHTML,
|
||||||
renderers,
|
renderers,
|
||||||
base: settings.config.base,
|
base: settings.config.base,
|
||||||
assetsPrefix: settings.config.build.assetsPrefix,
|
assetsPrefix: settings.config.build.assetsPrefix,
|
||||||
|
|
|
@ -10,17 +10,10 @@ export type RenderPage = {
|
||||||
mod: ComponentInstance;
|
mod: ComponentInstance;
|
||||||
renderContext: RenderContext;
|
renderContext: RenderContext;
|
||||||
env: Environment;
|
env: Environment;
|
||||||
isCompressHTML?: boolean;
|
|
||||||
cookies: AstroCookies;
|
cookies: AstroCookies;
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function renderPage({
|
export async function renderPage({ mod, renderContext, env, cookies }: RenderPage) {
|
||||||
mod,
|
|
||||||
renderContext,
|
|
||||||
env,
|
|
||||||
cookies,
|
|
||||||
isCompressHTML = false,
|
|
||||||
}: RenderPage) {
|
|
||||||
if (routeIsRedirect(renderContext.route)) {
|
if (routeIsRedirect(renderContext.route)) {
|
||||||
return new Response(null, {
|
return new Response(null, {
|
||||||
status: redirectRouteStatus(renderContext.route, renderContext.request.method),
|
status: redirectRouteStatus(renderContext.route, renderContext.request.method),
|
||||||
|
@ -47,6 +40,7 @@ export async function renderPage({
|
||||||
resolve: env.resolve,
|
resolve: env.resolve,
|
||||||
renderers: env.renderers,
|
renderers: env.renderers,
|
||||||
clientDirectives: env.clientDirectives,
|
clientDirectives: env.clientDirectives,
|
||||||
|
compressHTML: env.compressHTML,
|
||||||
request: renderContext.request,
|
request: renderContext.request,
|
||||||
site: env.site,
|
site: env.site,
|
||||||
scripts: renderContext.scripts,
|
scripts: renderContext.scripts,
|
||||||
|
@ -67,7 +61,6 @@ export async function renderPage({
|
||||||
renderContext.props,
|
renderContext.props,
|
||||||
null,
|
null,
|
||||||
env.streaming,
|
env.streaming,
|
||||||
isCompressHTML,
|
|
||||||
renderContext.route
|
renderContext.route
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,7 @@ export function createDevelopmentEnvironment(
|
||||||
// This will be overridden in the dev server
|
// This will be overridden in the dev server
|
||||||
renderers: [],
|
renderers: [],
|
||||||
clientDirectives: settings.clientDirectives,
|
clientDirectives: settings.clientDirectives,
|
||||||
|
compressHTML: settings.config.compressHTML,
|
||||||
resolve: createResolve(loader, settings.config.root),
|
resolve: createResolve(loader, settings.config.root),
|
||||||
routeCache: new RouteCache(logging, mode),
|
routeCache: new RouteCache(logging, mode),
|
||||||
site: settings.config.site,
|
site: settings.config.site,
|
||||||
|
|
|
@ -21,6 +21,7 @@ export interface Environment {
|
||||||
markdown: MarkdownRenderingOptions;
|
markdown: MarkdownRenderingOptions;
|
||||||
/** "development" or "production" */
|
/** "development" or "production" */
|
||||||
mode: RuntimeMode;
|
mode: RuntimeMode;
|
||||||
|
compressHTML: boolean;
|
||||||
renderers: SSRLoadedRenderer[];
|
renderers: SSRLoadedRenderer[];
|
||||||
clientDirectives: Map<string, string>;
|
clientDirectives: Map<string, string>;
|
||||||
resolve: (s: string) => Promise<string>;
|
resolve: (s: string) => Promise<string>;
|
||||||
|
|
|
@ -39,6 +39,7 @@ export interface CreateResultArgs {
|
||||||
pathname: string;
|
pathname: string;
|
||||||
renderers: SSRLoadedRenderer[];
|
renderers: SSRLoadedRenderer[];
|
||||||
clientDirectives: Map<string, string>;
|
clientDirectives: Map<string, string>;
|
||||||
|
compressHTML: boolean;
|
||||||
resolve: (s: string) => Promise<string>;
|
resolve: (s: string) => Promise<string>;
|
||||||
/**
|
/**
|
||||||
* Used for `Astro.site`
|
* Used for `Astro.site`
|
||||||
|
@ -134,8 +135,7 @@ class Slots {
|
||||||
let renderMarkdown: any = null;
|
let renderMarkdown: any = null;
|
||||||
|
|
||||||
export function createResult(args: CreateResultArgs): SSRResult {
|
export function createResult(args: CreateResultArgs): SSRResult {
|
||||||
const { markdown, params, pathname, renderers, clientDirectives, request, resolve, locals } =
|
const { markdown, params, request, resolve, locals } = args;
|
||||||
args;
|
|
||||||
|
|
||||||
const url = new URL(request.url);
|
const url = new URL(request.url);
|
||||||
const headers = new Headers();
|
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.
|
// Astro.cookies is defined lazily to avoid the cost on pages that do not use it.
|
||||||
let cookies: AstroCookies | undefined = args.cookies;
|
let cookies: AstroCookies | undefined = args.cookies;
|
||||||
let componentMetadata = args.componentMetadata ?? new Map();
|
|
||||||
|
|
||||||
// Create the result object that will be passed into the render function.
|
// 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
|
// 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>(),
|
styles: args.styles ?? new Set<SSRElement>(),
|
||||||
scripts: args.scripts ?? new Set<SSRElement>(),
|
scripts: args.scripts ?? new Set<SSRElement>(),
|
||||||
links: args.links ?? new Set<SSRElement>(),
|
links: args.links ?? new Set<SSRElement>(),
|
||||||
componentMetadata,
|
componentMetadata: args.componentMetadata ?? new Map(),
|
||||||
renderers,
|
renderers: args.renderers,
|
||||||
clientDirectives,
|
clientDirectives: args.clientDirectives,
|
||||||
pathname,
|
compressHTML: args.compressHTML,
|
||||||
|
pathname: args.pathname,
|
||||||
cookies,
|
cookies,
|
||||||
/** This function returns the `Astro` faux-global */
|
/** This function returns the `Astro` faux-global */
|
||||||
createAstro(
|
createAstro(
|
||||||
|
|
|
@ -30,7 +30,6 @@ function nonAstroPageNeedsHeadInjection(pageComponent: NonAstroPageComponent): b
|
||||||
async function iterableToHTMLBytes(
|
async function iterableToHTMLBytes(
|
||||||
result: SSRResult,
|
result: SSRResult,
|
||||||
iterable: ComponentIterable,
|
iterable: ComponentIterable,
|
||||||
isCompressHTML: boolean,
|
|
||||||
onDocTypeInjection?: (parts: HTMLParts) => Promise<void>
|
onDocTypeInjection?: (parts: HTMLParts) => Promise<void>
|
||||||
): Promise<Uint8Array> {
|
): Promise<Uint8Array> {
|
||||||
const parts = new HTMLParts();
|
const parts = new HTMLParts();
|
||||||
|
@ -40,7 +39,7 @@ async function iterableToHTMLBytes(
|
||||||
if (i === 0) {
|
if (i === 0) {
|
||||||
i++;
|
i++;
|
||||||
if (!/<!doctype html/i.test(String(chunk))) {
|
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) {
|
if (onDocTypeInjection) {
|
||||||
await onDocTypeInjection(parts);
|
await onDocTypeInjection(parts);
|
||||||
}
|
}
|
||||||
|
@ -74,7 +73,6 @@ export async function renderPage(
|
||||||
props: any,
|
props: any,
|
||||||
children: any,
|
children: any,
|
||||||
streaming: boolean,
|
streaming: boolean,
|
||||||
isCompressHTML: boolean,
|
|
||||||
route?: RouteData | undefined
|
route?: RouteData | undefined
|
||||||
): Promise<Response> {
|
): Promise<Response> {
|
||||||
if (!isAstroComponentFactory(componentFactory)) {
|
if (!isAstroComponentFactory(componentFactory)) {
|
||||||
|
@ -115,7 +113,7 @@ export async function renderPage(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Accumulate the HTML string and append the head if necessary.
|
// 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);
|
parts.append(head, result);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -156,7 +154,7 @@ export async function renderPage(
|
||||||
if (!/<!doctype html/i.test(String(chunk))) {
|
if (!/<!doctype html/i.test(String(chunk))) {
|
||||||
controller.enqueue(
|
controller.enqueue(
|
||||||
encoder.encode(
|
encoder.encode(
|
||||||
`${isCompressHTML ? '<!DOCTYPE html>' : '<!DOCTYPE html>\n'}`
|
`${result.compressHTML ? '<!DOCTYPE html>' : '<!DOCTYPE html>\n'}`
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -193,7 +191,7 @@ export async function renderPage(
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
body = await iterableToHTMLBytes(result, iterable, isCompressHTML);
|
body = await iterableToHTMLBytes(result, iterable);
|
||||||
headers.set('Content-Length', body.byteLength.toString());
|
headers.set('Content-Length', body.byteLength.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue