Skip to content

Commit

Permalink
fix(register-service-worker): show an error if installing SW not over…
Browse files Browse the repository at this point in the history
… HTTPS (#8958)

* Error if attempting to use SWs over HTTP

* Use console.error

* Only register the SW if there's no error
  • Loading branch information
vtenfys authored and pieh committed Oct 9, 2018
1 parent 9d4cd78 commit 2ba57ea
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/gatsby/cache-dir/load-directly-or-404.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export function getRedirectUrl(path) {
// This should never happen, but check just in case - otherwise, there would
// be an infinite redirect loop
if (url.search.match(/\?(.*&)?no-cache=1(&|$)/)) {
console.log(
console.error(
`Found no-cache=1 while attempting to load a page directly; ` +
`this is likely due to a bug in Gatsby, or a misconfiguration in your project.`
)
Expand Down
9 changes: 8 additions & 1 deletion packages/gatsby/cache-dir/register-service-worker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { apiRunner } from "./api-runner-browser"

if (`serviceWorker` in navigator) {
if (
window.location.protocol !== `https:` &&
window.location.hostname !== `localhost`
) {
console.error(
`Service workers can only be used over HTTPS, or on localhost for development`
)
} else if (`serviceWorker` in navigator) {
navigator.serviceWorker
.register(`${__PATH_PREFIX__}/sw.js`)
.then(function(reg) {
Expand Down

0 comments on commit 2ba57ea

Please sign in to comment.