summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jerry_wu <409187100@qq.com> 2023-06-05 16:02:04 +0800
committerGravatar GitHub <noreply@github.com> 2023-06-05 16:02:04 +0800
commit890a2bc9891a2449ab99b01b65468f6dddba6b12 (patch)
tree9cd3bf63c101a5d6fd0f6ad9057a58b357b232dc
parentef410fa30bb24962df61e825c970d411298f3750 (diff)
downloadastro-890a2bc9891a2449ab99b01b65468f6dddba6b12.tar.gz
astro-890a2bc9891a2449ab99b01b65468f6dddba6b12.tar.zst
astro-890a2bc9891a2449ab99b01b65468f6dddba6b12.zip
remove the white space after the doctype according to the property co… (#7242)
-rw-r--r--.changeset/tasty-stingrays-smile.md5
-rw-r--r--packages/astro/src/core/build/generate.ts4
-rw-r--r--packages/astro/src/core/render/core.ts4
-rw-r--r--packages/astro/src/runtime/server/render/page.ts10
-rw-r--r--packages/astro/test/minification-html.test.js4
5 files changed, 18 insertions, 9 deletions
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<void>
): Promise<Uint8Array> {
const parts = new HTMLParts();
@@ -39,7 +40,7 @@ async function iterableToHTMLBytes(
if (i === 0) {
i++;
if (!/<!doctype html/i.test(String(chunk))) {
- parts.append('<!DOCTYPE html>\n', result);
+ parts.append(`${isCompressHTML ? '<!DOCTYPE html>' : '<!DOCTYPE html>\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<Response> {
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 (!/<!doctype html/i.test(String(chunk))) {
- controller.enqueue(encoder.encode('<!DOCTYPE html>\n'));
+ controller.enqueue(encoder.encode(`${isCompressHTML ? '<!DOCTYPE html>' : '<!DOCTYPE html>\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);
});
});