Django and service workers - serve “sw.js” at application's root url

前端 未结 4 2164
闹比i
闹比i 2020-12-06 01:56

So I\'m building a Django progressive web app with offline support using service workers.

According to google\'s documentation, the sw.js file should be at the root

4条回答
  •  鱼传尺愫
    2020-12-06 02:31

    You can serve javascript as a view, not just html. Put this in your projects 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'),
    

    Then put your service-worker.js in your templates directory.

    Bonus is that you can now also use template tags like static in your javascript file.

提交回复
热议问题