Skip to content

Commit

Permalink
feat(pwa): further modify PWA configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
timoknapp committed Mar 13, 2023
1 parent 589c497 commit e385e00
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 29 deletions.
Binary file modified images/favicon.ico
Binary file not shown.
Binary file modified images/icon192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/icon512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/screenshots/1112x834-screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/screenshots/750x1334-screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/splash-13pro.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/splash-13promax.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/splash-14pro.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/splash-14promax.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 5 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Ubuntu+Mono:wght@400;700&display=swap" rel="stylesheet">
<title>Tennis Tournament Finder</title>
</html>


<!-- Make sure you put this AFTER Leaflet's CSS -->
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
integrity="sha512-BB3hKbKWOc9Ez/TAwyWxNXeoV9c1v6FIeYiBieIWkpLjauysF18NzgR1MBNBXf8/KABdlkX68nAhlwcDFLGPCQ=="
crossorigin=""></script>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js" integrity="sha512-BB3hKbKWOc9Ez/TAwyWxNXeoV9c1v6FIeYiBieIWkpLjauysF18NzgR1MBNBXf8/KABdlkX68nAhlwcDFLGPCQ==" crossorigin=""></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.5.3/leaflet.markercluster.js"></script>

<link rel="stylesheet" type="text/css" href="css/main.css">
<title>Tennis Tournament Finder</title>

<script>navigator.serviceWorker.register("service-worker.js")</script>
</head>
<body>
<div class="sideBar">
Expand Down
25 changes: 25 additions & 0 deletions manifest-local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"id": "/tennis-tournament-finder/",
"name": "Tennis Tournament Finder",
"language": "de-DE",
"short_name": "TTF",
"description": "Finde Tennis Turniere in deiner Nähe",
"start_url": "/",
"scope": "/",
"display": "fullscreen",
"theme_color": "#000",
"background_color": "#000",
"orientation": "any",
"icons": [
{
"src": "/images/icon192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "/images/icon512.png",
"type": "image/png",
"sizes": "512x512"
}
]
}
72 changes: 49 additions & 23 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@
{
"id": "/tennis-tournament-finder/",
"name": "Tennis Tournament Finder",
"language": "de-DE",
"short_name": "TTF",
"description": "Finde Tennis Turniere in deiner Nähe",
"start_url": "/tennis-tournament-finder/",
"scope": "/tennis-tournament-finder/",
"display": "standalone",
"theme_color": "#000",
"background_color": "#000",
"orientation": "any",
"icons": [
{
"src": "/tennis-tournament-finder/images/icon192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "/tennis-tournament-finder/images/icon512.png",
"type": "image/png",
"sizes": "512x512"
}
]
"id": "io.github.timoknapp.tennis-tournament-finder",
"name": "Tennis Tournament Finder",
"language": "de-DE",
"short_name": "TTF",
"description": "Finde Tennis Turniere in deiner Nähe",
"start_url": "/tennis-tournament-finder/",
"scope": "/tennis-tournament-finder/",
"display": "standalone",
"theme_color": "#000",
"background_color": "#000",
"orientation": "any",
"icons": [
{
"src": "/tennis-tournament-finder/images/icon192.png",
"type": "image/png",
"sizes": "192x192",
"purpose": "any"
},
{
"src": "/tennis-tournament-finder/images/icon512.png",
"type": "image/png",
"sizes": "512x512",
"purpose": "any"
},
{
"src": "/tennis-tournament-finder/images/icon192.png",
"type": "image/png",
"sizes": "192x192",
"purpose": "maskable"
},
{
"src": "/tennis-tournament-finder/images/icon512.png",
"type": "image/png",
"sizes": "512x512",
"purpose": "maskable"
}
],
"screenshots": [
{
"src": "images/screenshots/1112x834-screenshot.png",
"sizes": "1112x834",
"type": "image/png"
},
{
"src": "images/screenshots/750x1334-screenshot.png",
"sizes": "750x1334",
"type": "image/png"
}
]
}
93 changes: 93 additions & 0 deletions service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@

// Based off of https://github.com/pwa-builder/PWABuilder/blob/main/docs/sw.js

/*
Welcome to our basic Service Worker! This Service Worker offers a basic offline experience
while also being easily customizeable. You can add in your own code to implement the capabilities
listed below, or change anything else you would like.
Need an introduction to Service Workers? Check our docs here: https://docs.pwabuilder.com/#/home/sw-intro
Want to learn more about how our Service Worker generation works? Check our docs here: https://docs.pwabuilder.com/#/studio/existing-app?id=add-a-service-worker
Did you know that Service Workers offer many more capabilities than just offline?
- Background Sync: https://microsoft.github.io/win-student-devs/#/30DaysOfPWA/advanced-capabilities/06
- Periodic Background Sync: https://web.dev/periodic-background-sync/
- Push Notifications: https://microsoft.github.io/win-student-devs/#/30DaysOfPWA/advanced-capabilities/07?id=push-notifications-on-the-web
- Badges: https://microsoft.github.io/win-student-devs/#/30DaysOfPWA/advanced-capabilities/07?id=application-badges
*/

const HOSTNAME_WHITELIST = [
self.location.hostname,
'fonts.gstatic.com',
'fonts.googleapis.com',
'cdn.jsdelivr.net'
]

// The Util Function to hack URLs of intercepted requests
const getFixedUrl = (req) => {
var now = Date.now()
var url = new URL(req.url)

// 1. fixed http URL
// Just keep syncing with location.protocol
// fetch(httpURL) belongs to active mixed content.
// And fetch(httpRequest) is not supported yet.
url.protocol = self.location.protocol

// 2. add query for caching-busting.
// Github Pages served with Cache-Control: max-age=600
// max-age on mutable content is error-prone, with SW life of bugs can even extend.
// Until cache mode of Fetch API landed, we have to workaround cache-busting with query string.
// Cache-Control-Bug: https://bugs.chromium.org/p/chromium/issues/detail?id=453190
if (url.hostname === self.location.hostname) {
url.search += (url.search ? '&' : '?') + 'cache-bust=' + now
}
return url.href
}

/**
* @Lifecycle Activate
* New one activated when old isnt being used.
*
* waitUntil(): activating ====> activated
*/
self.addEventListener('activate', event => {
event.waitUntil(self.clients.claim())
})

/**
* @Functional Fetch
* All network requests are being intercepted here.
*
* void respondWith(Promise<Response> r)
*/
self.addEventListener('fetch', event => {
// Skip some of cross-origin requests, like those for Google Analytics.
if (HOSTNAME_WHITELIST.indexOf(new URL(event.request.url).hostname) > -1) {
// Stale-while-revalidate
// similar to HTTP's stale-while-revalidate: https://www.mnot.net/blog/2007/12/12/stale
// Upgrade from Jake's to Surma's: https://gist.github.com/surma/eb441223daaedf880801ad80006389f1
const cached = caches.match(event.request)
const fixedUrl = getFixedUrl(event.request)
const fetched = fetch(fixedUrl, { cache: 'no-store' })
const fetchedCopy = fetched.then(resp => resp.clone())

// Call respondWith() with whatever we get first.
// If the fetch fails (e.g disconnected), wait for the cache.
// If there’s nothing in cache, wait for the fetch.
// If neither yields a response, return offline pages.
event.respondWith(
Promise.race([fetched.catch(_ => cached), cached])
.then(resp => resp || fetched)
.catch(_ => { /* eat any errors */ })
)

// Update the cache with the version we fetched (only for ok status)
event.waitUntil(
Promise.all([fetchedCopy, caches.open("pwa-cache")])
.then(([response, cache]) => response.ok && cache.put(event.request, response))
.catch(_ => { /* eat any errors */ })
)
}
})

0 comments on commit e385e00

Please sign in to comment.