Skip to content

Commit

Permalink
added web manifest and service worker, thus making Scoold a PWA
Browse files Browse the repository at this point in the history
  • Loading branch information
albogdano committed Mar 31, 2022
1 parent 308bec7 commit 5c7e8c9
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 8 deletions.
16 changes: 13 additions & 3 deletions src/main/java/com/erudika/scoold/controllers/SearchController.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,26 @@ public ResponseEntity<String> webmanifest(HttpServletRequest req) {
String json = "{\n"
+ " \"theme_color\": \"#03a9f4\",\n"
+ " \"background_color\": \"#FFFFFF\",\n"
+ " \"display\": \"browser\",\n"
+ " \"display\": \"minimal-ui\",\n"
+ " \"scope\": \"/\",\n"
+ " \"start_url\": \"" + CONF.serverContextPath() + "\",\n"
+ " \"start_url\": \"" + CONF.serverContextPath() + "/\",\n"
+ " \"name\": \"" + CONF.appName() + "\",\n"
+ " \"description\": \"" + CONF.metaDescription() + "\",\n"
+ " \"short_name\": \"" + CONF.appName() + "\",\n"
+ " \"icons\": [\n"
+ " {\n"
+ " \"src\": \"" + CONF.logoUrl() + "\",\n"
+ " \"sizes\": \"550x550\",\n"
+ " \"sizes\": \"any\",\n"
+ " \"type\": \"image/svg-xml\"\n"
+ " },{\n"
+ " \"src\": \"" + CONF.imagesLink() + "/maskable512.png\",\n"
+ " \"sizes\": \"512x512\",\n"
+ " \"purpose\": \"maskable\",\n"
+ " \"type\": \"image/png\"\n"
+ " },{\n"
+ " \"src\": \"" + CONF.metaAppIconUrl() + "\",\n"
+ " \"sizes\": \"any\",\n"
+ " \"type\": \"image/png\"\n"
+ " }\n"
+ " ]\n"
+ "}";
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ spring.servlet.multipart.max-file-size=-1
spring.servlet.multipart.max-request-size=-1
# response compression
server.compression.enabled=true
server.compression.min-response-size=1
server.compression.min-response-size=128
server.compression.mime-types=text/html, text/xml, text/plain, text/css, text/javascript, application/javascript, application/json
Binary file added src/main/resources/static/images/maskable512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 76 additions & 0 deletions src/main/resources/static/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2015, 2019 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Incrementing OFFLINE_VERSION will kick off the install event and force
// previously cached resources to be updated from the network.
const OFFLINE_VERSION = 1;
const CACHE_NAME = 'offline';
// Customize this with a different URL if needed.
const OFFLINE_URL = 'offline.html';

self.addEventListener('install', (event) => {
event.waitUntil((async () => {
const cache = await caches.open(CACHE_NAME);
// Setting {cache: 'reload'} in the new request will ensure that the response
// isn't fulfilled from the HTTP cache; i.e., it will be from the network.
await cache.add(new Request(OFFLINE_URL, {cache: 'reload'})).catch(() => {});
})());
});

self.addEventListener('activate', (event) => {
event.waitUntil((async () => {
// Enable navigation preload if it's supported.
// See https://developers.google.com/web/updates/2017/02/navigation-preload
if ('navigationPreload' in self.registration) {
await self.registration.navigationPreload.enable();
}
})());

// Tell the active service worker to take control of the page immediately.
self.clients.claim();
});

self.addEventListener('fetch', (event) => {
// We only want to call event.respondWith() if this is a navigation request
// for an HTML page.
if (event.request.mode === 'navigate') {
event.respondWith((async () => {
try {
// First, try to use the navigation preload response if it's supported.
const preloadResponse = await event.preloadResponse;
if (preloadResponse) {
return preloadResponse;
}

const networkResponse = await fetch(event.request);
return networkResponse;
} catch (error) {
// catch is only triggered if an exception is thrown, which is likely
// due to a network error.
// If fetch() returns a valid HTTP response with a response code in
// the 4xx or 5xx range, the catch() will NOT be called.
console.log('Fetch failed; returning offline page instead.', error);

const cache = await caches.open(CACHE_NAME);
const cachedResponse = await cache.match(OFFLINE_URL);
return cachedResponse;
}
})());
}

// If our if() condition is false, then this fetch handler won't intercept the
// request. If there are any other fetch handlers registered, they will get a
// chance to call event.respondWith(). If no fetch handlers call
// event.respondWith(), the request will be handled by the browser as if there
// were no service worker involvement.
});
10 changes: 7 additions & 3 deletions src/main/resources/templates/base.vm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<title>$!APPNAME | $!title</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
<meta name="theme-color" media="(prefers-color-scheme: light)" content="white">
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="black">
<meta name="description" content="$!description">
<meta name="keywords" content="$!keywords">
<meta name="generator" content="Scoold">
Expand All @@ -24,8 +26,9 @@
<meta name="twitter:title" content="$!title">
<meta name="twitter:description" content="$!description">
<meta name="twitter:image" content="$!ogimage">
<link href="$!request.contextPath/opensearch.xml" title="$!APPNAME" type="application/opensearchdescription+xml" rel="search">
<link href="$!request.contextPath/feed.xml" rel="alternate" type="application/atom+xml" title="New questions feed">
<link rel="manifest" href="$!{CONTEXT_PATH}/manifest.webmanifest">
<link href="$!{CONTEXT_PATH}/opensearch.xml" title="$!APPNAME" type="application/opensearchdescription+xml" rel="search">
<link href="$!{CONTEXT_PATH}/feed.xml" rel="alternate" type="application/atom+xml" title="New questions feed">
<link href="$!ogimage" rel="image_src">
<link href="$!ogimage" rel="apple-touch-icon">
<link href="$!faviconUrl" rel="icon" sizes="any">
Expand Down Expand Up @@ -169,7 +172,7 @@
#end

<div class="brand-logo">
<a href="$!request.contextPath/">
<a href="$!{CONTEXT_PATH}/">
<img src="$logoUrl" class="scoold-logo" width="$logoWidth" alt="scoold logo">
</a>
</div>
Expand Down Expand Up @@ -419,6 +422,7 @@
<script nonce="$cspNonce">$ctxPathJS;$isRTLJS;$isAdminJS;$maxTagsJS;$minPassLen;
$avatarUploadsEnabledJS;$imgurJS;$cloudinaryJS;
$_welcomeMessageJS;$_welcomeMessageOnLoginJS;
if ('serviceWorker' in navigator) {navigator.serviceWorker.register(CONTEXT_PATH + '/service-worker.js');}
</script>
<script nonce="$cspNonce" src="$!scriptslink/scoold.js?r=$!rev"></script>

Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/templates/questions.vm
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@
#set($filterActiveTooltip = $!lang.get('posts.filtered'))
#end
<a href="#" id="question-filter-btn" class="next-div-toggle chip tooltipped ${filterActiveClass}"
data-tooltip="${filterActiveTooltip}"><i class="fa fa-filter"></i><span class="hide">Filter</span></a>
data-tooltip="${filterActiveTooltip}" title="${filterActiveTooltip}">
<i class="fa fa-filter"></i><span class="hide">Filter</span>
</a>
</div>

<h2 class="hide-on-med-and-down">#questionsTitle()</h2>
Expand Down

0 comments on commit 5c7e8c9

Please sign in to comment.