pwa 1.2.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7fe07b15c94d9ce07eeeb9d04c77bf04a73a5d70923d176da438b5a096b27e70
4
- data.tar.gz: 2945a4cc6a2326e3386acd27e486530a71a33824820e896e6145b7127d5a115d
3
+ metadata.gz: e4b16d74767980ee9dee6f68fe6028b9f155e160cca22043e1a2d3772c6158cd
4
+ data.tar.gz: b336abd47c05fb18c8bbae000d0704b8faa932034073c4726a04c4d5b17073fa
5
5
  SHA512:
6
- metadata.gz: 72aeb998d4323c29359152efb66b8481ebb815b34f5a121d41e5fea916f4897a5c4a43e578b94d145011c7c7bc2cf2131107323f3ecb7300b813dbf668671841
7
- data.tar.gz: 0ca785f4e2cba1ac96b79f1145aba452772b9efa7a146a13ac80c7b76d25baaa0c6ad2dd3d564e2c41b2e23eefc183f691940a1304aa4327f2f8a13ac01c2865
6
+ metadata.gz: b56b33826665907804b226bb31aa4ae442b41eaa6145ac9304044550ea46fda5ec5a50ed93238db27cbf817818cda540559717cf1c1c4e4467d778bee78d39b1
7
+ data.tar.gz: c25377415c2bdff4f98e315b98f1f126eb3101606233bc4e610b9f7bdbd3d32842612a1190053bf87e3ca8ca608b390ffa8a16142ae0ac62333c182a717baa63
data/CHANGELOG.md CHANGED
@@ -4,6 +4,11 @@
4
4
 
5
5
  * nothing yet
6
6
 
7
+ ### 2.0.0 - 2018/01/26
8
+
9
+ * features
10
+ * introduced `App` class to support multiple PWAs per Rails app
11
+
7
12
  ### 1.2.0 - 2018/01/19
8
13
 
9
14
  * features
data/README.md CHANGED
@@ -23,9 +23,13 @@ If a PWA is not enough and you want to bring your Web App into the store - check
23
23
 
24
24
  * [Installation](#installation)
25
25
  * [Usage](#usage)
26
- * [Manifest](#manifest)
26
+ * [Quick start](#quick-start)
27
+ * [Apps](#apps)
28
+ * [Manifest](#manifest)
29
+ * [Offline pages](#offline-pages)
27
30
  * [Service worker](#service-worker)
28
31
  * [Views](#views)
32
+ * [Configuration](#configuration)
29
33
  * [To Do](#to-do)
30
34
  * [Contributing](#contributing)
31
35
  * [Contributors](#contributors)
@@ -56,9 +60,24 @@ If you always want to be up to date fetch the latest from GitHub in your `Gemfil
56
60
  gem 'pwa', github: 'jonhue/pwa'
57
61
  ```
58
62
 
59
- Now run the generator:
63
+ ---
64
+
65
+ ## Usage
66
+
67
+ ### Quick start
60
68
 
61
- $ rails g pwa
69
+ Run the generators:
70
+
71
+ $ rails g pwa:install
72
+ $ rails g pwa:app -n "App"
73
+
74
+ Noew define your app:
75
+
76
+ ```ruby
77
+ Pwa.configure do |config|
78
+ config.define_app 'App'
79
+ end
80
+ ```
62
81
 
63
82
  Add the following to the `head` tag of your layout file:
64
83
 
@@ -75,18 +94,31 @@ Make sure to add the required javascript in `app/assets/javascripts/application.
75
94
  Lastly, go to your routes file (`config/routes.rb`) and mount the `Pwa::Engine` class:
76
95
 
77
96
  ```ruby
78
- mount Pwa::Engine, at: '/pwa'
97
+ mount Pwa::Engine, at: ''
79
98
  ```
80
99
 
81
- **Note:** The path `Pwa::Engine` gets mounted at, is currently required to be `pwa`.
100
+ **Note:** The path `Pwa::Engine` is being mounted at, is currently required to be `''`.
82
101
 
83
- ---
102
+ ### Apps
84
103
 
85
- ## Usage
104
+ Progressive Web Apps for Rails allows for multiple Progressive Web Apps per Rails app:
86
105
 
87
- ### Manifest
106
+ ```ruby
107
+ Pwa.configure do |config|
108
+ config.define_app 'Example', ['example.com', 'localhost:3000', 'lvh.me:3000']
109
+ config.define_app 'Subdomain', ['subdomain.example.com', 'subdomain.lvh.me:3000']
110
+ end
111
+ ```
88
112
 
89
- The generator generates a `manifest.json` file located in the `public` directory. You can customize it to your liking.
113
+ **Note:** You can omit the array of URL scopes if you have just one PWA.
114
+
115
+ #### Manifest
116
+
117
+ The app generator generates a manifest file located in the `app/views/pwa/apps/manifests` directory. It is accessible through `/manifest.json`. You can customize it to your liking.
118
+
119
+ #### Offline pages
120
+
121
+ Progressive Web Apps for Rails automatically stores a copy of the offline page (`app/views/pwa/apps/offline/_app.html.erb`) in the users cache, so your app is accessible at any time, even if requested URLs have not been cached yet.
90
122
 
91
123
  ### Service worker
92
124
 
@@ -114,6 +146,20 @@ To detect whether or not your app is currently being used as a Progressive Web A
114
146
 
115
147
  ---
116
148
 
149
+ ## Configuration
150
+
151
+ You can configure Pwa by passing a block to `configure`. This can be done in `config/initializers/pwa.rb`:
152
+
153
+ ```ruby
154
+ Pwa.configure do |config|
155
+ config.define_app 'App', ['example.com']
156
+ end
157
+ ```
158
+
159
+ * `define_app` Define apps with a name and URL scopes.
160
+
161
+ ---
162
+
117
163
  ## To Do
118
164
 
119
165
  [Here](https://github.com/jonhue/pwa/projects/1) is the full list of current projects.
@@ -0,0 +1,19 @@
1
+ module Pwa
2
+ class AppsController < ApplicationController
3
+
4
+ before_action :get_app
5
+
6
+ def manifest
7
+ end
8
+
9
+ def offline
10
+ end
11
+
12
+ private
13
+
14
+ def get_app
15
+ @app = ::Pwa::App.find_by_url(request.original_url)[0]
16
+ end
17
+
18
+ end
19
+ end
@@ -1,7 +1,7 @@
1
1
  module PwaHelper
2
2
 
3
3
  def pwa_manifest
4
- '<link rel="manifest" href="/manifest.json" />'.html_safe
4
+ "<link rel='manifest' href='manifest.json' />".html_safe
5
5
  end
6
6
 
7
7
  end
@@ -0,0 +1 @@
1
+ <%= render "pwa/apps/manifests/#{@app.safe_name}" %>
@@ -0,0 +1 @@
1
+ <%= render "pwa/apps/offline/#{@app.safe_name}" %>
data/config/routes.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  Pwa::Engine.routes.draw do
2
2
 
3
- get 'offline', to: 'pwa/offline#index'
3
+ get 'offline', to: 'pwa/apps#offline'
4
+ get 'manifest', to: 'pwa/apps#manifest', defaults: { format: :json }
4
5
 
5
6
  end
@@ -0,0 +1,21 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module Pwa
5
+ class AppGenerator < Rails::Generators::Base
6
+
7
+ include Rails::Generators::Migration
8
+
9
+ source_root File.join File.dirname(__FILE__), '../templates/app'
10
+ desc 'Generate a Progressive Web Apps for Rails app'
11
+
12
+ class_option :name, desc: 'App name', type: :string, default: 'index', aliases: '-n'
13
+
14
+ def create_files
15
+ @name = options[:name].parameterize.underscore
16
+ template 'manifest.json.erb', "app/views/pwa/apps/manifests/_#{@name}.json"
17
+ template 'offline.html.erb', "app/views/pwa/apps/offline/_#{@name}.html.erb"
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module Pwa
5
+ class InstallGenerator < Rails::Generators::Base
6
+
7
+ include Rails::Generators::Migration
8
+
9
+ source_root File.join File.dirname(__FILE__), '../templates/install'
10
+ desc 'Install Progressive Web Apps for Rails'
11
+
12
+ def create_initializer
13
+ template 'initializer.rb', 'config/initializers/pwa.rb'
14
+ end
15
+
16
+ def create_files
17
+ template 'service-worker.js', 'public/pwa-sw.js'
18
+ end
19
+
20
+ end
21
+ end
@@ -1,6 +1,6 @@
1
1
  {
2
- "short_name": "",
3
- "name": "",
2
+ "short_name": "<%= options[:name].titleize %>",
3
+ "name": "<%= options[:name].titleize %>",
4
4
  "start_url": "/",
5
5
  "scope": "/",
6
6
  "display": "standalone",
@@ -0,0 +1,7 @@
1
+ Pwa.configure do |config|
2
+
3
+ # Define Progressive Web Apps for Rails apps
4
+ # config.define_app 'Example', ['example.com', 'localhost:3000', 'lvh.me:3000']
5
+ # config.define_app 'Subdomain', ['subdomain.example.com', 'subdomain.lvh.me:3000']
6
+
7
+ end
@@ -6,7 +6,7 @@ var preLoad = function() {
6
6
  console.log('[PWA] Install Event processing');
7
7
  return caches.open('pwa-offline').then(function(cache) {
8
8
  console.log('[PWA] Cached index and offline page during Install');
9
- return cache.addAll(['/pwa/offline']);
9
+ return cache.addAll(['/offline']);
10
10
  });
11
11
  }
12
12
 
@@ -43,7 +43,7 @@ var returnFromCache = function(request) {
43
43
  return caches.open('pwa-offline').then(function(cache) {
44
44
  return cache.match(request).then(function(matching) {
45
45
  if( !matching || matching.status == 404 ) {
46
- return cache.match('pwa/offline');
46
+ return cache.match('offline');
47
47
  } else {
48
48
  return matching;
49
49
  };
data/lib/pwa.rb CHANGED
@@ -2,6 +2,10 @@ require 'pwa/version'
2
2
 
3
3
  module Pwa
4
4
 
5
+ require 'pwa/configuration'
6
+
7
+ require 'pwa/app'
8
+
5
9
  require 'pwa/engine'
6
10
 
7
11
  end
data/lib/pwa/app.rb ADDED
@@ -0,0 +1,25 @@
1
+ module Pwa
2
+ class App
3
+
4
+ attr_accessor :name
5
+ attr_accessor :scopes
6
+
7
+ def initialize name, scopes = nil
8
+ @name = name
9
+ @scopes = scopes
10
+ end
11
+
12
+ def safe_name
13
+ self.name.parameterize.underscore
14
+ end
15
+
16
+ def self.find_by_name name
17
+ Pwa.configuration.apps.select { |app| app.name == name }
18
+ end
19
+
20
+ def self.find_by_url url
21
+ Pwa.configuration.apps.select { |app| app.scopes.nil? || app.scopes.any? { |scope| url.include?(scope) } }
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+ module Pwa
2
+
3
+ class << self
4
+ attr_accessor :configuration
5
+ end
6
+
7
+ def self.configure
8
+ self.configuration ||= Configuration.new
9
+ yield configuration
10
+ end
11
+
12
+ class Configuration
13
+
14
+ attr_accessor :apps
15
+
16
+ def initialize
17
+ @apps = []
18
+ end
19
+
20
+ def define_app name, scope
21
+ self.apps << ::Pwa::App.new(name, scope)
22
+ end
23
+
24
+ end
25
+
26
+ end
data/lib/pwa/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Pwa
2
2
 
3
- VERSION = '1.2.0'
3
+ VERSION = '2.0.0'
4
4
 
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pwa
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Hübotter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-19 00:00:00.000000000 Z
11
+ date: 2018-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -61,14 +61,20 @@ files:
61
61
  - CHANGELOG.md
62
62
  - LICENSE
63
63
  - README.md
64
- - app/controllers/pwa/offline_controller.rb
64
+ - app/controllers/pwa/apps_controller.rb
65
65
  - app/helpers/pwa_helper.rb
66
+ - app/views/pwa/apps/manifest.json.erb
67
+ - app/views/pwa/apps/offline.html.erb
66
68
  - config/routes.rb
67
- - lib/generators/pwa_generator.rb
68
- - lib/generators/templates/manifest.json
69
- - lib/generators/templates/service_worker.js
70
- - lib/generators/templates/view.html.erb
69
+ - lib/generators/pwa/app_generator.rb
70
+ - lib/generators/pwa/install_generator.rb
71
+ - lib/generators/templates/app/manifest.json.erb
72
+ - lib/generators/templates/app/offline.html.erb
73
+ - lib/generators/templates/install/initializer.rb
74
+ - lib/generators/templates/install/service-worker.js
71
75
  - lib/pwa.rb
76
+ - lib/pwa/app.rb
77
+ - lib/pwa/configuration.rb
72
78
  - lib/pwa/engine.rb
73
79
  - lib/pwa/version.rb
74
80
  - vendor/assets/javascripts/pwa.js
@@ -82,10 +88,10 @@ post_install_message: |
82
88
 
83
89
  There are two more steps to take:
84
90
 
85
- 1) Run `rails g pwa`
91
+ 1) Run `rails g pwa:install` and `rails g pwa:install -n "Application Name"`
86
92
  2) Mount engine in `config/routes.rb`:
87
93
 
88
- mount Pwa::Engine, at: '/pwa'
94
+ mount Pwa::Engine, at: ''
89
95
 
90
96
 
91
97
  Learn more at https://github.com/jonhue/pwa
@@ -1,8 +0,0 @@
1
- module Pwa
2
- class OfflineController < ApplicationController
3
-
4
- def index
5
- end
6
-
7
- end
8
- end
@@ -1,23 +0,0 @@
1
- require 'rails/generators'
2
- require 'rails/generators/migration'
3
-
4
- class PwaGenerator < Rails::Generators::Base
5
-
6
- include Rails::Generators::Migration
7
-
8
- source_root File.join File.dirname(__FILE__), 'templates'
9
- desc 'Install Progressive Web Apps for Rails'
10
-
11
- def create_view
12
- template 'view.html.erb', 'app/views/pwa/offline/index.html.erb'
13
- end
14
-
15
- def create_manifest
16
- template 'manifest.json', 'public/manifest.json'
17
- end
18
-
19
- def create_service_worker
20
- template 'service_worker.js', 'public/pwa-sw.js'
21
- end
22
-
23
- end