| @@ -205,27 +205,30 @@ class App < Sinatra::Application · 204 unmodified lines |
| 205 | ].compact | 205 | ].compact |
| 206 | end | 206 | end |
| 207 | | 207 | |
| | 208 | # Returns a cache name for the service worker to use which changes if any of the assets changes |
| | 209 | # When a manifest exist, this name is only created once based on the asset manifest because it never changes without a server restart |
| | 210 | # If a manifest does not exist, it is created every time this method is called because the assets can change while the server is running |
| 208 | def service_worker_cache_name | 211 | def service_worker_cache_name |
| 209 | # Returns the digest of the last modified asset | | |
| 210 | # When a manifest exist, this digest is only calculated once based on the asset manifest because it never changes without a server restart | | |
| 211 | # If a manifest does not exist, it is calculated every time this method is called because the assets can change while the server is running | | |
| 212 | if File.exist?(App.assets_manifest_path) | 212 | if File.exist?(App.assets_manifest_path) |
| 213 | return @@service_worker_cache_name ||= | 213 | if defined?(@@service_worker_cache_name) |
| 214 | Sprockets::Manifest | 214 | return @@service_worker_cache_name |
| 215 | .new(nil, App.assets_manifest_path) | 215 | end |
| 216 | .files | 216 | |
| 217 | .values | 217 | digest = Sprockets::Manifest |
| 218 | .sort_by {|file| file["mtime"]} | 218 | .new(nil, App.assets_manifest_path) |
| 219 | .reverse | 219 | .files |
| 220 | .first["digest"] | 220 | .values |
| | 221 | .map {|file| file["digest"]} |
| | 222 | .join |
| | 223 | |
| | 224 | return @@service_worker_cache_name ||= Digest::MD5.hexdigest(digest) |
| 221 | else | 225 | else |
| 222 | last_modified_file = App.sprockets | 226 | paths = App.sprockets |
| 223 | .each_file | 227 | .each_file |
| 224 | .to_a | 228 | .to_a |
| 225 | .reject {|file| file.start_with?(App.docs_path)} | 229 | .reject {|file| file.start_with?(App.docs_path)} |
| 226 | .max_by {|file| File.mtime(file)} | | |
| 227 | | 230 | |
| 228 | return App.sprockets.pack_base64digest(App.sprockets.file_digest(last_modified_file)) | 231 | return App.sprockets.pack_hexdigest(App.sprockets.files_digest(paths)) |
| 229 | end | 232 | end |
| 230 | end | 233 | end |
| 231 | | 234 | |