Try fixing query string stylesheets

This commit is contained in:
Daniil Gentili 2020-08-22 22:09:30 +02:00
parent b12a0dad33
commit 972f0d2ace
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 23 additions and 8 deletions

View File

@ -148,8 +148,10 @@ function inline(html, styles, options) {
links.map((link) => { links.map((link) => {
if (o.extract) { if (o.extract) {
const href = link.getAttribute('href'); const href = link.getAttribute('href');
const file = path.resolve(path.join(o.basePath || process.cwd, href)); const fileOrig = path.resolve(path.join(o.basePath || process.cwd, href));
const files = [fileOrig, fileOrig.replace(/\?.*/, '')];
for (const file of files) {
if (fs.existsSync(file)) { if (fs.existsSync(file)) {
const orig = fs.readFileSync(file); const orig = fs.readFileSync(file);
const diff = extractCss(orig, inlined, o.minify); const diff = extractCss(orig, inlined, o.minify);
@ -157,7 +159,12 @@ function inline(html, styles, options) {
fs.writeFileSync(filename, diff); fs.writeFileSync(filename, diff);
link.setAttribute('href', normalizePath(reaver.rev(href, diff))); link.setAttribute('href', normalizePath(reaver.rev(href, diff)));
} else if (!/\/\//.test(href)) { // eslint-disable-next-line array-callback-return
return;
}
}
if (!/\/\//.test(href)) {
throw new Error(`Error: file "${href}" not found in "${o.basePath || process.cwd}". Specify base path.`); throw new Error(`Error: file "${href}" not found in "${o.basePath || process.cwd}". Specify base path.`);
} }
} }

View File

@ -16,6 +16,14 @@ test('Inline css', async () => {
expect(strip(out.toString())).toBe(strip(expected)); expect(strip(out.toString())).toBe(strip(expected));
}); });
test('Inline css with query string link', async () => {
const html = await read('fixtures/index-query.html');
const css = await read('fixtures/critical.css');
const expected = await read('expected/index-inlined-async-query.html');
const out = inline(html, css, {minify: false, polyfill: true});
expect(strip(out.toString())).toBe(strip(expected));
});
test('Inline css with media=print', async () => { test('Inline css with media=print', async () => {
const html = await read('fixtures/index.html'); const html = await read('fixtures/index.html');
const css = await read('fixtures/critical.css'); const css = await read('fixtures/critical.css');