Skip to content

Commit bb5e279

Browse files
authored
Add default pwa manifest and service worker file (rails#50528)
* Add default pwa manifest and service worker file * Update CHANGELOG.md
1 parent 1ea796d commit bb5e279

File tree

7 files changed

+74
-0
lines changed

7 files changed

+74
-0
lines changed

railties/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Add default PWA files for manifest and service-worker that are served from `app/views/pwa` and can be dynamically rendered through erb. Mount these files explicitly at the root with default routes in the generated routes file.
2+
3+
*DHH*
4+
15
* Updated system tests to now use headless Chrome by default for the new applications.
26

37
*DHH*

railties/lib/rails.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module Rails
3131
autoload :Info
3232
autoload :InfoController
3333
autoload :MailersController
34+
autoload :PwaController
3435
autoload :WelcomeController
3536

3637
class << self

railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
<head>
44
<title><%%= content_for(:title) || "<%= camelized %>" %></title>
55
<meta name="viewport" content="width=device-width,initial-scale=1">
6+
<meta name="apple-mobile-web-app-capable" content="yes">
67
<%%= csrf_meta_tags %>
78
<%%= csp_meta_tag %>
89

910
<%%= yield :head %>
1011

12+
<link rel="manifest" href="/manifest.json">
1113
<link rel="icon" href="/icon.png" type="image/png">
1214
<link rel="apple-touch-icon" href="/icon.png" sizes="512x512">
1315
<%- if options[:skip_hotwire] || options[:skip_javascript] -%>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "<%= camelized %>",
3+
"icons": [
4+
{
5+
"src": "/icon.png",
6+
"type": "image/png",
7+
"sizes": "512x512"
8+
},
9+
{
10+
"src": "/icon.png",
11+
"type": "image/png",
12+
"sizes": "512x512",
13+
"purpose": "maskable"
14+
}
15+
],
16+
"start_url": "/",
17+
"display": "standalone",
18+
"scope": "/",
19+
"description": "<%= camelized %>.",
20+
"theme_color": "red",
21+
"background_color": "red"
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Add a service worker for processing Web Push notifications:
2+
//
3+
// self.addEventListener("push", async (event) => {
4+
// const { title, options } = await event.data.json()
5+
// event.waitUntil(self.registration.showNotification(title, options))
6+
// })
7+
//
8+
// self.addEventListener("notificationclick", function(event) {
9+
// event.notification.close()
10+
// event.waitUntil(
11+
// clients.matchAll({ type: "window" }).then((clientList) => {
12+
// for (let i = 0; i < clientList.length; i++) {
13+
// let client = clientList[i]
14+
// let clientPath = (new URL(client.url)).pathname
15+
//
16+
// if (clientPath == event.notification.data.path && "focus" in client) {
17+
// return client.focus()
18+
// }
19+
// }
20+
//
21+
// if (clients.openWindow) {
22+
// return clients.openWindow(event.notification.data.path)
23+
// }
24+
// })
25+
// )
26+
// })

railties/lib/rails/generators/rails/app/templates/config/routes.rb.tt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Rails.application.routes.draw do
55
# Can be used by load balancers and uptime monitors to verify that the app is live.
66
get "up" => "rails/health#show", as: :rails_health_check
77

8+
# Render dynamic PWA files from app/views/pwa/*
9+
get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker
10+
get "manifest" => "rails/pwa#manifest", as: :pwa_manifest
11+
812
# Defines the root path route ("/")
913
# root "posts#index"
1014
end

railties/lib/rails/pwa_controller.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
require "rails/application_controller"
4+
5+
class Rails::PwaController < Rails::ApplicationController # :nodoc:
6+
skip_forgery_protection
7+
8+
def service_worker
9+
render template: "pwa/service-worker", layout: false
10+
end
11+
12+
def manifest
13+
render template: "pwa/manifest", layout: false
14+
end
15+
end

0 commit comments

Comments
 (0)