diff options
author | 2021-11-11 12:28:14 -0700 | |
---|---|---|
committer | 2021-11-11 12:28:14 -0700 | |
commit | 529486bfb0d6f0ca54b091b000ed446dd1a085af (patch) | |
tree | 4d08fe57545b8cecb361ddd7f135b965e027b6c1 | |
parent | 5e0cb796a6e075645d9ad9e1195e7f694df010d0 (diff) | |
download | astro-529486bfb0d6f0ca54b091b000ed446dd1a085af.tar.gz astro-529486bfb0d6f0ca54b091b000ed446dd1a085af.tar.zst astro-529486bfb0d6f0ca54b091b000ed446dd1a085af.zip |
Inject Doctype tag (#1783)
Diffstat (limited to '')
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 <!doctype html> if missing (TODO: is a more robust check needed for comments, etc.?) + if (!/<!doctype html/i.test(html)) { + html = '<!DOCTYPE html>\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 <!doctype> 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(/^<!doctype html>/); + expect(html).to.match(/^<!DOCTYPE html>/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(/^<!doctype html>/); + expect(html).to.match(/^<!DOCTYPE html>/i); }); - it('Preserves user provided doctype', async () => { - const html = await fixture.readFile('/preserve/index.html'); + // Note: parse5 converts this to <!DOCTYPE html> (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('^<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">')); - }); + // // test that Doctype included was preserved + // expect(html).to.match(new RegExp('^<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">', '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(/^<!DOCTYPE html>/); + expect(html).to.match(/^<!DOCTYPE html>/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(/^<!doctype html>/); + expect(html).to.match(/^<!DOCTYPE html>/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(/^<!doctype html>/); + expect(html).to.match(/^<!DOCTYPE html>/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(/^<!doctype html>/); + expect(html).to.match(/^<!DOCTYPE html>/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'; --- <!doctype html> <html lang="en"> <head> <title>My App</title> + <link rel="styleshseet" href={Astro.resolve('../styles/global.css')}> <Meta /> </head> <body> diff --git a/packages/astro/test/fixtures/astro-doctype/src/layouts/WithoutDoctype.astro b/packages/astro/test/fixtures/astro-doctype/src/layouts/WithoutDoctype.astro index 9d25a250a..692c75571 100644 --- a/packages/astro/test/fixtures/astro-doctype/src/layouts/WithoutDoctype.astro +++ b/packages/astro/test/fixtures/astro-doctype/src/layouts/WithoutDoctype.astro @@ -1,9 +1,7 @@ ---- -import '../styles/global.css' ---- <html lang="en"> <head> <title>My App</title> + <link rel="styleshseet" href={Astro.resolve('../styles/global.css')}> </head> <body> diff --git a/packages/astro/test/fixtures/astro-doctype/src/pages/preserve.astro b/packages/astro/test/fixtures/astro-doctype/src/pages/preserve.astro index 38c15afc0..4725df370 100644 --- a/packages/astro/test/fixtures/astro-doctype/src/pages/preserve.astro +++ b/packages/astro/test/fixtures/astro-doctype/src/pages/preserve.astro @@ -2,7 +2,7 @@ let title = 'My Site'; --- -<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> +<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="en"> <head><title>{title}</title></head> <body><h1>Hello world</h1></body> |