Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add service worker
dominik-zeglen7072a6ad07df
diff --git a/saleor/core/urls.py b/saleor/core/urls.pyindex 53c55edec8..67b35e839c 100644--- a/saleor/core/urls.py+++ b/saleor/core/urls.py@@ -15,4 +15,8 @@ urlpatterns = [url(r'^offline$', views.offline, name='offline'),url(r'^manifest\.json$', TemplateView.as_view(template_name='manifest.json', content_type='application/json')),+ url(r'^serviceworker\.js', TemplateView.as_view(+ template_name="serviceworker.js",+ content_type='application/javascript',+ ), name='serviceworker.js'),]diff --git a/saleor/static/js/components/misc.js b/saleor/static/js/components/misc.jsindex 1a9495e245..7e8530d7cf 100644--- a/saleor/static/js/components/misc.js+++ b/saleor/static/js/components/misc.js@@ -26,4 +26,15 @@ export default $(document).ready((e) => {let hash = window.location.hash;$('.nav-tabs a[href="' + hash + '"]').tab('show');++ if ('serviceWorker' in navigator) {+ navigator.serviceWorker+ .register('/serviceworker.js')+ .then((registration) => {+ console.log('Registration successful, scope is:', registration.scope);+ })+ .catch((error) => {+ console.log('Service worker registration failed, error:', error);+ });+ }});diff --git a/templates/base.html b/templates/base.htmlindex 5e7010dc02..98afe9d259 100644--- a/templates/base.html+++ b/templates/base.html@@ -27,7 +27,8 @@<meta property="og:image" content="{% static "images/block1.png" %}">{% endblock meta_tags %}- <link rel="manifest" href="./manifest.json">+ <link rel="manifest" href="/manifest.json"/>+ <link rel="serviceworker" href="/serviceworker.js"/><!-- Le HTML5 shim, for IE6-8 support of HTML5 elements --><!--[if lt IE 9]>diff --git a/templates/manifest.json b/templates/manifest.jsonnew file mode 100644index 0000000000..f082385db1--- /dev/null+++ b/templates/manifest.json@@ -0,0 +1,63 @@+{+ "lang": "en",+ "dir": "ltr",+ "name": "Saleor demo shop",+ "description": "Saleor is best storefront engine evar",+ "short_name": "Saleor Demo",+ "icons": [+ {+ "src": "/static/assets/favicons/android-chrome-36x36.png",+ "sizes": "36x36",+ "type": "image/png"+ },+ {+ "src": "/static/assets/favicons/android-chrome-48x48.png",+ "sizes": "48x48",+ "type": "image/png"+ },+ {+ "src": "/static/assets/favicons/android-chrome-72x72.png",+ "sizes": "72x72",+ "type": "image/png"+ },+ {+ "src": "/static/assets/favicons/android-chrome-96x96.png",+ "sizes": "96x96",+ "type": "image/png"+ },+ {+ "src": "/static/assets/favicons/android-chrome-144x144.png",+ "sizes": "144x144",+ "type": "image/png"+ },+ {+ "src": "/static/assets/favicons/android-chrome-192x192.png",+ "sizes": "192x192",+ "type": "image/png"+ },+ {+ "src": "/static/assets/favicons/android-chrome-256x256.png",+ "sizes": "256x256",+ "type": "image/png"+ },+ {+ "src": "/static/assets/favicons/android-chrome-384x384.png",+ "sizes": "384x384",+ "type": "image/png"+ },+ {+ "src": "/static/assets/favicons/android-chrome-512x512.png",+ "sizes": "512x512",+ "type": "image/png"+ }+ ],+ "scope": "/",+ "start_url": "/",+ "display": "fullscreen",+ "orientation": "portrait",+ "theme_color": "#333333",+ "background_color": "#ffffff",+ "serviceworker": {+ "src": "/service_worker.js"+ }+}diff --git a/templates/serviceworker.js b/templates/serviceworker.jsnew file mode 100644index 0000000000..19ba67fec2--- /dev/null+++ b/templates/serviceworker.js@@ -0,0 +1,42 @@+const cacheName = 'staticFiles';++// during the install phase you usually want to cache static assets+self.addEventListener('install', (e) => {+ // once the SW is installed, go ahead and fetch the resources to make this work offline+ e.waitUntil(+ caches.open(cacheName)+ .then((cache) => {+ return cache.addAll([+ '/',+ '/offline'+ ])+ .then(() => {+ self.skipWaiting();+ })+ .catch(e => console.log(e));+ })+ .catch(e => console.log(e))+ );+});++// when the browser fetches a url+self.addEventListener('fetch', (event) => {+ if (event.request.url.match(/\.(css|js|svg)$/)) {+ console.log(`caching ${event.request.url}`);+ caches.open(cacheName)+ .then(cache => cache.add(event.request.url))+ .then(() => self.skipWaiting())+ .catch(e => console.log(e));+ }+ event.respondWith(+ fetch(event.request)+ .catch(() => {+ if (!event.request.url.match(/\.(js|css|eot|otf|png|svg|jpg|ttf|woff|woff2)(\?v=[0-9.]+)?$/)) {+ console.log(event.request.url);+ return caches.match('/offline')+ .then(res => res);+ }+ return caches.match(event.request);+ })+ );+});