Skip to content

Commit

Permalink
Show how out of date stale pages are
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechilds committed Sep 22, 2016
1 parent a6e5111 commit 416ccb0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
14 changes: 13 additions & 1 deletion public/assets/enhancements.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,20 @@
};

// Register service worker
if(supports.test(['serviceWorker'])) {
if(supports.test(['serviceWorker', 'querySelector', 'classList'])) {
navigator.serviceWorker.register('/sw.js');
DOMReady(function() {
if(window.cacheDate) {
var offlineMessage = create('div');
offlineMessage.classList.add('cache-message');
offlineMessage.innerText = '*There seems to be an issue connecting to the server. This is a cached version of this page from ' + window.cacheDate;
var main = find('main');
if(main) {
doc.body.classList.add('no-connection');
main.insertBefore(offlineMessage, main.firstChild);
}
}
});
}

// Init favourite nodes
Expand Down
7 changes: 7 additions & 0 deletions public/assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,13 @@ footer {
font-size: 18px;
}

/* Cache message */
.cache-message {
font-size: 12px;
color: rgba(255, 255, 255, 0.9);
margin-bottom: 1rem;
}

/* Large desktop */
@media (min-width: 1230px) {
pre {
Expand Down
9 changes: 8 additions & 1 deletion public/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ self.addEventListener('fetch', function(event) {
.then(function(response) {
if(requestUrl.pathname === '/') {
caches.open(cacheName).then(function(cache) {
cache.put(event.request, response.clone());

// Modify the response html so we know when it was cached
response.clone().text().then(function(html) {
html = html.replace('window.cacheDate=false;', 'window.cacheDate="'+Date()+'";');
var modifiedResponse = new Response(new Blob([html]), { headers: response.headers });
cache.put(event.request, modifiedResponse);
});

});
}

Expand Down
1 change: 1 addition & 0 deletions views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
</div>
</footer>

<script>window.cacheDate=false;</script>
<script src="/assets/enhancements.js" async></script>
</body>
</html>

1 comment on commit 416ccb0

@mar619
Copy link

@mar619 mar619 commented on 416ccb0 Dec 25, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.