Skip to content
This repository was archived by the owner on Dec 20, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,18 @@ PWA_APP_ICONS = [

All settings are optional, and the app will work fine with its internal defaults. Highly recommend setting at least `PWA_APP_NAME` and `PWA_APP_DESCRIPTION`.

Add the progressive web app URLs to urls.py:
Add the progressive web app URLs to urls.py (django >= 2):
```python
from django.urls import path, include

urlpatterns = [
...
path('', include('pwa.urls')), # You MUST use an empty string as the URL prefix
...
]
```

the old way (django < 2.0):
```python
from django.conf.urls import url, include

Expand All @@ -58,6 +69,7 @@ urlpatterns = [
]
```


Inject the required meta tags in your base.html (or wherever your HTML &lt;head&gt; is defined):
```html
{% load pwa %}
Expand All @@ -80,10 +92,10 @@ While running the Django test server:

Adding Your Own Service Worker
=====
By default, the service worker implemented by this app is empty. To add service worker functionality, you'll want to create a `serviceworker.js` or similarly named file, and then point at it using the PWA_SERVICE_WORKER_PATH variable.
By default, the service worker implemented by this app is empty. To add service worker functionality, you'll want to create a `serviceworker.js` or similarly named template in a template directory, and then point at it using the PWA_SERVICE_WORKER_PATH variable.

```python
PWA_SERVICE_WORKER_PATH = os.path.join(BASE_DIR, 'my_app', 'serviceworker.js')
PWA_SERVICE_WORKER_PATH = 'my_app/serviceworker.js'

```

Expand Down
1 change: 1 addition & 0 deletions pwa/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default_app_config = 'pwa.apps.PwaConfig'
5 changes: 1 addition & 4 deletions pwa/app_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import os

# Path to the service worker implementation. Default implementation is empty.
PWA_SERVICE_WORKER_PATH = getattr(settings, 'PWA_SERVICE_WORKER_PATH',
os.path.join(os.path.abspath(os.path.dirname(__file__)), 'templates', 'serviceworker.js'))
PWA_SERVICE_WORKER_PATH = getattr(settings, 'PWA_SERVICE_WORKER_PATH', 'serviceworker.js')

# App parameters to include in manifest.json and appropriate meta tags
PWA_APP_NAME = getattr(settings, 'PWA_APP_NAME', 'MyApp')
Expand All @@ -19,5 +18,3 @@
'sizes': '160x160'
}
])


23 changes: 16 additions & 7 deletions pwa/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
from django.conf.urls import url
from .views import manifest, service_worker
import django
from .views import Manifest, ServiceWorker

# Serve up serviceworker.js and manifest.json at the root
urlpatterns = [
url('^serviceworker.js$', service_worker),
url('^manifest.json$', manifest)
]
if django.VERSION[0] >= 2:
from django.urls import path
# Serve up serviceworker.js and manifest.json at the root
urlpatterns = [
path('serviceworker.js', ServiceWorker.as_view()),
path('manifest.json', Manifest.as_view())
]
else:
from django.conf.urls import url
# Serve up serviceworker.js and manifest.json at the root
urlpatterns = [
url('serviceworker.js$', ServiceWorker.as_view()),
url('manifest.json$', Manifest.as_view())
]
23 changes: 12 additions & 11 deletions pwa/views.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from django.http import HttpResponse
from django.shortcuts import render
from django.views.generic import TemplateView

from . import app_settings

class ServiceWorker(TemplateView):
content_type = 'application/javascript'
template_name = app_settings.PWA_SERVICE_WORKER_PATH

def service_worker(request):
response = HttpResponse(open(app_settings.PWA_SERVICE_WORKER_PATH).read(), content_type='application/javascript')
return response
class Manifest(TemplateView):
content_type = 'application/json'
template_name = 'manifest.json'


def manifest(request):
return render(request, 'manifest.json', {
setting_name: getattr(app_settings, setting_name)
for setting_name in dir(app_settings)
if setting_name.startswith('PWA_')
})
def get_context_data(self, **kwargs):
for setting_name in dir(app_settings):
if setting_name.startswith('PWA_'):
kwargs[setting_name] = getattr(app_settings, setting_name)
return super().get_context_data(**kwargs)
9 changes: 7 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))

install_requirements = [
"django>=1.11",
]
setup(
name='django-progressive-web-app',
version='0.1',
packages=find_packages(),
install_requires=install_requirements,
include_package_data=True,
license='MIT License',
description=short_description,
Expand All @@ -30,7 +34,8 @@
classifiers=[
'Environment :: Web Environment',
'Framework :: Django',
'Framework :: Django :: 1.10',
'Framework :: Django :: 1.11',
'Framework :: Django :: 2.0',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
Expand All @@ -42,4 +47,4 @@
'Topic :: Internet :: WWW/HTTP',
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
],
)
)