From 890a2bc9891a2449ab99b01b65468f6dddba6b12 Mon Sep 17 00:00:00 2001 From: Jerry_wu <409187100@qq.com> Date: Mon, 5 Jun 2023 16:02:04 +0800 Subject: [PATCH] =?UTF-8?q?remove=20the=20white=20space=20after=20the=20do?= =?UTF-8?q?ctype=20according=20to=20the=20property=20co=E2=80=A6=20(#7242)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/tasty-stingrays-smile.md | 5 +++++ packages/astro/src/core/build/generate.ts | 4 ++-- packages/astro/src/core/render/core.ts | 4 +++- packages/astro/src/runtime/server/render/page.ts | 10 ++++++---- packages/astro/test/minification-html.test.js | 4 ++-- 5 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 .changeset/tasty-stingrays-smile.md diff --git a/.changeset/tasty-stingrays-smile.md b/.changeset/tasty-stingrays-smile.md new file mode 100644 index 000000000..fe99fe392 --- /dev/null +++ b/.changeset/tasty-stingrays-smile.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +remove the white space after the doctype according to the property compressHTML diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index a12313987..0c1e077aa 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -506,11 +506,11 @@ async function generatePath( onRequest as MiddlewareResponseHandler, apiContext, () => { - return renderPage({ mod, renderContext, env, apiContext }); + return renderPage({ mod, renderContext, env, apiContext, isCompressHTML: settings.config.compressHTML }); } ); } else { - response = await renderPage({ mod, renderContext, env, apiContext }); + response = await renderPage({ mod, renderContext, env, apiContext, isCompressHTML: settings.config.compressHTML }); } } catch (err) { if (!AstroError.is(err) && !(err as SSRError).id && typeof err === 'object') { diff --git a/packages/astro/src/core/render/core.ts b/packages/astro/src/core/render/core.ts index 24bec9d30..4ca822583 100644 --- a/packages/astro/src/core/render/core.ts +++ b/packages/astro/src/core/render/core.ts @@ -108,9 +108,10 @@ export type RenderPage = { renderContext: RenderContext; env: Environment; apiContext?: APIContext; + isCompressHTML?: boolean }; -export async function renderPage({ mod, renderContext, env, apiContext }: RenderPage) { +export async function renderPage({ mod, renderContext, env, apiContext, isCompressHTML = false }: RenderPage) { // Validate the page component before rendering the page const Component = mod.default; if (!Component) @@ -152,6 +153,7 @@ export async function renderPage({ mod, renderContext, env, apiContext }: Render renderContext.props, null, env.streaming, + isCompressHTML, renderContext.route ); diff --git a/packages/astro/src/runtime/server/render/page.ts b/packages/astro/src/runtime/server/render/page.ts index 45ab4236e..5bbca039f 100644 --- a/packages/astro/src/runtime/server/render/page.ts +++ b/packages/astro/src/runtime/server/render/page.ts @@ -30,6 +30,7 @@ function nonAstroPageNeedsHeadInjection(pageComponent: NonAstroPageComponent): b async function iterableToHTMLBytes( result: SSRResult, iterable: ComponentIterable, + isCompressHTML: boolean, onDocTypeInjection?: (parts: HTMLParts) => Promise ): Promise { const parts = new HTMLParts(); @@ -39,7 +40,7 @@ async function iterableToHTMLBytes( if (i === 0) { i++; if (!/\n', result); + parts.append(`${isCompressHTML ? '' : '\n'}`, result); if (onDocTypeInjection) { await onDocTypeInjection(parts); } @@ -73,6 +74,7 @@ export async function renderPage( props: any, children: any, streaming: boolean, + isCompressHTML: boolean, route?: RouteData | undefined ): Promise { if (!isAstroComponentFactory(componentFactory)) { @@ -113,7 +115,7 @@ export async function renderPage( } // Accumulate the HTML string and append the head if necessary. - const bytes = await iterableToHTMLBytes(result, output, async (parts) => { + const bytes = await iterableToHTMLBytes(result, output, isCompressHTML, async (parts) => { parts.append(head, result); }); @@ -152,7 +154,7 @@ export async function renderPage( if (isHTMLString(chunk)) { if (i === 0) { if (!/\n')); + controller.enqueue(encoder.encode(`${isCompressHTML ? '' : '\n'}`)); } } } @@ -187,7 +189,7 @@ export async function renderPage( }, }); } else { - body = await iterableToHTMLBytes(result, iterable); + body = await iterableToHTMLBytes(result, iterable, isCompressHTML); headers.set('Content-Length', body.byteLength.toString()); } diff --git a/packages/astro/test/minification-html.test.js b/packages/astro/test/minification-html.test.js index 818d9a76d..cad8db304 100644 --- a/packages/astro/test/minification-html.test.js +++ b/packages/astro/test/minification-html.test.js @@ -5,7 +5,7 @@ import testAdapter from './test-adapter.js'; const NEW_LINES = /[\r\n]+/gm; /** - * The doctype declaration is on a line between the rest of the HTML. + * The doctype declaration is on a line between the rest of the HTML in SSG. * This function removes the doctype so that we can check if the rest of the HTML is without * whitespace. */ @@ -53,7 +53,7 @@ describe('HTML minification', () => { it('should emit compressed HTML in the emitted file', async () => { const html = await fixture.readFile('/index.html'); - expect(NEW_LINES.test(removeDoctypeLine(html))).to.equal(false); + expect(NEW_LINES.test(html)).to.equal(false); }); });