From 529486bfb0d6f0ca54b091b000ed446dd1a085af Mon Sep 17 00:00:00 2001 From: Drew Powers <1369770+drwpow@users.noreply.github.com> Date: Thu, 11 Nov 2021 12:28:14 -0700 Subject: [PATCH] Inject Doctype tag (#1783) --- packages/astro/src/core/build/index.ts | 1 - packages/astro/src/core/ssr/index.ts | 5 +++ packages/astro/test/astro-doctype.test.js | 31 +++++++++---------- .../src/layouts/WithDoctype.astro | 2 +- .../src/layouts/WithoutDoctype.astro | 4 +-- .../astro-doctype/src/pages/preserve.astro | 2 +- 6 files changed, 22 insertions(+), 23 deletions(-) diff --git a/packages/astro/src/core/build/index.ts b/packages/astro/src/core/build/index.ts index 390362055..ed8529a77 100644 --- a/packages/astro/src/core/build/index.ts +++ b/packages/astro/src/core/build/index.ts @@ -201,7 +201,6 @@ class AstroBuilder { // Build your final sitemap. timer.sitemapStart = performance.now(); if (this.config.buildOptions.sitemap && this.config.buildOptions.site) { - const sitemapStart = performance.now(); const sitemap = generateSitemap(pageNames.map((pageName) => new URL(`/${pageName}`, this.config.buildOptions.site).href)); const sitemapPath = new URL('./sitemap.xml', this.config.dist); await fs.promises.mkdir(new URL('./', sitemapPath), { recursive: true }); diff --git a/packages/astro/src/core/ssr/index.ts b/packages/astro/src/core/ssr/index.ts index 4a634367c..598497144 100644 --- a/packages/astro/src/core/ssr/index.ts +++ b/packages/astro/src/core/ssr/index.ts @@ -197,6 +197,11 @@ export async function render(renderers: Renderer[], mod: ComponentInstance, ssrO let html = await renderPage(result, Component, pageProps, null); + // inject if missing (TODO: is a more robust check needed for comments, etc.?) + if (!/\n' + html; + } + // inject tags const tags: vite.HtmlTagDescriptor[] = []; diff --git a/packages/astro/test/astro-doctype.test.js b/packages/astro/test/astro-doctype.test.js index c467af78a..43087dcab 100644 --- a/packages/astro/test/astro-doctype.test.js +++ b/packages/astro/test/astro-doctype.test.js @@ -1,5 +1,3 @@ -/** - * UNCOMMENT: compiler doesn’t insert import { expect } from 'chai'; import cheerio from 'cheerio'; import { loadFixture } from './test-utils.js'; @@ -16,38 +14,40 @@ describe('Doctype', () => { const html = await fixture.readFile('/prepend/index.html'); // test that Doctype always included - expect(html).to.match(/^/); + expect(html).to.match(/^/i); }); it('No attributes added when doctype is provided by user', async () => { const html = await fixture.readFile('/provided/index.html'); // test that Doctype always included - expect(html).to.match(/^/); + expect(html).to.match(/^/i); }); - it('Preserves user provided doctype', async () => { - const html = await fixture.readFile('/preserve/index.html'); + // Note: parse5 converts this to (HTML5). Uncomment if we want to support legacy doctypes. + // + // it('Preserves user provided doctype', async () => { + // const html = await fixture.readFile('/preserve/index.html'); - // test that Doctype included was preserved - expect(html).to.match(new RegExp('^')); - }); + // // test that Doctype included was preserved + // expect(html).to.match(new RegExp('^', 'i')); + // }); it('User provided doctype is case insensitive', async () => { const html = await fixture.readFile('/capital/index.html'); // test 1: Doctype left alone - expect(html).to.match(/^/); + expect(html).to.match(/^/i); // test 2: no closing tag - expect(html).not.to.include(`!DOCTYPE>`); + expect(html).not.to.match(/<\/!DOCTYPE>/i); }); it('Doctype can be provided in a layout', async () => { const html = await fixture.readFile('/in-layout/index.html'); // test 1: doctype is at the front - expect(html).to.match(/^/); + expect(html).to.match(/^/i); // test 2: A link inside of the head const $ = cheerio.load(html); @@ -58,20 +58,17 @@ describe('Doctype', () => { const html = await fixture.readFile('/in-layout-no-doctype/index.html'); // test that doctype is at the front - expect(html).to.match(/^/); + expect(html).to.match(/^/i); }); it('Doctype is added in a layout used with markdown pages', async () => { const html = await fixture.readFile('/in-layout-article/index.html'); // test 1: doctype is at the front - expect(html).to.match(/^/); + expect(html).to.match(/^/i); // test 2: A link inside of the head const $ = cheerio.load(html); expect($('head link')).to.have.lengthOf(1); }); }); -*/ - -it.skip('is skipped', () => {}); diff --git a/packages/astro/test/fixtures/astro-doctype/src/layouts/WithDoctype.astro b/packages/astro/test/fixtures/astro-doctype/src/layouts/WithDoctype.astro index b3ad03aa0..cbfe5c478 100644 --- a/packages/astro/test/fixtures/astro-doctype/src/layouts/WithDoctype.astro +++ b/packages/astro/test/fixtures/astro-doctype/src/layouts/WithDoctype.astro @@ -1,11 +1,11 @@ --- -import '../styles/global.css'; import Meta from '../components/Meta.astro'; ---