-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwa.api.php
115 lines (106 loc) · 3.52 KB
/
pwa.api.php
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
use Drupal\node\Entity\Node;
/**
* @file
* Hooks provided by the Progressive Web App module.
*/
use Drupal\Core\Cache\CacheableMetadata;
/**
* @addtogroup hooks
* @{
*/
/**
* Alters cached urls list for the service worker file.
*
* This hook allows adding or removing urls to the whitelist for the cache urls.
* This is the same as the "URLs to cache on install" list on the service worker
* admin page.
*
* After you make your modifications you do NOT need to return the results.
* Since $cacheUrls is passed by reference, any changes made to $cacheUrls are
* automatically registered.
*
* @param array &$cacheUrls
* List of urls to cache.
* @param \Drupal\Core\Cache\CacheableMetadata $cacheableMetadata
* Cacheability metadata.
*/
function hook_pwa_cache_urls_alter(&$cacheUrls, CacheableMetadata &$cacheableMetadata) {
// Get a node URL and its cacheability metadata.
$generated_url = Node::load('1')->toUrl()->toString(TRUE);
// Add the URL to the list.
$cacheUrls[] = $generated_url->getGeneratedUrl();
// Merge the cacheability metadata.
$cacheableMetadata = $cacheableMetadata->merge($generated_url);
}
/**
* Alters excluded urls list for the service worker file.
*
* This hook allows adding or removing urls to the blacklist for the excluded
* urls. This is the same as the "URLs to cache on install" list on the service
* worker admin page.
*
* After you make your modifications you do NOT need to return the results.
* Since $excludeUrls is passed by reference, any changes made to $excludeUrls
* are automatically registered.
*
* @param array &$excludeUrls
* List of url to cache. Paths matching these patterns will not be cached by
* the Service Worker. Can be JavaScript regex.
* @param \Drupal\Core\Cache\CacheableMetadata $cacheableMetadata
* Cacheability metadata.
*/
function hook_pwa_exclude_urls_alter(&$excludeUrls, CacheableMetadata &$cacheableMetadata) {
$excludeUrls[] = "path/pattern/.*";
$excludeUrls[] = "just/path";
}
/**
* Alters cached assets urls list for the service worker file.
*
* This hook allows altering the list of asset URLs to cache on install.
*
* @param array &$resources
* List of asset URLs to cache.
*
* @see hook_pwa_cache_urls_assets_page_alter()
*/
function hook_pwa_cache_urls_assets_alter(&$resources) {
$module_path = \Drupal::service('extension.list.module')->getPath('pwa');
$resources[] = "/$module_path/assets/loading.gif";
}
/**
* Alters cached assets urls list for each page for the service worker file.
*
* This hook allows altering the list of asset URLs to cache on install. This
* hook is called once for every page to be cached.
*
* @param array &$resources
* List of asset URLs to cache for the given page.
* @param string $page
* The relative URL of the page that resources are currently being extracted
* from.
* @param \DOMXPath $xpath
* The page xpath that may be queried to add additional resource URLs.
*
* @see hook_pwa_cache_urls_assets_alter()
*/
function hook_pwa_cache_urls_assets_page_alter(&$resources, $page, $xpath) {
// Cache lazily-loaded images.
foreach ($xpath->query('//img[@data-src]') as $image) {
$resources[] = $image->getAttribute('data-src');
}
}
/**
* Alters manifest data.
*
* This hook allows altering the generated manifest data before encoding it to JSON.
*
* @param array &$manifestData
* Manifest data generated in Manifest::getOutput().
*/
function hook_pwa_manifest_alter(&$manifestData) {
$manifestData['short_name'] = 'App';
}
/**
* @} End of "addtogroup hooks".
*/