diff --git a/packages/dom/src/utils.js b/packages/dom/src/utils.js index 2a5b32d40..7dd167f8f 100644 --- a/packages/dom/src/utils.js +++ b/packages/dom/src/utils.js @@ -45,6 +45,12 @@ export function styleSheetFromNode(node) { } export function rewriteLocalhostURL(url) { + let parsedURL = new URL(url); + + // check if URL has chrome-error scheme and rewrite to a non-existent URL that will 404 + if (parsedURL.protocol === 'chrome-error:') { + url = 'http://we-got-a-chrome-error-url-handled-gracefully.com/' + parsedURL.host + parsedURL.pathname + parsedURL.search + parsedURL.hash; + } return url.replace(/(http[s]{0,1}:\/\/)(localhost|127.0.0.1)[:\d+]*/, '$1render.percy.local'); } diff --git a/packages/dom/test/utils.test.js b/packages/dom/test/utils.test.js index 128e402dd..584290345 100644 --- a/packages/dom/test/utils.test.js +++ b/packages/dom/test/utils.test.js @@ -145,5 +145,13 @@ describe('utils', () => { const case4 = rewriteLocalhostURL('https://hellolocalhost:2000/world'); expect(case4).toEqual('https://hellolocalhost:2000/world'); }); + it('should rewrite chrome-error scheme to non-existent URL', () => { + const case1 = rewriteLocalhostURL('chrome-error://chromewebdata/'); + expect(case1).toEqual('http://we-got-a-chrome-error-url-handled-gracefully.com/chromewebdata/'); + const case2 = rewriteLocalhostURL('chrome-error://chromewebdata/__serialized__/abc.png'); + expect(case2).toEqual('http://we-got-a-chrome-error-url-handled-gracefully.com/chromewebdata/__serialized__/abc.png'); + const case3 = rewriteLocalhostURL('chrome-error://chromewebdata/path?query=1#hash'); + expect(case3).toEqual('http://we-got-a-chrome-error-url-handled-gracefully.com/chromewebdata/path?query=1#hash'); + }); }); });