Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement service workers and custom offline fallback page (#8594)
Co-authored-by: quino <quino@populate.tools> Co-authored-by: Fernando Blat <fernando@blat.es>
- Loading branch information
1 parent
49d5ab8
commit 601982c
Showing
23 changed files
with
3,049 additions
and
176 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
decidim-core/app/controllers/decidim/manifests_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
# A controller to serve the manifest file for PWA | ||
class ManifestsController < Decidim::ApplicationController | ||
def show | ||
organization_presenter = OrganizationPresenter.new(current_organization) | ||
render layout: false, locals: { organization_params: organization_presenter }, content_type: "application/manifest+json" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
class OfflineController < Decidim::ApplicationController | ||
def show; end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 5 additions & 1 deletion
6
decidim-core/app/packs/src/decidim/security/selfxss_warning.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// check if the browser supports serviceWorker at all | ||
window.addEventListener("load", async () => { | ||
if ("serviceWorker" in navigator) { | ||
// eslint-disable-next-line no-unused-vars | ||
const registration = await navigator.serviceWorker.register("/sw.js", { scope: "/" }); | ||
} else { | ||
console.log("Your browser doesn't support service workers 🤷♀️"); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { | ||
imageCache, | ||
staticResourceCache, | ||
offlineFallback | ||
} from "workbox-recipes"; | ||
import { registerRoute } from "workbox-routing"; | ||
import { NetworkFirst, NetworkOnly } from "workbox-strategies"; | ||
import { CacheableResponsePlugin } from "workbox-cacheable-response"; | ||
import { ExpirationPlugin } from "workbox-expiration"; | ||
|
||
|
||
// https://developers.google.com/web/tools/workbox/guides/troubleshoot-and-debug#debugging_workbox | ||
self.__WB_DISABLE_DEV_LOGS = true | ||
|
||
/** | ||
* This is a workaround to bypass a webpack compilation error | ||
* | ||
* The InjectManifest function requires the __WB_MANIFEST somewhere in this file, | ||
* however, we cannot add precacheAndRoute as the issue suggests, | ||
* as the other workbox-recipes won't work properly | ||
* | ||
* See more: https://github.com/GoogleChrome/workbox/issues/2519#issuecomment-634164566 | ||
*/ | ||
// eslint-disable-next-line no-unused-vars | ||
const dummy = self.__WB_MANIFEST; | ||
|
||
// avoid caching admin or users paths | ||
registerRoute( | ||
({ url }) => ["/admin/", "/users/"].some((path) => url.pathname.startsWith(path)), | ||
new NetworkOnly() | ||
); | ||
|
||
// https://developers.google.com/web/tools/workbox/modules/workbox-recipes#pattern_3 | ||
registerRoute( | ||
({ request }) => request.mode === "navigate", | ||
new NetworkFirst({ | ||
networkTimeoutSeconds: 3, | ||
cacheName: "pages", | ||
plugins: [ | ||
new CacheableResponsePlugin({ | ||
statuses: [0, 200] | ||
}), | ||
new ExpirationPlugin({ | ||
maxAgeSeconds: 60 * 60 | ||
}) | ||
] | ||
}), | ||
); | ||
|
||
// common recipes | ||
staticResourceCache(); | ||
|
||
imageCache(); | ||
|
||
offlineFallback({ pageFallback: "/offline" }); |
10 changes: 10 additions & 0 deletions
10
decidim-core/app/presenters/decidim/organization_presenter.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
# A general presenter to render organization logic to build a manifest | ||
class OrganizationPresenter < SimpleDelegator | ||
def translated_description | ||
ActionView::Base.full_sanitizer.sanitize(translated_attribute(description)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "<%= organization_params.name %>", | ||
"lang": "<%= organization_params.default_locale %>", | ||
"description": "<%= organization_params.translated_description %>" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<div id="offline-fallback-html" style="text-align:center;padding:1rem"> | ||
<svg xmlns="http://www.w3.org/2000/svg" width="5rem" height="r5em" viewBox="0 0 25 25"> | ||
<path | ||
d="m20.293 4-1.477 1.477A4.473 4.473 0 0 0 11.17 7.33 4.362 4.362 0 0 0 9.5 7a4.486 4.486 0 0 0-4.23 3.01 4.49 4.49 0 0 0 .042 8.971L4 20.293l.707.707L21 4.707zM19.99 8.21a3.936 3.936 0 0 0-.16-.92L8.12 19H18.5a5.497 5.497 0 0 0 1.49-10.79z" | ||
style="fill:#1a181d" /> | ||
</svg> | ||
<h3><%= t(".message") %></h3> | ||
<button class="button large button--sc mt-s" onclick="location.reload()"><%= t(".retry") %></button> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
decidim-core/app/views/layouts/decidim/_offline_banner.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<div class="flash callout primary small js-offline-message" style="display: none"> | ||
<%= t(".cache_version_page") %> | ||
</div> | ||
<script> | ||
document.addEventListener("DOMContentLoaded", () => { | ||
// show the banner if it's offline AND the offline-fallback is not displaying | ||
if (!navigator.onLine && !document.querySelector("#offline-fallback-html")) { | ||
document.querySelector(".js-offline-message").style.display = "block" | ||
} | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
module Decidim | ||
describe ManifestsController, type: :controller do | ||
routes { Decidim::Core::Engine.routes } | ||
|
||
let(:organization) { create(:organization) } | ||
|
||
before do | ||
request.env["decidim.current_organization"] = organization | ||
end | ||
|
||
describe "GET /manifest.json" do | ||
render_views | ||
|
||
it "returns the manifest" do | ||
get :show, format: :json | ||
|
||
expect(response).to be_successful | ||
|
||
manifest = JSON.parse(response.body) | ||
expect(manifest["name"]).to eq(organization.name) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
module Decidim | ||
describe OfflineController, type: :controller do | ||
routes { Decidim::Core::Engine.routes } | ||
|
||
let(:organization) { create(:organization) } | ||
|
||
before do | ||
request.env["decidim.current_organization"] = organization | ||
end | ||
|
||
describe "GET /offline" do | ||
it "returns the offline content" do | ||
get :show | ||
|
||
expect(response).to be_successful | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.