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 support with service worker #978

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions _includes/header.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<input type="checkbox" id="babel-toggle-search" class="babel-toggle-search-checkbox" />
<header class="navbar navbar-default navbar-fixed-top babel-nav" id="top" role="banner">

<div id="offline-indicator" class="hidden">
You are offline
</div>

<div class="container">
<a href="/" class="navbar-brand logo">Babel</a>
<div class="babel-navbar-toggles">
Expand Down
1 change: 1 addition & 0 deletions _sass/_main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ body {
@import "components/_tick-list.scss";
@import "components/_type.scss";
@import "components/_user.scss";
@import "components/_offline-indicator.scss";

@import "pages/_404.scss";
@import "pages/_blog.scss";
Expand Down
4 changes: 4 additions & 0 deletions _sass/components/_offline-indicator.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#offline-indicator {
text-align: center;
padding: 5px;
}
15 changes: 14 additions & 1 deletion scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,20 @@ $(document).ready(function() {
if ('serviceWorker' in navigator) {

navigator.serviceWorker.register('/service-worker.js').then(function() {
console.log('CLIENT: service worker registration complete.');
var offlineIndicator = $("#offline-indicator");

window.addEventListener('online', function() {
offlineIndicator.addClass("hidden");
});

window.addEventListener('offline', function() {
offlineIndicator.removeClass("hidden");
});

Choose a reason for hiding this comment

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

Mostly leaving here as a note in case helpful later :)

The navigator.{online/offline} events may not accurately indicate that you can or can't access the network. There are well documented gotchas where you might need additional means to check that you're really online. One is checking connection loss by making failed XHR requests (retry a request a few times, if it doesn't go through, you're definitely offline).

Choose a reason for hiding this comment

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

@addyosmani flipkart is awesome !

if (!navigator.onLine) {
offlineIndicator.removeClass("hidden");
}

}, function() {
console.log('CLIENT: service worker registration failure.');
});
Expand Down