From 154eb2b6c3ac8dab8f55e7d05566ad7aff1e5d9a Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Sun, 9 Feb 2020 14:26:49 -0800 Subject: [PATCH] fix: ignore prefetch and connect for link (#145) --- src/links.ts | 9 ++++++++- test/fixtures/prefetch/index.html | 8 ++++++++ test/test.ts | 10 ++++++++++ test/{cli.ts => zcli.ts} | 0 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/prefetch/index.html rename test/{cli.ts => zcli.ts} (100%) diff --git a/src/links.ts b/src/links.ts index ecd4a6c3..cb832fe2 100644 --- a/src/links.ts +++ b/src/links.ts @@ -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); }); }); @@ -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; diff --git a/test/fixtures/prefetch/index.html b/test/fixtures/prefetch/index.html new file mode 100644 index 00000000..dfcc9d1a --- /dev/null +++ b/test/fixtures/prefetch/index.html @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/test/test.ts b/test/test.ts index ad8a537b..57154918 100644 --- a/test/test.ts +++ b/test/test.ts @@ -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); + }); }); diff --git a/test/cli.ts b/test/zcli.ts similarity index 100% rename from test/cli.ts rename to test/zcli.ts