Skip to content

Commit 5ea0059

Browse files
Merged in r2-2822-theme-caching (pull request #6743)
R2-2822: Dynamically caching theme and manifest in workbox Approved-by: Alberto Espinoza
2 parents ae27e86 + 0006b75 commit 5ea0059

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

app/javascript/libs/load-external-theme.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ const themePromise =
44
process.env.NODE_ENV !== "test"
55
? await (async () => {
66
if (window.useTheme) {
7-
const { default: importedTheme } = await import(/* webpackIgnore: true */ `${window.location.origin}/theme`);
7+
const { default: importedTheme } = await import(
8+
/* webpackIgnore: true */ `${window.location.origin}/theme?__WB_REVISION__=${window.themeRevision}`
9+
);
810

911
return importedTheme;
1012
}

app/javascript/worker.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ registerRoute(
7373
METHODS.GET
7474
);
7575

76+
registerRoute(
77+
/\/(?:theme|manifest).*$/,
78+
new CacheFirst({
79+
cacheName: "theme",
80+
plugins: [
81+
new ExpirationPlugin({
82+
maxEntries: 2
83+
})
84+
]
85+
}),
86+
METHODS.GET
87+
);
88+
7689
// Api Endpoints
7790
Object.values(METHODS).forEach(method => {
7891
registerRoute(/\/api\/.*/, new NetworkOnly(), method);
@@ -89,16 +102,7 @@ const manifest = self.__WB_MANIFEST.map(entry => {
89102
return entry;
90103
});
91104

92-
const revision = manifest?.filter(({ url }) => url === "/")?.[0]?.revision;
93-
94-
const themeFiles = ["theme", "manifest.json"].map(asset => {
95-
return {
96-
url: `${self.location.origin}/${asset}`,
97-
revision
98-
};
99-
});
100-
101-
precacheAndRoute([...manifest, ...themeFiles]);
105+
precacheAndRoute(manifest);
102106

103107
setCatchHandler(({ event }) => {
104108
if (isNav(event)) return caches.match(getCacheKeyForURL("/"));

app/models/theme.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ class Theme < ApplicationRecord
2828

2929
PICTORIAL_SIZES = %w[144 192 256].freeze
3030

31-
store_accessor :data, :site_description, :site_title, :colors, :use_contained_nav_style, :show_powered_by_primero
31+
store_accessor :data, :site_description, :site_title, :colors, :use_contained_nav_style, :show_powered_by_primero,
32+
:revision
3233

3334
has_one_attached :login_background
3435
has_one_attached :logo
@@ -47,6 +48,12 @@ class Theme < ApplicationRecord
4748
validates :favicon, presence: true
4849
# rubocop:enable Naming/VariableNumber
4950

51+
before_save :generate_new_revision
52+
53+
def generate_new_revision
54+
self.revision = SecureRandom.uuid
55+
end
56+
5057
def valid_html_colors
5158
return unless colors.present?
5259

app/views/layouts/application.html.erb

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<meta name="google" value="notranslate" />
1313
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
1414
<title><%= @theme&.site_title || 'Primero' %></title>
15-
<link rel="manifest" href="/manifest.json">
15+
<link rel="manifest" href="/manifest.json?__WB_REVISION__=<%= @theme.revision %>">
1616
<link rel="icon" type="image/x-icon" href="<%= @theme&.favicon.present? ? rails_blob_path(@theme&.favicon, only_path: true) : '/favicon.ico' %>" />
1717
<!-- jss-insertion-point -->
1818
<%= stylesheet_bundle_tag 'application', skip_pipeline: true, manifest: :application, nonce: true %>
@@ -22,6 +22,7 @@
2222
<%= javascript_tag nonce: true do %>
2323
window.locationManifest = <%= available_locations %>
2424
window.useTheme = <%= @theme.present? %>
25+
window.themeRevision = "<%= @theme.revision %>"
2526
<% end %>
2627
<%= javascript_include_tag '/javascripts/i18n.js', nonce: true %>
2728

0 commit comments

Comments
 (0)