-
Notifications
You must be signed in to change notification settings - Fork 2
/
pwa.js
100 lines (89 loc) · 2.58 KB
/
pwa.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { getSettings } from 'meteor/quave:settings';
import { Meteor } from 'meteor/meteor';
import { WebApp } from 'meteor/webapp';
const PACKAGE_NAME = 'quave:pwa';
const settings = getSettings({ packageName: PACKAGE_NAME });
export const MANIFEST_PATH = settings.manifestPath || '/pwa.json';
export const getGoolePlayAppUrl = ({ googlePlayAppId }) => {
if (!googlePlayAppId) {
return null;
}
return `https://play.google.com/store/apps/details?id=${googlePlayAppId}`;
};
export const getAppleItunesAppUrl = ({ appleItunesAppId }) => {
if (!appleItunesAppId) {
return null;
}
return `https://itunes.apple.com/app/id${appleItunesAppId}`;
};
export const getPwaSettings = (data = {}) => {
const {
appleItunesAppId,
googlePlayAppId,
gcmSenderId,
backgroundColor = 'white',
themeColor,
startUrl = '/',
display = 'standalone',
orientation = 'portrait',
lang = 'en-US',
name,
shortName = name,
description = name,
icons = [],
preferRelatedApplications,
} = data;
return {
background_color: backgroundColor,
theme_color: themeColor,
start_url: startUrl,
display,
orientation,
lang,
name,
short_name: shortName,
description,
icons: icons.filter((icon) => !!icon.src),
gcm_sender_id: gcmSenderId,
prefer_related_applications:
preferRelatedApplications == null
? !!(googlePlayAppId || appleItunesAppId)
: preferRelatedApplications,
related_applications: [
googlePlayAppId && {
platform: 'play',
url: getGoolePlayAppUrl(data),
id: googlePlayAppId,
},
appleItunesAppId && {
platform: 'itunes',
url: getAppleItunesAppUrl(data),
id: appleItunesAppId,
},
].filter(Boolean),
};
};
export const respondPwaManifest = (req, res, customSettings = {}) => {
const data = { ...settings, ...customSettings };
if (!data.name) {
res.setHeader('Content-Type', 'text/html');
res.writeHead(405);
res.end(
`<h1>${PACKAGE_NAME} PWA not configured, add to your settings.packages.[quave:pwa] the desired PWA keys</h1>`
);
return;
}
res.setHeader('Content-Type', 'javascript/json');
res.writeHead(200);
res.end(JSON.stringify(getPwaSettings(data)));
};
export const createResponderPwaManifest = (customSettings) => (req, res) =>
respondPwaManifest(req, res, customSettings);
export const registerPwaManifestHandler = (customSettings) => {
WebApp.connectHandlers.use(
MANIFEST_PATH,
Meteor.bindEnvironment((req, res) =>
respondPwaManifest(req, res, customSettings)
)
);
};