Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offline plugin fetch error #8158

Merged
merged 3 commits into from
Sep 14, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/gatsby-plugin-offline/src/gatsby-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to use const a = new URL(resource) here - same effect but we don't need to create a hidden element

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also might be better to rename this if it lo longer uses an <a> tag

const isExternal = (a.host && a.host !== window.location.host)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be .origin rather than .host since a URL is considered cross-origin if the port or protocol are different. Also, if we use new URL() as above then it guarantees the variable a has a .origin property, so the check can be removed.

fetch(resource, isExternal ? { mode: `no-cors` } : undefined)
}

// Loop over all resources and fetch the page component and JSON
Expand Down