Skip to content

Commit

Permalink
fix: ignore prefetch and connect for link (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Feb 9, 2020
1 parent d2f6bce commit 154eb2b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ export function getLinks(source: string, baseUrl: string): ParsedUrl[] {
const elements = linksAttr[attr].map(tag => `${tag}[${attr}]`).join(',');
$(elements).each((i, element) => {
const values = parseAttr(attr, element.attribs[attr]);
// ignore href properties for link tags where rel is likely to fail
const relValuesToIgnore = ['dns-prefetch', 'preconnect'];
if (
element.tagName === 'link' &&
relValuesToIgnore.includes(element.attribs['rel'])
) {
return;
}
links.push(...values);
});
});
Expand All @@ -62,7 +70,6 @@ function getBaseUrl(htmlBaseUrl: string, oldBaseUrl: string): string {
if (isAbsoluteUrl(htmlBaseUrl)) {
return htmlBaseUrl;
}

const url = new URL(htmlBaseUrl, oldBaseUrl);
url.hash = '';
return url.href;
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/prefetch/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<link rel="dns-prefetch" href="https://fonts.gstatic.com">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link rel="stylesheet" href="http://fake.local/site.css">
</head>
<body></body>
</html>
10 changes: 10 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,14 @@ describe('linkinator', () => {
assert.ok(results.passed);
scopes.forEach(x => x.done());
});

it('should not attempt to validate preconnect or prefetch urls', async () => {
const scope = nock('http://fake.local')
.head('/site.css')
.reply(200, '');
const results = await check({ path: 'test/fixtures/prefetch' });
scope.done();
assert.ok(results.passed);
assert.strictEqual(results.links.length, 2);
});
});
File renamed without changes.

0 comments on commit 154eb2b

Please sign in to comment.