From 07bf6b9fb9be119cf8a5fcd33f1ff8e0caf61f66 Mon Sep 17 00:00:00 2001 From: Mike Allanson Date: Fri, 14 Sep 2018 17:09:04 +0100 Subject: [PATCH 1/3] Use 'no-cors' mode when fetching external resources --- packages/gatsby-plugin-offline/src/gatsby-browser.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-plugin-offline/src/gatsby-browser.js b/packages/gatsby-plugin-offline/src/gatsby-browser.js index 07c7b136e9e92..fff6a955e838c 100644 --- a/packages/gatsby-plugin-offline/src/gatsby-browser.js +++ b/packages/gatsby-plugin-offline/src/gatsby-browser.js @@ -28,8 +28,12 @@ exports.onServiceWorkerInstalled = ({ getResourceURLsForPathname }) => { .call(nodes) .map(node => node.src || node.href || node.getAttribute(`data-href`)) + const a = document.createElement(`a`) + for (const resource of resources) { - fetch(resource) + a.href = resource + const isExternal = (a.host && a.host !== window.location.host) + fetch(resource, isExternal ? { mode: `no-cors` } : undefined) } // Loop over all resources and fetch the page component and JSON From 60b6c3fac1bc2a88fd16cfa3978fc6aa8a3134b8 Mon Sep 17 00:00:00 2001 From: Mike Allanson Date: Fri, 14 Sep 2018 20:42:32 +0100 Subject: [PATCH 2/3] Review feedback --- packages/gatsby-plugin-offline/src/gatsby-browser.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/gatsby-plugin-offline/src/gatsby-browser.js b/packages/gatsby-plugin-offline/src/gatsby-browser.js index fff6a955e838c..8026afbefc015 100644 --- a/packages/gatsby-plugin-offline/src/gatsby-browser.js +++ b/packages/gatsby-plugin-offline/src/gatsby-browser.js @@ -28,12 +28,14 @@ exports.onServiceWorkerInstalled = ({ getResourceURLsForPathname }) => { .call(nodes) .map(node => node.src || node.href || node.getAttribute(`data-href`)) - const a = document.createElement(`a`) - for (const resource of resources) { - a.href = resource - const isExternal = (a.host && a.host !== window.location.host) - fetch(resource, isExternal ? { mode: `no-cors` } : undefined) + const url = new URL(resource, window.location.origin) + const isExternal = url.origin !== window.location.origin + console.log(isExternal, resource) + fetch( + resource, + isExternal ? { mode: `no-cors` } : undefined + ) } // Loop over all resources and fetch the page component and JSON From 7bddbac7e7f34019f4580f3ff9f1a985c2b70065 Mon Sep 17 00:00:00 2001 From: Mike Allanson Date: Fri, 14 Sep 2018 20:43:16 +0100 Subject: [PATCH 3/3] No log --- packages/gatsby-plugin-offline/src/gatsby-browser.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/gatsby-plugin-offline/src/gatsby-browser.js b/packages/gatsby-plugin-offline/src/gatsby-browser.js index 8026afbefc015..cc863bb9ec962 100644 --- a/packages/gatsby-plugin-offline/src/gatsby-browser.js +++ b/packages/gatsby-plugin-offline/src/gatsby-browser.js @@ -31,7 +31,6 @@ exports.onServiceWorkerInstalled = ({ getResourceURLsForPathname }) => { for (const resource of resources) { const url = new URL(resource, window.location.origin) const isExternal = url.origin !== window.location.origin - console.log(isExternal, resource) fetch( resource, isExternal ? { mode: `no-cors` } : undefined