serviceworker-rails 0.4.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +6 -2
- data/Appraisals +12 -0
- data/CHANGELOG.md +13 -0
- data/README.md +79 -17
- data/Rakefile +2 -0
- data/bin/appraisal +17 -0
- data/gemfiles/rails_3.gemfile +16 -0
- data/gemfiles/rails_4.gemfile +15 -0
- data/gemfiles/rails_5.gemfile +15 -0
- data/lib/assets/images/convert.rb +3 -0
- data/lib/assets/images/serviceworker-rails/heart-1200x1200.png +0 -0
- data/lib/assets/images/serviceworker-rails/heart-120x120.png +0 -0
- data/lib/assets/images/serviceworker-rails/heart-152x152.png +0 -0
- data/lib/assets/images/serviceworker-rails/heart-180x180.png +0 -0
- data/lib/assets/images/serviceworker-rails/heart-192x192.png +0 -0
- data/lib/assets/images/serviceworker-rails/heart-36x36.png +0 -0
- data/lib/assets/images/serviceworker-rails/heart-48x48.png +0 -0
- data/lib/assets/images/serviceworker-rails/heart-512x512.png +0 -0
- data/lib/assets/images/serviceworker-rails/heart-60x60.png +0 -0
- data/lib/assets/images/serviceworker-rails/heart-72x72.png +0 -0
- data/lib/assets/images/serviceworker-rails/heart-76x76.png +0 -0
- data/lib/assets/images/serviceworker-rails/heart-96x96.png +0 -0
- data/lib/generators/serviceworker/install_generator.rb +36 -24
- data/lib/generators/serviceworker/templates/manifest.json +6 -53
- data/lib/generators/serviceworker/templates/offline.html +66 -0
- data/lib/generators/serviceworker/templates/serviceworker.js +53 -14
- data/lib/serviceworker/{railtie.rb → engine.rb} +9 -1
- data/lib/serviceworker/handler.rb +1 -0
- data/lib/serviceworker/rails.rb +1 -1
- data/lib/serviceworker/rails/handler.rb +1 -0
- data/lib/serviceworker/rails/version.rb +2 -1
- data/lib/serviceworker/route.rb +1 -0
- data/serviceworker-rails.gemspec +1 -0
- metadata +37 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1375be90e53a9669b3c5cf8e87c9b60fc7358280
|
4
|
+
data.tar.gz: 1ca30b3dc68fe59b06d5b16fd3373820d281e861
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5122e12977bddf53e9800f8c2995d5b21da94f21ef8569ee1e5a74f447123f26ddce865d7f8855f5b0919feeb6c7658267bdbe6ddf5044e126d4cd5ecd5c91af
|
7
|
+
data.tar.gz: 2846135d62fb691d98a3979d1d86ad86b57d8a7c2394d2e06495332018ddd158801b7838d219fd49a16852b2b9b8c10d0b47f59c7bbebeeff4d61571c119fbd7
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/Appraisals
ADDED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
### 0.5.0 - 2016-11-10
|
2
|
+
|
3
|
+
* enhancements
|
4
|
+
* Convert railtie to Rails engine
|
5
|
+
* Extend install generator to add offline page and starter icons for generated web app manifest
|
6
|
+
* Install serviceworker and manifest as .erb files
|
7
|
+
|
8
|
+
### 0.4.0 - 2016-11-09
|
9
|
+
|
10
|
+
* enhancements
|
11
|
+
* Add install generator to insert code snippets and add default serviceworker
|
12
|
+
and companion scripts, web app manifest, and Rails initializer
|
13
|
+
|
1
14
|
### 0.3.1 - 2016-05-03
|
2
15
|
|
3
16
|
* enhancements
|
data/README.md
CHANGED
@@ -61,6 +61,29 @@ your Rails project.
|
|
61
61
|
$ rails g serviceworker:install
|
62
62
|
```
|
63
63
|
|
64
|
+
The generator will create the following files:
|
65
|
+
|
66
|
+
* `config/initializers/serviceworker.rb` - for configuring your Rails app
|
67
|
+
* `app/assets/javascripts/serviceworker.js.erb` - a blank Service Worker
|
68
|
+
script with some example strategies
|
69
|
+
* `app/assets/javascripts/serviceworker-companion.js` - a snippet of JavaScript
|
70
|
+
necessary to register your Service Worker in the browser
|
71
|
+
* `app/assets/javascripts/manifest.json.erb` - a starter web app manifest
|
72
|
+
pointing to some default app icons provided by the gem
|
73
|
+
* `public/offline.html` - a starter offline page
|
74
|
+
|
75
|
+
It will also make the following modifications to existing files:
|
76
|
+
|
77
|
+
* Adds a sprockets directive to `application.js` to require
|
78
|
+
`serviceworker-companion.js`
|
79
|
+
* Adds `serviceworker.js` and `manifest.json` to the list of compiled assets in
|
80
|
+
`config/initializers/assets.rb`
|
81
|
+
* Injects tags into the `head` of `app/views/layouts/application.html.erb` for
|
82
|
+
linking to the web app manifest
|
83
|
+
|
84
|
+
To learn more about each of the changes or to perform the set up yourself, check
|
85
|
+
out the manual setup section below.
|
86
|
+
|
64
87
|
### Manual setup
|
65
88
|
|
66
89
|
Let's add a `ServiceWorker` to cache some of your JavaScript and CSS assets. We'll assume you already have a Rails application using the asset pipeline built on Sprockets.
|
@@ -75,34 +98,41 @@ console.log('[Service Worker] Hello world!');
|
|
75
98
|
|
76
99
|
function onInstall(event) {
|
77
100
|
event.waitUntil(
|
78
|
-
caches.open('cached-assets').then(function prefill(cache) {
|
101
|
+
caches.open('cached-assets-v1').then(function prefill(cache) {
|
79
102
|
return cache.addAll([
|
80
103
|
'<%= asset_path "application.js" %>',
|
81
104
|
'<%= asset_path "application.css" %>',
|
82
|
-
'
|
105
|
+
'/offline.html',
|
83
106
|
// you get the idea ...
|
84
107
|
]);
|
85
108
|
})
|
86
109
|
);
|
87
110
|
}
|
88
111
|
|
112
|
+
function onActivate(event) {
|
113
|
+
console.log('[Serviceworker]', "Activating!", event);
|
114
|
+
event.waitUntil(
|
115
|
+
caches.keys().then(function(cacheNames) {
|
116
|
+
return Promise.all(
|
117
|
+
cacheNames.filter(function(cacheName) {
|
118
|
+
// Return true if you want to remove this cache,
|
119
|
+
// but remember that caches are shared across
|
120
|
+
// the whole origin
|
121
|
+
return key.indexOf('v1') !== 0;
|
122
|
+
}).map(function(cacheName) {
|
123
|
+
return caches.delete(cacheName);
|
124
|
+
})
|
125
|
+
);
|
126
|
+
})
|
127
|
+
);
|
128
|
+
}
|
129
|
+
|
89
130
|
self.addEventListener('install', onInstall)
|
131
|
+
self.addEventListener('activate', onActivate)
|
90
132
|
```
|
91
133
|
|
92
134
|
For use in production, instruct Sprockets to precompile service worker scripts separately from `application.js`, as in the following example:
|
93
135
|
|
94
|
-
#### Precompile the asset
|
95
|
-
|
96
|
-
```ruby
|
97
|
-
# config/initializers/assets.rb
|
98
|
-
|
99
|
-
Rails.application.configure do
|
100
|
-
config.assets.precompile += %w[
|
101
|
-
serviceworker.js
|
102
|
-
]
|
103
|
-
end
|
104
|
-
```
|
105
|
-
|
106
136
|
#### Register the service worker
|
107
137
|
|
108
138
|
You'll need to register the service worker with a companion script in your main page JavaScript, like `application.js`. You can use the following:
|
@@ -128,9 +158,8 @@ if (navigator.serviceWorker) {
|
|
128
158
|
You may also want to create a `manifest.json` file to make your web app installable.
|
129
159
|
|
130
160
|
```
|
131
|
-
// manifest.json
|
161
|
+
// app/assets/javascripts/manifest.json
|
132
162
|
{
|
133
|
-
"name": "My Rails App"
|
134
163
|
"name": "My Progressive Rails App",
|
135
164
|
"short_name": "Progressive",
|
136
165
|
"start_url": "/"
|
@@ -159,6 +188,16 @@ Rails.application.configure do
|
|
159
188
|
end
|
160
189
|
```
|
161
190
|
|
191
|
+
#### Precompile the assets
|
192
|
+
|
193
|
+
```ruby
|
194
|
+
# config/initializers/assets.rb
|
195
|
+
|
196
|
+
Rails.application.configure do
|
197
|
+
config.assets.precompile += %w[serviceworker.js manifest.json]
|
198
|
+
end
|
199
|
+
```
|
200
|
+
|
162
201
|
#### Test the setup
|
163
202
|
|
164
203
|
At this point, restart your Rails app and reload a page in your app in Chrome or Firefox. Using dev tools, you should be able to determine.
|
@@ -174,7 +213,30 @@ So far so good? At this point, all we've done is pre-fetched assets and added th
|
|
174
213
|
|
175
214
|
Now, we can use the service worker to intercept requests and either serve them from the cache if they exist there or fallback to the network response otherwise. In most cases, we can expect responses coming from the local cache to be much faster than those coming from the network.
|
176
215
|
|
177
|
-
|
216
|
+
```javascript
|
217
|
+
// app/assets/javascripts/serviceworker.js.erb
|
218
|
+
|
219
|
+
function onFetch(event) {
|
220
|
+
// Fetch from network, fallback to cached content, then offline.html for same-origin GET requests
|
221
|
+
var request = event.request;
|
222
|
+
|
223
|
+
if (!request.url.match(/^https?:\/\/example.com/) ) { return; }
|
224
|
+
if (request.method !== 'GET') { return; }
|
225
|
+
|
226
|
+
event.respondWith(
|
227
|
+
fetch(request). // first, the network
|
228
|
+
.catch(function fallback() {
|
229
|
+
caches.match(request).then(function(response) { // then, the cache
|
230
|
+
response || caches.match("/offline.html"); // then, /offline cache
|
231
|
+
})
|
232
|
+
})
|
233
|
+
);
|
234
|
+
|
235
|
+
// See https://jakearchibald.com/2014/offline-cookbook/#on-network-response for more examples
|
236
|
+
}
|
237
|
+
|
238
|
+
self.addEventListener('install', onInstall);
|
239
|
+
```
|
178
240
|
|
179
241
|
## Configuration
|
180
242
|
|
data/Rakefile
CHANGED
data/bin/appraisal
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
#
|
4
|
+
# This file was generated by Bundler.
|
5
|
+
#
|
6
|
+
# The application 'appraisal' is installed as part of a gem, and
|
7
|
+
# this file is here to facilitate running it.
|
8
|
+
#
|
9
|
+
|
10
|
+
require "pathname"
|
11
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
|
12
|
+
Pathname.new(__FILE__).realpath)
|
13
|
+
|
14
|
+
require "rubygems"
|
15
|
+
require "bundler/setup"
|
16
|
+
|
17
|
+
load Gem.bin_path("appraisal", "appraisal")
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "rails", "~> 3.2"
|
6
|
+
gem "test-unit", "~> 3.0"
|
7
|
+
|
8
|
+
group :development, :test do
|
9
|
+
gem "rubocop", "0.39.0"
|
10
|
+
gem "pry"
|
11
|
+
gem "pry-byebug", :platforms => [:mri]
|
12
|
+
gem "guard"
|
13
|
+
gem "guard-minitest"
|
14
|
+
end
|
15
|
+
|
16
|
+
gemspec :path => "../"
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "rails", "~> 4.2"
|
6
|
+
|
7
|
+
group :development, :test do
|
8
|
+
gem "rubocop", "0.39.0"
|
9
|
+
gem "pry"
|
10
|
+
gem "pry-byebug", :platforms => [:mri]
|
11
|
+
gem "guard"
|
12
|
+
gem "guard-minitest"
|
13
|
+
end
|
14
|
+
|
15
|
+
gemspec :path => "../"
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "rails", "~> 5.0"
|
6
|
+
|
7
|
+
group :development, :test do
|
8
|
+
gem "rubocop", "0.39.0"
|
9
|
+
gem "pry"
|
10
|
+
gem "pry-byebug", :platforms => [:mri]
|
11
|
+
gem "guard"
|
12
|
+
gem "guard-minitest"
|
13
|
+
end
|
14
|
+
|
15
|
+
gemspec :path => "../"
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require "rails/generators"
|
2
|
+
require "fileutils"
|
2
3
|
|
3
4
|
module Serviceworker
|
4
5
|
module Generators
|
@@ -7,44 +8,47 @@ module Serviceworker
|
|
7
8
|
source_root File.join(File.dirname(__FILE__), "templates")
|
8
9
|
|
9
10
|
def create_assets
|
10
|
-
template "
|
11
|
-
|
12
|
-
template "serviceworker-companion.js",
|
13
|
-
File.join(javascripts_base_dir, "serviceworker-companion.js")
|
14
|
-
template "manifest.json",
|
15
|
-
File.join(javascripts_base_dir, "manifest.json")
|
11
|
+
template "manifest.json", javascripts_dir("manifest.json.erb")
|
12
|
+
template "serviceworker.js", javascripts_dir("serviceworker.js.erb")
|
13
|
+
template "serviceworker-companion.js", javascripts_dir("serviceworker-companion.js")
|
16
14
|
end
|
17
15
|
|
18
16
|
def create_initializer
|
19
|
-
template "serviceworker.rb",
|
20
|
-
File.join(initializers_dir, "serviceworker.rb")
|
17
|
+
template "serviceworker.rb", initializers_dir("serviceworker.rb")
|
21
18
|
end
|
22
19
|
|
23
20
|
def update_application_js
|
24
21
|
ext, directive = detect_js_format
|
25
|
-
|
22
|
+
snippet = "#{directive} require serviceworker-companion\n"
|
23
|
+
append_to_file application_js_path(ext), snippet
|
26
24
|
end
|
27
25
|
|
28
26
|
def update_precompiled_assets
|
29
|
-
|
30
|
-
|
27
|
+
snippet = "Rails.configuration.assets.precompile += %w[serviceworker.js manifest.json]\n"
|
28
|
+
file_path = initializers_dir("assets.rb")
|
29
|
+
FileUtils.touch file_path
|
30
|
+
append_to_file file_path, snippet
|
31
31
|
end
|
32
32
|
|
33
33
|
def update_application_layout
|
34
|
-
|
35
|
-
|
36
|
-
|
34
|
+
snippet = %(<link rel="manifest" href="/manifest.json" />)
|
35
|
+
snippet << %(\n<meta name="apple-mobile-web-app-capable" content="yes">)
|
36
|
+
insert_into_file detect_layout, snippet, before: "</head>\n"
|
37
|
+
end
|
38
|
+
|
39
|
+
def add_offline_html
|
40
|
+
template "offline.html", public_dir("offline.html")
|
37
41
|
end
|
38
42
|
|
39
43
|
private
|
40
44
|
|
41
45
|
def application_js_path(ext)
|
42
|
-
|
46
|
+
javascripts_dir("application#{ext}")
|
43
47
|
end
|
44
48
|
|
45
49
|
def detect_js_format
|
46
|
-
%w[.
|
47
|
-
next unless File.exist?(
|
50
|
+
%w[.js .js.erb .coffee .coffee.erb .js.coffee .js.coffee.erb].each do |ext|
|
51
|
+
next unless File.exist?(javascripts_dir("application#{ext}"))
|
48
52
|
return [ext, "#="] if ext.include?(".coffee")
|
49
53
|
return [ext, "//="]
|
50
54
|
end
|
@@ -52,21 +56,29 @@ module Serviceworker
|
|
52
56
|
|
53
57
|
def detect_layout
|
54
58
|
layouts = %w[.html.erb .html.haml .html.slim].map do |ext|
|
55
|
-
|
59
|
+
layouts_dir("application#{ext}")
|
56
60
|
end
|
57
61
|
layouts.find { |layout| File.exist?(layout) }
|
58
62
|
end
|
59
63
|
|
60
|
-
def
|
61
|
-
|
64
|
+
def javascripts_dir(*paths)
|
65
|
+
join("app", "assets", "javascripts", *paths)
|
66
|
+
end
|
67
|
+
|
68
|
+
def initializers_dir(*paths)
|
69
|
+
join("config", "initializers", *paths)
|
70
|
+
end
|
71
|
+
|
72
|
+
def layouts_dir(*paths)
|
73
|
+
join("app", "views", "layouts", *paths)
|
62
74
|
end
|
63
75
|
|
64
|
-
def
|
65
|
-
|
76
|
+
def public_dir(*paths)
|
77
|
+
join("public", *paths)
|
66
78
|
end
|
67
79
|
|
68
|
-
def
|
69
|
-
File.join(
|
80
|
+
def join(*paths)
|
81
|
+
File.expand_path(File.join(*paths), destination_root)
|
70
82
|
end
|
71
83
|
end
|
72
84
|
end
|
@@ -1,63 +1,16 @@
|
|
1
|
+
<%% icon_sizes = Rails.configuration.serviceworker.icon_sizes %>
|
1
2
|
{
|
2
3
|
"name": "My Progressive Rails App",
|
3
4
|
"short_name": "Progressive",
|
4
5
|
"start_url": "/",
|
5
6
|
"icons": [
|
7
|
+
<%% icon_sizes.map { |s| "#{s}x#{s}" }.each.with_index do |dim, i| %>
|
6
8
|
{
|
7
|
-
"src": "/
|
8
|
-
"sizes": "
|
9
|
+
"src": "<%%= image_path "serviceworker-rails/heart-#{dim}.png" %>",
|
10
|
+
"sizes": "<%%= dim %>",
|
9
11
|
"type": "image/png"
|
10
|
-
},
|
11
|
-
|
12
|
-
"src": "/icons/icon48.png",
|
13
|
-
"sizes": "48x48",
|
14
|
-
"type": "image/png"
|
15
|
-
},
|
16
|
-
{
|
17
|
-
"src": "/icons/icon60.png",
|
18
|
-
"sizes": "60x60",
|
19
|
-
"type": "image/png"
|
20
|
-
},
|
21
|
-
{
|
22
|
-
"src": "/icons/icon72.png",
|
23
|
-
"sizes": "72x72",
|
24
|
-
"type": "image/png"
|
25
|
-
},
|
26
|
-
{
|
27
|
-
"src": "/icons/icon76.png",
|
28
|
-
"sizes": "76x76",
|
29
|
-
"type": "image/png"
|
30
|
-
},
|
31
|
-
{
|
32
|
-
"src": "/icons/icon96.png",
|
33
|
-
"sizes": "96x96",
|
34
|
-
"type": "image/png"
|
35
|
-
},
|
36
|
-
{
|
37
|
-
"src": "/icons/icon120.png",
|
38
|
-
"sizes": "120x120",
|
39
|
-
"type": "image/png"
|
40
|
-
},
|
41
|
-
{
|
42
|
-
"src": "/icons/icon152.png",
|
43
|
-
"sizes": "152x152",
|
44
|
-
"type": "image/png"
|
45
|
-
},
|
46
|
-
{
|
47
|
-
"src": "/icons/icon180.png",
|
48
|
-
"sizes": "180x180",
|
49
|
-
"type": "image/png"
|
50
|
-
},
|
51
|
-
{
|
52
|
-
"src": "/icons/icon192.png",
|
53
|
-
"sizes": "192x192",
|
54
|
-
"type": "image/png"
|
55
|
-
},
|
56
|
-
{
|
57
|
-
"src": "/icons/icon512.png",
|
58
|
-
"sizes": "512x512",
|
59
|
-
"type": "image/png"
|
60
|
-
}
|
12
|
+
}<%%= i == (icon_sizes.length - 1) ? '' : ',' %>
|
13
|
+
<%% end %>
|
61
14
|
],
|
62
15
|
"theme_color": "#000000",
|
63
16
|
"background_color": "#FFFFFF",
|
@@ -0,0 +1,66 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>You are not connected to the Internet</title>
|
5
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
6
|
+
<style>
|
7
|
+
body {
|
8
|
+
background-color: #EFEFEF;
|
9
|
+
color: #2E2F30;
|
10
|
+
text-align: center;
|
11
|
+
font-family: arial, sans-serif;
|
12
|
+
margin: 0;
|
13
|
+
}
|
14
|
+
|
15
|
+
div.dialog {
|
16
|
+
width: 95%;
|
17
|
+
max-width: 33em;
|
18
|
+
margin: 4em auto 0;
|
19
|
+
}
|
20
|
+
|
21
|
+
div.dialog > div {
|
22
|
+
border: 1px solid #CCC;
|
23
|
+
border-right-color: #999;
|
24
|
+
border-left-color: #999;
|
25
|
+
border-bottom-color: #BBB;
|
26
|
+
border-top: #B00100 solid 4px;
|
27
|
+
border-top-left-radius: 9px;
|
28
|
+
border-top-right-radius: 9px;
|
29
|
+
background-color: white;
|
30
|
+
padding: 7px 12% 0;
|
31
|
+
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
|
32
|
+
}
|
33
|
+
|
34
|
+
h1 {
|
35
|
+
font-size: 100%;
|
36
|
+
color: #730E15;
|
37
|
+
line-height: 1.5em;
|
38
|
+
}
|
39
|
+
|
40
|
+
div.dialog > p {
|
41
|
+
margin: 0 0 1em;
|
42
|
+
padding: 1em;
|
43
|
+
background-color: #F7F7F7;
|
44
|
+
border: 1px solid #CCC;
|
45
|
+
border-right-color: #999;
|
46
|
+
border-left-color: #999;
|
47
|
+
border-bottom-color: #999;
|
48
|
+
border-bottom-left-radius: 4px;
|
49
|
+
border-bottom-right-radius: 4px;
|
50
|
+
border-top-color: #DADADA;
|
51
|
+
color: #666;
|
52
|
+
box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
|
53
|
+
}
|
54
|
+
</style>
|
55
|
+
</head>
|
56
|
+
|
57
|
+
<body>
|
58
|
+
<!-- This file lives in public/404.html -->
|
59
|
+
<div class="dialog">
|
60
|
+
<div>
|
61
|
+
<h1>It looks like you've lost your Internet connection</h1>
|
62
|
+
<p>You may need to reconnect to Wi-Fi.</p>
|
63
|
+
</div>
|
64
|
+
</div>
|
65
|
+
</body>
|
66
|
+
</html>
|
@@ -1,14 +1,53 @@
|
|
1
|
-
function onInstall() {
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
}
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
1
|
+
// function onInstall(event) {
|
2
|
+
// console.log('[Serviceworker]', "Installing!", event);
|
3
|
+
// event.waitUntil(
|
4
|
+
// caches.open('cached-assets-v1').then(function prefill(cache) {
|
5
|
+
// return cache.addAll([
|
6
|
+
// '<%%= asset_path "application.js" %>',
|
7
|
+
// '<%%= asset_path "application.css" %>',
|
8
|
+
// '/offline.html'
|
9
|
+
// ]);
|
10
|
+
// })
|
11
|
+
// );
|
12
|
+
// }
|
13
|
+
//
|
14
|
+
// function onActivate(event) {
|
15
|
+
// console.log('[Serviceworker]', "Activating!", event);
|
16
|
+
// event.waitUntil(
|
17
|
+
// caches.keys().then(function(cacheNames) {
|
18
|
+
// return Promise.all(
|
19
|
+
// cacheNames.filter(function(cacheName) {
|
20
|
+
// // Return true if you want to remove this cache,
|
21
|
+
// // but remember that caches are shared across
|
22
|
+
// // the whole origin
|
23
|
+
// return key.indexOf('v1') !== 0;
|
24
|
+
// }).map(function(cacheName) {
|
25
|
+
// return caches.delete(cacheName);
|
26
|
+
// })
|
27
|
+
// );
|
28
|
+
// })
|
29
|
+
// );
|
30
|
+
// }
|
31
|
+
//
|
32
|
+
// function onFetch(event) {
|
33
|
+
// // Fetch from network, fallback to cached content, then offline.html for same-origin GET requests
|
34
|
+
// var request = event.request;
|
35
|
+
//
|
36
|
+
// if (!request.url.match(/^https?:\/\/example.com/) ) { return; }
|
37
|
+
// if (request.method !== 'GET') { return; }
|
38
|
+
//
|
39
|
+
// event.respondWith(
|
40
|
+
// fetch(request). // first, the network
|
41
|
+
// .catch(function fallback() {
|
42
|
+
// caches.match(request).then(function(response) { // then, the cache
|
43
|
+
// response || caches.match("/offline.html"); // then, /offline cache
|
44
|
+
// })
|
45
|
+
// })
|
46
|
+
// );
|
47
|
+
//
|
48
|
+
// // See https://jakearchibald.com/2014/offline-cookbook/#on-network-response for more examples
|
49
|
+
// }
|
50
|
+
//
|
51
|
+
// self.addEventListener('install', onInstall);
|
52
|
+
// self.addEventListener('activate', onActivate);
|
53
|
+
// self.addEventListener('fetch', onFetch);
|
@@ -1,17 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require "rails"
|
2
3
|
require "rails/railtie"
|
3
4
|
require "serviceworker"
|
4
5
|
|
5
6
|
module ServiceWorker
|
6
|
-
class
|
7
|
+
class Engine < ::Rails::Engine
|
7
8
|
config.serviceworker = ActiveSupport::OrderedOptions.new
|
8
9
|
config.serviceworker.headers = {}
|
9
10
|
config.serviceworker.routes = ServiceWorker::Router.new
|
10
11
|
config.serviceworker.handler = ServiceWorker::Rails::Handler.new
|
12
|
+
config.serviceworker.icon_sizes = %w[36 48 60 72 76 96 120 152 180 192 512]
|
11
13
|
|
12
14
|
initializer "serviceworker-rails.configure_rails_initialization", after: :load_config_initializers do
|
13
15
|
config.serviceworker.logger ||= ::Rails.logger
|
14
16
|
|
17
|
+
if config.respond_to?(:assets)
|
18
|
+
config.assets.precompile += %w[serviceworker-rails/*.png]
|
19
|
+
elsif app.config.respond_to?(:assets)
|
20
|
+
app.config.assets.precompile += %w[serviceworker-rails/*.png]
|
21
|
+
end
|
22
|
+
|
15
23
|
app.middleware.use ServiceWorker::Middleware, config.serviceworker
|
16
24
|
end
|
17
25
|
|
data/lib/serviceworker/rails.rb
CHANGED
data/lib/serviceworker/route.rb
CHANGED
data/serviceworker-rails.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: serviceworker-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ross Kaffenberger
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-11-
|
11
|
+
date: 2016-11-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -108,6 +108,20 @@ dependencies:
|
|
108
108
|
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: appraisal
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 2.1.0
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 2.1.0
|
111
125
|
description: Integrates ServiceWorker into the Rails asset pipeline.
|
112
126
|
email:
|
113
127
|
- rosskaff@gmail.com
|
@@ -118,6 +132,7 @@ files:
|
|
118
132
|
- ".gitignore"
|
119
133
|
- ".rubocop.yml"
|
120
134
|
- ".travis.yml"
|
135
|
+
- Appraisals
|
121
136
|
- CHANGELOG.md
|
122
137
|
- CODE_OF_CONDUCT.md
|
123
138
|
- Gemfile
|
@@ -126,25 +141,43 @@ files:
|
|
126
141
|
- README.md
|
127
142
|
- Rakefile
|
128
143
|
- bin/_guard-core
|
144
|
+
- bin/appraisal
|
129
145
|
- bin/console
|
130
146
|
- bin/guard
|
131
147
|
- bin/rake
|
132
148
|
- bin/rubocop
|
133
149
|
- bin/setup
|
150
|
+
- gemfiles/rails_3.gemfile
|
151
|
+
- gemfiles/rails_4.gemfile
|
152
|
+
- gemfiles/rails_5.gemfile
|
153
|
+
- lib/assets/images/convert.rb
|
154
|
+
- lib/assets/images/serviceworker-rails/heart-1200x1200.png
|
155
|
+
- lib/assets/images/serviceworker-rails/heart-120x120.png
|
156
|
+
- lib/assets/images/serviceworker-rails/heart-152x152.png
|
157
|
+
- lib/assets/images/serviceworker-rails/heart-180x180.png
|
158
|
+
- lib/assets/images/serviceworker-rails/heart-192x192.png
|
159
|
+
- lib/assets/images/serviceworker-rails/heart-36x36.png
|
160
|
+
- lib/assets/images/serviceworker-rails/heart-48x48.png
|
161
|
+
- lib/assets/images/serviceworker-rails/heart-512x512.png
|
162
|
+
- lib/assets/images/serviceworker-rails/heart-60x60.png
|
163
|
+
- lib/assets/images/serviceworker-rails/heart-72x72.png
|
164
|
+
- lib/assets/images/serviceworker-rails/heart-76x76.png
|
165
|
+
- lib/assets/images/serviceworker-rails/heart-96x96.png
|
134
166
|
- lib/generators/serviceworker/install_generator.rb
|
135
167
|
- lib/generators/serviceworker/templates/manifest.json
|
168
|
+
- lib/generators/serviceworker/templates/offline.html
|
136
169
|
- lib/generators/serviceworker/templates/serviceworker-companion.js
|
137
170
|
- lib/generators/serviceworker/templates/serviceworker.js
|
138
171
|
- lib/generators/serviceworker/templates/serviceworker.rb
|
139
172
|
- lib/service_worker.rb
|
140
173
|
- lib/serviceworker-rails.rb
|
141
174
|
- lib/serviceworker.rb
|
175
|
+
- lib/serviceworker/engine.rb
|
142
176
|
- lib/serviceworker/handler.rb
|
143
177
|
- lib/serviceworker/middleware.rb
|
144
178
|
- lib/serviceworker/rails.rb
|
145
179
|
- lib/serviceworker/rails/handler.rb
|
146
180
|
- lib/serviceworker/rails/version.rb
|
147
|
-
- lib/serviceworker/railtie.rb
|
148
181
|
- lib/serviceworker/route.rb
|
149
182
|
- lib/serviceworker/router.rb
|
150
183
|
- serviceworker-rails.gemspec
|
@@ -168,7 +201,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
168
201
|
version: '0'
|
169
202
|
requirements: []
|
170
203
|
rubyforge_project:
|
171
|
-
rubygems_version: 2.
|
204
|
+
rubygems_version: 2.5.2
|
172
205
|
signing_key:
|
173
206
|
specification_version: 4
|
174
207
|
summary: ServiceWorker for Rails 3+
|