rails-pwa 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +35 -5
- data/lib/rails/pwa/railtie.rb +1 -1
- data/lib/rails/pwa/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3244af30921cbd3649866dc9920f6ac7cc6928e2752e56dc8198c22ecb747c94
|
4
|
+
data.tar.gz: 9f124aeb326d6b8d70f2a981ad7e95101645c5727d0582a34f372f2f5889fbd8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 98868d94a06273931e040cf078fc241e117b383c8f0fdfd799e445a98340fdd603fb4658ba3495715f91812e73f12f872b5e904cece36f97991fd072a2c95bd4
|
7
|
+
data.tar.gz: c8ff81cc8c89efe14adcb1fc90b95741fdbcd48ef2a0dc7b6e16d7f2a2b86fcf687cd3654686a3262452b4d4ca73c983a1802a93aa7c9c52586c7033a2b9247b
|
data/README.md
CHANGED
@@ -1,8 +1,34 @@
|
|
1
|
-
# Rails::
|
2
|
-
|
1
|
+
# Rails::PWA
|
2
|
+
A simple hack to serve service worker scripts from rails apps.
|
3
|
+
|
4
|
+
_TLDR_: Serves the webpack defined at `app/javascripts/packs/worker.js` from `/worker.js`
|
5
|
+
|
6
|
+
Service workers can only intercept requests in the directory and subdirectory they are served from. Rails serves webpacked javascript from paths similar to `/packs/js/application-abcd1234.js`. Any service worker served from a path like this would only be able to operate on paths under `/packs/js/`; generally not what you want.
|
7
|
+
|
8
|
+
This gem lets you create a javascript pack for a service worker, and serve it from `/worker.js`.
|
3
9
|
|
4
10
|
## Usage
|
5
|
-
|
11
|
+
Install the gem (see full instructions below).
|
12
|
+
|
13
|
+
Restart your server (so the gem's middleware can be added to the app).
|
14
|
+
|
15
|
+
Add a service worker pack by creating a file `app/javascript/packs/worker.js`. Here's a simple example to get started:
|
16
|
+
|
17
|
+
```js
|
18
|
+
for (const name of ["install", "activate", "fetch"]) {
|
19
|
+
self.addEventListener(name, event => console.log(event))
|
20
|
+
}
|
21
|
+
```
|
22
|
+
|
23
|
+
You also need to register your worker, adding something like this to `app/javascript/packs/application.js`:
|
24
|
+
|
25
|
+
```
|
26
|
+
if ("serviceWorker" in navigator) {
|
27
|
+
window.addEventListener("load", function () {
|
28
|
+
navigator.serviceWorker.register("/worker.js")
|
29
|
+
})
|
30
|
+
}
|
31
|
+
```
|
6
32
|
|
7
33
|
## Installation
|
8
34
|
Add this line to your application's Gemfile:
|
@@ -21,8 +47,12 @@ Or install it yourself as:
|
|
21
47
|
$ gem install rails-pwa
|
22
48
|
```
|
23
49
|
|
24
|
-
##
|
25
|
-
|
50
|
+
## How it works
|
51
|
+
|
52
|
+
This gem installs a very simple piece of `Rack::Middleware` into your app, which intercepts all requests to `/worker.js` and either:
|
53
|
+
|
54
|
+
* In development: Rewrites and forwards the request to the rails app (where the usual webpacker magic will occure)
|
55
|
+
* In other environments: Serves the precompiled `worker.js` pack directly from `/public/packs/js/`. N.B. You may want to use some other form of url rewriting in production, depending on how your app is deployed.
|
26
56
|
|
27
57
|
## License
|
28
58
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/rails/pwa/railtie.rb
CHANGED
@@ -10,7 +10,7 @@ class Rails::PWA::Railtie < ::Rails::Railtie
|
|
10
10
|
scripts = app.config.pwa.scripts
|
11
11
|
root = Rails.env.development? ? false : Rails.public_path
|
12
12
|
|
13
|
-
app.config.
|
13
|
+
app.config.middleware.insert_before Rack::Sendfile, Rails::PWA::Interceptor,
|
14
14
|
scripts: app.config.pwa.scripts,
|
15
15
|
root: root
|
16
16
|
end
|
data/lib/rails/pwa/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-pwa
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom Ward
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 6.0.0
|
19
|
+
version: 6.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 6.0.0
|
26
|
+
version: 6.0.0
|
27
27
|
description: Serves service worker scripts
|
28
28
|
email:
|
29
29
|
- tom@popdog.net
|