File size: 2,456 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import querystring from 'querystring';
import config from '@automattic/calypso-config';

const getWordPressOptions = ( environmentUrlSuffix ) => ( {
	gcm_sender_id: '87234302238',
	icons: [
		{
			src: '/calypso/images/manifest/icon-144x144.png' + environmentUrlSuffix,
			sizes: '144x144',
			type: 'image/png',
		},
		{
			src: '/calypso/images/manifest/icon-192x192.png' + environmentUrlSuffix,
			sizes: '192x192',
			type: 'image/png',
		},
		{
			src: '/calypso/images/manifest/icon-512x512.png' + environmentUrlSuffix,
			sizes: '512x512',
			type: 'image/png',
		},
	],
	related_applications: [
		{
			platform: 'play',
			url: 'https://play.google.com/store/apps/details?id=org.wordpress.android',
		},
		{
			platform: 'itunes',
			url: 'https://itunes.apple.com/app/wordpress/id335703880',
		},
	],
} );

const getJetpackCloudOptions = ( environmentUrlSuffix ) => ( {
	icons: [
		{
			src: '/calypso/images/manifest/jetpack-icon-144x144.png' + environmentUrlSuffix,
			sizes: '144x144',
			type: 'image/png',
		},
		{
			src: '/calypso/images/manifest/jetpack-icon-192x192.png' + environmentUrlSuffix,
			sizes: '192x192',
			type: 'image/png',
		},
		{
			src: '/calypso/images/manifest/jetpack-icon-512x512.png' + environmentUrlSuffix,
			sizes: '512x512',
			type: 'image/png',
		},
	],
} );

/**
 * TODO:
 * - l10n
 * @returns {Object} An express app that returns /manifest.json
 */
const buildManifest = ( { branchName } ) => {
	// These options exist to make sure that manifest-linked URLs load correctly even if
	// cookies haven't been set yet in the calypso.live environment.
	// If we find a way to make calypso.live set its cookies before the manifest is loaded,
	// then this can be safely removed.
	const environmentUrlOptions = { source: 'pwa' };

	if ( branchName && 'trunk' !== branchName ) {
		environmentUrlOptions.branch = branchName;
	}

	const environmentUrlSuffix = '?' + querystring.stringify( environmentUrlOptions );

	return {
		display: 'standalone',
		name: config( 'site_name' ),
		short_name: config( 'site_name' ),
		start_url: '/' + environmentUrlSuffix,
		background_color: config( 'theme_color' ),
		theme_color: config( 'theme_color' ),
		...( config.isEnabled( 'jetpack-cloud' )
			? getJetpackCloudOptions( environmentUrlSuffix )
			: getWordPressOptions( environmentUrlSuffix ) ),
	};
};

export default ( request, response ) => {
	response.json( buildManifest( { branchName: request.query.branch } ) );
};