Django and service workers - serve "sw.js" at the root URL of the application

Therefore, I am creating a progressive Django web application with offline support using service workers.

According to Google documentation , the sw.js file should be in the root of the application URL:

You must do this because the scope of the employee’s service (the set of URLs for which ServiceWorker will be loaded) is determined by the directory where it is located.

At the moment I am serving all static resources from the http://example.com/static/ folder . But I need to provide this specific file at the url, for example: http://example.com/sw.js .

Any ideas how to achieve this? I could make a specific nginx rule to do this redirection, but I don't know if this is the cleanest way to do this. Maybe this parameter should be in urls.py?

Note : I saw this question , which suggests using the static () method from django.conf.urls.static. But django docs say the static method is for development only, so it doesn't suit me.

Note (2) : I suppose I could change the setting STATIC_URL, but I am pleased that my files are served from the / static directory. I just want this file to be at the root of the URL.

+9
4

javascript , html. urls.py

url(r'^service-worker.js', cache_control(max_age=2592000)(TemplateView.as_view(
    template_name="service-worker.js",
    content_type='application/javascript',
)), name='service-worker.js'),

service-worker.js .

, , static javascript.

+15

Django 1.11 urls.py :

from django.views.generic import TemplateView

urlpatterns = [
  url(r'^sw.js', (TemplateView.as_view(template_name="sw.js", content_type='application/javascript', )), name='sw.js'),
]
+2

2.2

myproj/
|-app/
| |-templates/
|   |-app/
|     -sw.js
|-myproj/
  -urls.py

urls.py ()

from django.views.generic import TemplateView

urlpatterns = [
  ...
  path('sw.js', (TemplateView.as_view(template_name="app/sw.js", 
  content_type='application/javascript', )), name='sw.js'),
]
0

DOMException: The script resource is behind a redirect, which is disallowed.

.

urls.py:

from django.views.generic import TemplateView

urlpatterns = [
  ...,
  url(r'^service-worker.js', (TemplateView.as_view(template_name="service-worker.js", content_type='application/javascript', )), name='service-worker.js'),
]

.

<script>
 if ('serviceWorker' in navigator) {
    console.log("Will the service worker register?");
    navigator.serviceWorker.register('service-worker.js')
      .then(function(reg){
        console.log("Yes, it did.");
     }).catch(function(err) {
       console.log("No it didn't. This happened:", err)
        console.log("err.message:", err.message)
    });
 }
</script>

:

<script>
 if ('serviceWorker' in navigator) {
    console.log("Will the service worker register?");
    navigator.serviceWorker.register("{% url 'service-worker.js' %}") //note that I am using the url template here
      .then(function(reg){
        console.log("Yes, it did.");
     }).catch(function(err) {
       console.log("No it didn't. This happened:", err)
        console.log("err.message:", err.message)
    });
 }
</script>
0

Source: https://habr.com/ru/post/1649769/


All Articles