Skip to content

Commit

Permalink
Step 2 | Add JS responsible about SW
Browse files Browse the repository at this point in the history
  • Loading branch information
amahdy committed Jan 16, 2017
1 parent 56fb4fc commit 2b70f0b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/vaadin/java_pwa/MyUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.JavaScript;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
Expand All @@ -20,6 +21,7 @@
* overridden to add component to the user interface and initialize non-component functionality.
*/
@Theme("mytheme")
@JavaScript("vaadin:https://js/app.js")
public class MyUI extends UI {

@Override
Expand Down
26 changes: 26 additions & 0 deletions src/main/webapp/VAADIN/js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('../sw.js', { scope: '/' }).then((reg) => {
if (reg.installing) {
console.log('Service worker installing');
} else if(reg.waiting) {
console.log('Service worker installed');
} else if(reg.active) {
console.log('Service worker active');
}

}).catch((error) => {
console.log('Registration failed with ' + error); // Registration failed
});

// Communicate with the service worker using MessageChannel API.
function sendMessage(message) {
return new Promise((resolve, reject) => {
const messageChannel = new MessageChannel();
messageChannel.port1.onmessage = function(event) {
resolve(`Direct message from SW: ${event.data}`);
};

navigator.serviceWorker.controller.postMessage(message, [messageChannel.port2])
});
}
}

0 comments on commit 2b70f0b

Please sign in to comment.