Commit 7c1211a9 authored by Julien Enselme's avatar Julien Enselme

Add ServiceWorker

parent fdf7519b
......@@ -10,5 +10,17 @@
<h1>A page</h1>
<p>This is a very small and uninteresting page of a test site for Django and PWA</p>
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('{% url "sw.js" %}', { scope: '/' }).then(function(reg) {
// registration worked
console.log('Registration succeeded. Scope is ' + reg.scope);
}).catch(function(error) {
// registration failed
console.log('Registration failed with ' + error);
});
}
</script>
</body>
</html>
const VERSION = '1.0.0';
self.addEventListener('install', (event) => {
console.log('[SW] Installing SW version:', VERSION);
});
......@@ -15,6 +15,7 @@ Including another URLconf
"""
from django.contrib import admin
from django.urls import path
from django.views.generic import TemplateView
from .apps.pwa import views as pwa_views
......@@ -24,4 +25,13 @@ urlpatterns = [
path('', pwa_views.my_page),
path('offline/', pwa_views.offline),
# The service worker cannot be in /static because its scope will be limited to /static.
# Since we want it to have a scope of the full application, we rely on this TemplateView
# trick to make it work.
path(
'sw.js',
TemplateView.as_view(template_name='sw.js', content_type='application/javascript'),
name='sw.js',
),
]
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment