Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove the white space after the doctype according to the property co… #7242

Merged
merged 7 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tasty-stingrays-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

remove the white space after the doctype according to the property compressHTML
4 changes: 2 additions & 2 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
4 changes: 3 additions & 1 deletion packages/astro/src/core/render/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -152,6 +153,7 @@ export async function renderPage({ mod, renderContext, env, apiContext }: Render
renderContext.props,
null,
env.streaming,
isCompressHTML,
renderContext.route
);

Expand Down
10 changes: 6 additions & 4 deletions packages/astro/src/runtime/server/render/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
Expand Down Expand Up @@ -73,6 +74,7 @@ export async function renderPage(
props: any,
children: any,
streaming: boolean,
isCompressHTML: boolean,
route?: RouteData | undefined
): Promise<Response> {
if (!isAstroComponentFactory(componentFactory)) {
Expand Down Expand Up @@ -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);
});

Expand Down Expand Up @@ -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'}`));
}
}
}
Expand Down Expand Up @@ -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());
}

Expand Down
4 changes: 2 additions & 2 deletions packages/astro/test/minification-html.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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);
});
});

Expand Down