Included the default serviceworker, updated the manifest, Add the default offline page, updated the unit test, Redme
No files matched your search
@@ -10,4 +10,17 @@
|
||||
- Add Oritentation on manifest.json
|
||||
- Add tox
|
||||
- Add Coverage
|
||||
|
||||
|
||||
## 1.0.1
|
||||
|
||||
- Add django 2 requirement
|
||||
- Use templateviews instead of own implementations
|
||||
- Add content_types
|
||||
- Update `README.md`
|
||||
- Add `PWA_APP_FETCH_URL`
|
||||
- Add default_config in `__init__.py`
|
||||
- Add basic serviceworker
|
||||
- Add default offline page and default icons
|
||||
- Updated the unit tests
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
include *.md
|
||||
recursive-include pwa/templates *
|
||||
recursive-include pwa/templates *
|
||||
recursive-include pwa/static *
|
||||
@@ -60,11 +60,11 @@ All settings are optional, and the app will work fine with its internal defaults
|
||||
|
||||
Add the progressive web app URLs to urls.py:
|
||||
```python
|
||||
from django.conf.urls import url, include
|
||||
from django.urls import path, include
|
||||
|
||||
urlpatterns = [
|
||||
...
|
||||
url('', include('pwa.urls')), # You MUST use an empty string as the URL prefix
|
||||
path('', include('pwa.urls')), # You MUST use an empty string as the URL prefix
|
||||
...
|
||||
]
|
||||
```
|
||||
@@ -86,18 +86,85 @@ While running the Django test server:
|
||||
|
||||
1. Verify that `/manifest.json` is being served
|
||||
1. Verify that `/serviceworker.js` is being served
|
||||
1. Verify that `/offline` is being served
|
||||
1. Use the Application tab in the Chrome Developer Tools to verify the progressive web app is configured correctly.
|
||||
1. Use the "Add to homescreen" link on the Application Tab to verify you can add the app successfully.
|
||||
|
||||
|
||||
The Service Worker
|
||||
=====
|
||||
By default, the service worker implemented by this app is:
|
||||
```js
|
||||
// Base Service Worker implementation. To use your own Service Worker, set the PWA_SERVICE_WORKER_PATH variable in settings.py
|
||||
|
||||
var staticCacheName = "django-pwa-v" + new Date().getTime();
|
||||
var filesToCache = [
|
||||
'/offline',
|
||||
'/css/django-pwa-app.css',
|
||||
'/images/icons/icon-72x72.png',
|
||||
'/images/icons/icon-96x96.png',
|
||||
'/images/icons/icon-128x128.png',
|
||||
'/images/icons/icon-144x144.png',
|
||||
'/images/icons/icon-152x152.png',
|
||||
'/images/icons/icon-192x192.png',
|
||||
'/images/icons/icon-384x384.png',
|
||||
'/images/icons/icon-512x512.png',
|
||||
];
|
||||
|
||||
// Cache on install
|
||||
self.addEventListener("install", event => {
|
||||
this.skipWaiting();
|
||||
event.waitUntil(
|
||||
caches.open(staticCacheName)
|
||||
.then(cache => {
|
||||
return cache.addAll(filesToCache);
|
||||
})
|
||||
)
|
||||
});
|
||||
|
||||
// Clear cache on activate
|
||||
self.addEventListener('activate', event => {
|
||||
event.waitUntil(
|
||||
caches.keys().then(cacheNames => {
|
||||
return Promise.all(
|
||||
cacheNames
|
||||
.filter(cacheName => (cacheName.startsWith("django-pwa-")))
|
||||
.filter(cacheName => (cacheName !== staticCacheName))
|
||||
.map(cacheName => caches.delete(cacheName))
|
||||
);
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
// Serve from Cache
|
||||
self.addEventListener("fetch", event => {
|
||||
event.respondWith(
|
||||
caches.match(event.request)
|
||||
.then(response => {
|
||||
return response || fetch(event.request);
|
||||
})
|
||||
.catch(() => {
|
||||
return caches.match('offline');
|
||||
})
|
||||
)
|
||||
});
|
||||
```
|
||||
|
||||
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 (PWA_APP_FETCH_URL is passed through).
|
||||
|
||||
```python
|
||||
PWA_SERVICE_WORKER_PATH = os.path.join(BASE_DIR, 'my_app', 'serviceworker.js')
|
||||
PWA_SERVICE_WORKER_PATH = 'my_app/serviceworker.js'
|
||||
|
||||
```
|
||||
|
||||
The offline view
|
||||
=====
|
||||
By default, the offline view is implemented in `templates/offline.html`
|
||||
You can overwrite it in a template directory if you continue using the default `serviceworker.js`.
|
||||
|
||||
|
||||
Feedback
|
||||
=====
|
||||
I welcome your feedback and pull requests. Enjoy!
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
default_app_config = 'pwa.apps.PwaConfig'
|
||||
@@ -3,8 +3,7 @@ from django.conf import settings
|
||||
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')
|
||||
@@ -15,10 +14,39 @@ PWA_APP_BACKGROUND_COLOR = getattr(settings, 'PWA_APP_BACKGROUND_COLOR', '#fff')
|
||||
PWA_APP_DISPLAY = getattr(settings, 'PWA_APP_DISPLAY', 'standalone')
|
||||
PWA_APP_ORIENTATION = getattr(settings, 'PWA_APP_ORIENTATION', 'any')
|
||||
PWA_APP_START_URL = getattr(settings, 'PWA_APP_START_URL', '/')
|
||||
PWA_APP_FETCH_URL = getattr(settings, 'PWA_APP_FETCH_URL', '/')
|
||||
PWA_APP_ICONS = getattr(settings, 'PWA_APP_ICONS', [
|
||||
{
|
||||
'src': '/',
|
||||
'sizes': '160x160'
|
||||
'src': '/static/images/icons/icon-72x72.png',
|
||||
'sizes': '72x72'
|
||||
},
|
||||
{
|
||||
'src': '/static/images/icons/icon-96x96.png',
|
||||
'sizes': '96x96'
|
||||
},
|
||||
{
|
||||
'src': '/static/images/icons/icon-128x128.png',
|
||||
'sizes': '128x128'
|
||||
},
|
||||
{
|
||||
'src': '/static/images/icons/icon-144x144.png',
|
||||
'sizes': '144x144'
|
||||
},
|
||||
{
|
||||
'src': '/static/images/icons/icon-152x152.png',
|
||||
'sizes': '152x152'
|
||||
},
|
||||
{
|
||||
'src': '/static/images/icons/icon-192x192.png',
|
||||
'sizes': '192x192'
|
||||
},
|
||||
{
|
||||
'src': '/static/images/icons/icon-384x384.png',
|
||||
'sizes': '384x384'
|
||||
},
|
||||
{
|
||||
'src': '/static/images/icons/icon-512x512.png',
|
||||
'sizes': '512x512'
|
||||
}
|
||||
])
|
||||
PWA_APP_DIR = getattr(settings, 'PWA_APP_DIR', 'auto')
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
@import url(https://fonts.googleapis.com/css?family=Raleway:300,400,600);/*!
|
||||
* Bootstrap v3.3.7 (http://getbootstrap.com)
|
||||
* Copyright 2011-2016 Twitter, Inc.
|
||||
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
|
||||
*//*! normalize.css v3.0.3 | MIT License | github.com/necolas/normalize.css */.label,sub,sup{vertical-align:baseline}hr,img{border:0}body,figure{margin:0}.btn-group>.btn-group,.btn-toolbar .btn,.btn-toolbar .btn-group,.btn-toolbar .input-group,.col-xs-1,.col-xs-10,.col-xs-11,.col-xs-12,.col-xs-2,.col-xs-3,.col-xs-4,.col-xs-5,.col-xs-6,.col-xs-7,.col-xs-8,.col-xs-9,.dropdown-menu{float:left}.img-responsive,.img-thumbnail,.table,label{max-width:100%}.navbar-fixed-bottom .navbar-collapse,.navbar-fixed-top .navbar-collapse,.pre-scrollable{max-height:340px}html{font-family:sans-serif;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}article,aside,details,figcaption,figure,footer,header,hgroup,main,menu,nav,section,summary{display:block}audio,canvas,progress,video{display:inline-block;vertical-align:baseline}audio:not([controls]){display:none;height:0}[hidden],template{display:none}a{background-color:transparent}a:active,a:hover{outline:0}b,optgroup,strong{font-weight:700}dfn{font-style:italic}h1{margin:.67em 0}mark{background:#ff0;color:#000}sub,sup{font-size:75%;line-height:0;position:relative}sup{top:-.5em}sub{bottom:-.25em}img{vertical-align:middle}svg:not(:root){overflow:hidden}hr{box-sizing:content-box;height:0}pre,textarea{overflow:auto}code,kbd,pre,samp{font-size:1em}button,input,optgroup,select,textarea{color:inherit;font:inherit;margin:0}button{overflow:visible}button,select{text-transform:none}button,html input[type=button],input[type=reset],input[type=submit]{-webkit-appearance:button;cursor:pointer}button[disabled],html input[disabled]{cursor:default}button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0}input[type=checkbox],input[type=radio]{box-sizing:border-box;padding:0}input[type=number]::-webkit-inner-spin-button,input[type=number]::-webkit-outer-spin-button{height:auto}input[type=search]::-webkit-search-cancel-button,input[type=search]::-webkit-search-decoration{-webkit-appearance:none}table{border-collapse:collapse;border-spacing:0}td,th{padding:0}/*! Source: https://github.com/h5bp/html5-boilerplate/blob/master/src/css/main.css */@media print{blockquote,img,pre,tr{page-break-inside:avoid}*,:after,:before{background:0 0!important;color:#000!important;box-shadow:none!important;text-shadow:none!important}a,a:visited{text-decoration:underline}a[href]:after{content:" (" attr(href) ")"}abbr[title]:after{content:" (" attr(title) ")"}a[href^="javascript:"]:after,a[href^="#"]:after{content:""}blockquote,pre{border:1px solid #999}thead{display:table-header-group}img{max-width:100%!important}h2,h3,p{orphans:3;widows:3}h2,h3{page-break-after:avoid}.navbar{display:none}.btn>.caret,.dropup>.btn>.caret{border-top-color:#000!important}.label{border:1px solid #000}.table{border-collapse:collapse!important}.table td,.table th{background-color:#fff!important}.table-bordered td,.table-bordered th{border:1px solid #ddd!important}}.btn,.btn-danger.active,.btn-danger:active,.btn-default.active,.btn-default:active,.btn-info.active,.btn-info:active,.btn-primary.active,.btn-primary:active,.btn-warning.active,.btn-warning:active,.btn.active,.btn:active,.dropdown-menu>.disabled>a:focus,.dropdown-menu>.disabled>a:hover,.form-control,.navbar-toggle,.open>.btn-danger.dropdown-toggle,.open>.btn-default.dropdown-toggle,.open>.btn-info.dropdown-toggle,.open>.btn-primary.dropdown-toggle,.open>.btn-warning.dropdown-toggle{background-image:none}.img-thumbnail,body{background-color:#f5f8fa}@font-face{font-family:'Glyphicons Halflings';src:url(../fonts/bootstrap/glyphicons-halflings-regular.eot);src:url(../fonts/bootstrap/glyphicons-halflings-regular.eot?#iefix) format("embedded-opentype"),url(../fonts/bootstrap/glyphicons-halflings-regular.woff2) format("woff2"),url(../fonts/bootstrap/glyphicons-halflings-regular.woff) format("woff"),url(../fonts/bootstrap/glyphicons-halflings-regular.ttf) format("truetype"),url(../fonts/bootstrap/glyphicons-halflings-regular.svg#glyphicons_halflingsregular) format("svg")}.glyphicon{position:relative;top:1px;display:inline-block;font-family:'Glyphicons Halflings';font-style:normal;font-weight:400;line-height:1;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}.glyphicon-asterisk:before{content:"\002a"}.glyphicon-plus:before{content:"\002b"}.glyphicon-eur:before,.glyphicon-euro:before{content:"\20ac"}.glyphicon-minus:before{content:"\2212"}.glyphicon-cloud:before{content:"\2601"}.glyphicon-envelope:before{content:"\2709"}.glyphicon-pencil:before{content:"\270f"}.glyphicon-glass:before{content:"\e001"}.glyphicon-music:before{content:"\e002"}.glyphicon-search:before{content:"\e003"}.glyphicon-heart:before{content:"\e005"}.glyphicon-star:before{content:"\e006"}.glyphicon-star-empty:before{content:"\e007"}.glyphicon-user:before{content:"\e008"}.glyphicon-film:before{content:"\e009"}.glyphicon-th-large:before{content:"\e010"}.glyphicon-th:before{content:"\e011"}.glyphicon-th-list:before{content:"\e012"}.glyphicon-ok:before{content:"\e013"}.glyphicon-remove:beforLine truncated
|
||||
|
After Width: | Height: | Size: 6.6 KiB |
|
After Width: | Height: | Size: 7.5 KiB |
|
After Width: | Height: | Size: 7.9 KiB |
|
After Width: | Height: | Size: 10 KiB |
|
After Width: | Height: | Size: 24 KiB |
|
After Width: | Height: | Size: 22 KiB |
|
After Width: | Height: | Size: 3.2 KiB |
|
After Width: | Height: | Size: 4.6 KiB |
@@ -0,0 +1,11 @@
|
||||
{% load static %}<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Default offline template</title>
|
||||
<link href="{% static '/css/django-pwa-app.css' %}" rel="stylesheet">
|
||||
</head>
|
||||
<body>
|
||||
<h1>You are currently not connected to any networks.</h1>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,2 +1,53 @@
|
||||
// Empty Service Worker implementation. To use your own Service Worker, set the PWA_SERVICE_WORKER_PATH variable in
|
||||
// settings.py
|
||||
// Base Service Worker implementation. To use your own Service Worker, set the PWA_SERVICE_WORKER_PATH variable in settings.py
|
||||
|
||||
var staticCacheName = "django-pwa-v" + new Date().getTime();
|
||||
var filesToCache = [
|
||||
'/offline',
|
||||
'/static/css/django-pwa-app.css',
|
||||
'/static/images/icons/icon-72x72.png',
|
||||
'/static/images/icons/icon-96x96.png',
|
||||
'/static/images/icons/icon-128x128.png',
|
||||
'/static/images/icons/icon-144x144.png',
|
||||
'/static/images/icons/icon-152x152.png',
|
||||
'/static/images/icons/icon-192x192.png',
|
||||
'/static/images/icons/icon-384x384.png',
|
||||
'/static/images/icons/icon-512x512.png',
|
||||
];
|
||||
|
||||
// Cache on install
|
||||
self.addEventListener("install", event => {
|
||||
this.skipWaiting();
|
||||
event.waitUntil(
|
||||
caches.open(staticCacheName)
|
||||
.then(cache => {
|
||||
return cache.addAll(filesToCache);
|
||||
})
|
||||
)
|
||||
});
|
||||
|
||||
// Clear cache on activate
|
||||
self.addEventListener('activate', event => {
|
||||
event.waitUntil(
|
||||
caches.keys().then(cacheNames => {
|
||||
return Promise.all(
|
||||
cacheNames
|
||||
.filter(cacheName => (cacheName.startsWith("django-pwa-")))
|
||||
.filter(cacheName => (cacheName !== staticCacheName))
|
||||
.map(cacheName => caches.delete(cacheName))
|
||||
);
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
// Serve from Cache
|
||||
self.addEventListener("fetch", event => {
|
||||
event.respondWith(
|
||||
caches.match(event.request)
|
||||
.then(response => {
|
||||
return response || fetch(event.request);
|
||||
})
|
||||
.catch(() => {
|
||||
return caches.match('offline');
|
||||
})
|
||||
)
|
||||
});
|
||||
@@ -1,8 +1,10 @@
|
||||
from django.conf.urls import url
|
||||
from .views import manifest, service_worker
|
||||
from django.urls import path
|
||||
|
||||
from .views import Manifest, ServiceWorker, OfflineView
|
||||
|
||||
# Serve up serviceworker.js and manifest.json at the root
|
||||
urlpatterns = [
|
||||
url('^serviceworker.js$', service_worker, name="serviceworker"),
|
||||
url('^manifest.json$', manifest, name="manifest")
|
||||
path('serviceworker.js', ServiceWorker.as_view(), name='serviceworker'),
|
||||
path('manifest.json', Manifest.as_view(), name='manifest'),
|
||||
path('offline', OfflineView.as_view(), name='offline')
|
||||
]
|
||||
@@ -1,17 +1,27 @@
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import render
|
||||
from django.views.generic.base import TemplateView
|
||||
|
||||
from . import app_settings
|
||||
|
||||
|
||||
def service_worker(request):
|
||||
response = HttpResponse(open(app_settings.PWA_SERVICE_WORKER_PATH).read(), content_type='application/javascript')
|
||||
return response
|
||||
class ServiceWorker(TemplateView):
|
||||
content_type = 'application/javascript'
|
||||
template_name = app_settings.PWA_SERVICE_WORKER_PATH
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
kwargs['PWA_APP_FETCH_URL'] = app_settings.PWA_APP_FETCH_URL
|
||||
return super().get_context_data(**kwargs)
|
||||
|
||||
|
||||
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_')
|
||||
})
|
||||
class Manifest(TemplateView):
|
||||
content_type = 'application/json'
|
||||
template_name = 'manifest.json'
|
||||
|
||||
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)
|
||||
|
||||
|
||||
class OfflineView(TemplateView):
|
||||
template_name = "offline.html"
|
||||
@@ -10,16 +10,21 @@ try:
|
||||
import pypandoc
|
||||
|
||||
long_description = pypandoc.convert('README.md', 'rst')
|
||||
except:
|
||||
except RuntimeError:
|
||||
long_description = short_description
|
||||
|
||||
# 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>=2",
|
||||
]
|
||||
|
||||
setup(
|
||||
name='django-pwa',
|
||||
version='1.0.0',
|
||||
version='1.0.1',
|
||||
packages=find_packages(),
|
||||
install_requires=install_requirements,
|
||||
include_package_data=True,
|
||||
license='MIT License',
|
||||
description=short_description,
|
||||
@@ -30,7 +35,8 @@ setup(
|
||||
classifiers=[
|
||||
'Environment :: Web Environment',
|
||||
'Framework :: Django',
|
||||
'Framework :: Django :: 1.10',
|
||||
'Framework :: Django :: 2.0',
|
||||
'Framework :: Django :: 2.1',
|
||||
'Intended Audience :: Developers',
|
||||
'License :: OSI Approved :: MIT License',
|
||||
'Operating System :: OS Independent',
|
||||
|
||||
@@ -33,4 +33,6 @@ DATABASES = {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
'NAME': 'mydatabase',
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
STATIC_URL = '/static/'
|
||||
@@ -15,6 +15,7 @@ class AppSettingsTest(TestCase):
|
||||
'PWA_APP_DISPLAY',
|
||||
'PWA_APP_ORIENTATION',
|
||||
'PWA_APP_START_URL',
|
||||
'PWA_APP_FETCH_URL',
|
||||
'PWA_APP_ICONS',
|
||||
'PWA_APP_DIR',
|
||||
'PWA_APP_LANG'
|
||||
|
||||
@@ -14,7 +14,14 @@ class CreateMetaTemplateTagTest(TestCase):
|
||||
def test_has_tags(self):
|
||||
"""Must contains the tags in HTML"""
|
||||
tags = [
|
||||
'<link rel="apple-touch-icon" href="/" sizes="160x160">',
|
||||
'<link rel="apple-touch-icon" href="/images/icons/icon-72x72.png" sizes="72x72">',
|
||||
'<link rel="apple-touch-icon" href="/images/icons/icon-96x96.png" sizes="96x96">',
|
||||
'<link rel="apple-touch-icon" href="/images/icons/icon-128x128.png" sizes="128x128">',
|
||||
'<link rel="apple-touch-icon" href="/images/icons/icon-144x144.png" sizes="144x144">',
|
||||
'<link rel="apple-touch-icon" href="/images/icons/icon-152x152.png" sizes="152x152">',
|
||||
'<link rel="apple-touch-icon" href="/images/icons/icon-192x192.png" sizes="192x192">',
|
||||
'<link rel="apple-touch-icon" href="/images/icons/icon-384x384.png" sizes="384x384">',
|
||||
'<link rel="apple-touch-icon" href="/images/icons/icon-512x512.png" sizes="512x512">',
|
||||
'<link rel="manifest" href="/manifest.json">',
|
||||
'<meta name="theme-color" content="#000">',
|
||||
'<meta name="apple-mobile-web-app-capable" content="yes">',
|
||||
|
||||
@@ -41,3 +41,13 @@ class ManifestTest(TestCase):
|
||||
for expected in contents:
|
||||
with self.subTest():
|
||||
self.assertContains(self.response, expected)
|
||||
|
||||
|
||||
class OfflineTest(TestCase):
|
||||
def setUp(self):
|
||||
self.response = self.client.get(r('offline'))
|
||||
|
||||
def test_get(self):
|
||||
"""GET /offline Should return status code 200"""
|
||||
self.assertEqual(200, self.response.status_code)
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
[tox]
|
||||
envlist = py36-django{20}
|
||||
envlist =
|
||||
py36-django{20}
|
||||
py36-django{21}
|
||||
|
||||
[testenv]
|
||||
commands = python runtests.py
|
||||
@@ -7,4 +9,6 @@ setenv =
|
||||
DJANGO_SETTINGS_MODULE=tests.settings
|
||||
PYTHONPATH={toxinidir}
|
||||
basepython = py36: python3.6
|
||||
deps = django20: Django==2.0
|
||||
deps =
|
||||
django20: Django==2.0
|
||||
django21: Django==2.1
|
||||