Skip to content

Commit 5372677

Browse files
collimarcorossta
andcommitted
Add alternative method to pass the VAPID public key to JavaScript (closes #11)
Co-authored-by: rossta <[email protected]>
1 parent f140f28 commit 5372677

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

README.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,16 @@ navigator.serviceWorker.register('/service-worker.js')
5555

5656
### Subscribing to push notifications
5757

58-
The VAPID public key you generated earlier is made available to the client as a `UInt8Array`. To do this, one way would be to expose the urlsafe-decoded bytes from Ruby to JavaScript when rendering the HTML template.
58+
The VAPID public key that you generated earlier needs to be made available to JavaScript, which can be done in many ways, for example with a fetch request or when rendering the HTML template in Ruby:
5959

60-
```javascript
61-
window.vapidPublicKey = new Uint8Array(<%= Base64.urlsafe_decode64(ENV['VAPID_PUBLIC_KEY']).bytes %>);
60+
```erb
61+
<script>
62+
// Make the VAPID public key available to the client as a Uint8Array
63+
window.vapidPublicKey = new Uint8Array(<%= Base64.urlsafe_decode64(ENV['VAPID_PUBLIC_KEY']).bytes %>)
64+
65+
// Or you can pass it as a string if you prefer
66+
window.vapidPublicKey = "<%= ENV['VAPID_PUBLIC_KEY'].delete('=') %>"
67+
</script>
6268
```
6369

6470
Your JavaScript code uses the `pushManager` interface to subscribe to push notifications, passing the VAPID public key to the subscription settings.

0 commit comments

Comments
 (0)