Inject Doctype tag (#1783)

This commit is contained in:
Drew Powers 2021-11-11 12:28:14 -07:00 committed by GitHub
parent 5e0cb796a6
commit 529486bfb0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 22 additions and 23 deletions

View file

@ -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 });

View file

@ -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[] = [];

View file

@ -1,5 +1,3 @@
/**
* UNCOMMENT: compiler doesnt 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', () => {});

View file

@ -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>

View file

@ -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>

View file

@ -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>