Skip to content

Commit

Permalink
Merge pull request unicef#1247 from sheralim012/bug/offline-cache-for…
Browse files Browse the repository at this point in the history
…-assets

return assets urls for precache
  • Loading branch information
geoo89 authored May 16, 2022
2 parents aab2fcd + c642dbb commit 3045d74
Show file tree
Hide file tree
Showing 73 changed files with 89 additions and 33 deletions.
7 changes: 4 additions & 3 deletions home/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ def get_page(self):
return self

def get_icon(self):
from wagtail.images.views.serve import generate_image_url

class Icon(object):
url = ''
is_svg_icon = False
Expand All @@ -51,11 +49,14 @@ def __init__(self, url='', is_svg_icon=False):
self.url = url
self.is_svg_icon = is_svg_icon

def __str__(self):
return f'{self.url}'

icon = Icon()
if hasattr(self, 'icon') and self.icon:
icon = Icon(self.icon.url, True)
icon.path = self.icon.file.name
elif hasattr(self, 'image_icon') and self.image_icon:
icon = Icon(generate_image_url(self.image_icon, 'fill-32x32'), False)
icon = Icon(self.image_icon.get_rendition('fill-32x32').url, False)

return icon
2 changes: 2 additions & 0 deletions iogt/static/js/iogt.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,7 @@ $(document).ready(() => {
$(window).on('online', () => enableForOnlineAccess());

window.navigator.onLine ? enableForOnlineAccess() : disableForOfflineAccess();
} else {
offlineAppBtns.hide();
}
});
File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
2 changes: 1 addition & 1 deletion iogt/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
</script>
<script src="{% url 'javascript-catalog' %}"></script>
<script src="{% static 'js/sw-init.js' %}"></script>
<script src="{% static 'third_party/jquery-3.6.0.min.js' %}"></script>
<script src="{% static 'js/jquery-3.6.0.min.js' %}"></script>
<script src="{% static 'js/iogt.js' %}"></script>
<noscript>
<style type="text/css">
Expand Down
37 changes: 21 additions & 16 deletions iogt/templates/sw.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
importScripts('../static/third_party/workbox/workbox-v6.1.5/workbox-sw.js');

importScripts('../../static/js/workbox/workbox-v6.1.5/workbox-sw.js');

// Below we are using a custom dimension to track online vs. offline interactions. So make
// sure to create a custom dimension on GA

workbox.googleAnalytics.initialize({
parameterOverrides: {
cd1: 'offline',
},
parameterOverrides: {
cd1: 'offline',
},
});

const precacheController = new workbox.precaching.PrecacheController();

self.addEventListener('install', event => {
const resp = fetch(`${location.protocol}//${location.host}/sitemap/`)
self.addEventListener('install', (event) => {
const resp = fetch('/sitemap/')
.then(response => response.json())
.then(urls => {
precacheController.addToCacheList(urls.map(url => ({
url,
revision: null,
})));
workbox.precaching.precacheAndRoute(urls.map(url => ({
url,
revision: null,
})));
})
.then(() => {
}).then(() => {
// Passing in event is required in Workbox v6+
precacheController.install(event)
});
event.waitUntil(precacheController.install(event));
})
event.waitUntil(resp);
});

self.addEventListener('activate', (event) => {
// Passing in event is required in Workbox v6+
event.waitUntil(precacheController.activate(event));
});

self.addEventListener('fetch', (event) => {
event.respondWith(
fetch(event.request)
.catch(() => caches.match(event.request))
);
});
72 changes: 60 additions & 12 deletions iogt/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import os
from pathlib import Path

from django.conf import settings
from django.contrib.admin.utils import flatten
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.views.generic import TemplateView
from rest_framework.response import Response
from rest_framework.views import APIView
from wagtail.core.models import Page
from wagtail.core.models import Page, Locale
from wagtail.images.models import Rendition
from wagtailmedia.models import Media

from questionnaires.models import Poll, Survey, Quiz

Expand Down Expand Up @@ -46,17 +52,59 @@ def get_context_data(self, **kwargs):

class SitemapAPIView(APIView):
def get(self, request):
from home.models import HomePage, Section, Article, FooterPage

home_page_urls = [p.full_url for p in HomePage.objects.live()],
section_urls = [p.full_url for p in Section.objects.live()],
article_urls = [p.full_url for p in Article.objects.live()],
footer_urls = [p.full_url for p in FooterPage.objects.live()],
poll_urls = [p.full_url for p in Poll.objects.live()],
survey_urls = [p.full_url for p in Survey.objects.live()],
quiz_urls = [p.full_url for p in Quiz.objects.live()],
from home.models import HomePage, Section, Article, FooterPage, OfflineAppPage, SVGToPNGMap

home_page_urls = [p.url for p in HomePage.objects.live()],
section_urls = [p.url for p in Section.objects.live()],
article_urls = [p.url for p in Article.objects.live()],
footer_urls = [p.url for p in FooterPage.objects.live()],
poll_urls = [p.url for p in Poll.objects.live()],
survey_urls = [p.url for p in Survey.objects.live()],
quiz_urls = [p.url for p in Quiz.objects.live()],
offline_app_page_urls = [p.url for p in OfflineAppPage.objects.live()],

jsi18n_urls = []
for locale in Locale.objects.all():
jsi18n_urls.append(f'/{locale.language_code}/jsi18n/')

image_urls = []
for image in Rendition.objects.all():
image_urls.append(f'{settings.MEDIA_URL}{image.file.name}')

for image in SVGToPNGMap.objects.all():
image_urls.append(f'{settings.MEDIA_URL}{image.png_image_file.name}')

media_urls = []
for media in Media.objects.all():
media_urls.append(f'{settings.MEDIA_URL}{media.file.name}')
if media.thumbnail:
image_urls.append(f'{settings.MEDIA_URL}{media.thumbnail.name}')

static_urls = []
static_dirs = [
{'name': 'css', 'extensions': ('.css',)},
{'name': 'js', 'extensions': ('.js',)},
{'name': 'fonts', 'extensions': ('.woff', '.woff2',)},
{'name': 'icons', 'extensions': ('.svg',)},
]
for static_dir in static_dirs:
for root, dirs, files in os.walk(Path(settings.STATIC_ROOT) / static_dir['name']):
for file in files:
if file.endswith(static_dir['extensions']):
static_urls.append(f'{settings.STATIC_URL}{root.split("/static/")[-1]}/{file}')

sitemap = flatten(
home_page_urls + section_urls + article_urls + footer_urls + poll_urls + survey_urls + quiz_urls)

home_page_urls +
section_urls +
article_urls +
footer_urls +
poll_urls +
survey_urls +
quiz_urls +
offline_app_page_urls +
tuple(static_urls) +
tuple(image_urls) +
tuple(media_urls) +
tuple(jsi18n_urls)
)
return Response(sitemap)

0 comments on commit 3045d74

Please sign in to comment.