refactor: generalize sw route
Signed-off-by: Varun Patil <radialapps@gmail.com>pull/888/head
parent
05b8534c47
commit
eb1e224bfa
|
@ -98,7 +98,7 @@ return [
|
||||||
['name' => 'Admin#setSystemConfig', 'url' => '/api/system-config/{key}', 'verb' => 'PUT'],
|
['name' => 'Admin#setSystemConfig', 'url' => '/api/system-config/{key}', 'verb' => 'PUT'],
|
||||||
['name' => 'Admin#placesSetup', 'url' => '/api/occ/places-setup', 'verb' => 'POST'],
|
['name' => 'Admin#placesSetup', 'url' => '/api/occ/places-setup', 'verb' => 'POST'],
|
||||||
|
|
||||||
// Service worker
|
// Service worker and assets
|
||||||
['name' => 'Other#serviceWorker', 'url' => '/service-worker.js', 'verb' => 'GET'],
|
['name' => 'Other#static', 'url' => '/static/{name}', 'verb' => 'GET'],
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
|
@ -157,15 +157,26 @@ class OtherController extends GenericApiController
|
||||||
*
|
*
|
||||||
* @NoCSRFRequired
|
* @NoCSRFRequired
|
||||||
*/
|
*/
|
||||||
public function serviceWorker(): StreamResponse
|
public function static(string $name): Http\Response
|
||||||
{
|
{
|
||||||
$response = new StreamResponse(__DIR__.'/../../js/memories-service-worker.js');
|
return Util::guardEx(static function () use ($name) {
|
||||||
$response->setHeaders([
|
switch ($name) {
|
||||||
'Content-Type' => 'application/javascript',
|
case 'service-worker.js':
|
||||||
'Service-Worker-Allowed' => '/',
|
$response = (new StreamResponse(__DIR__.'/../../js/memories-service-worker.js'))->setHeaders([
|
||||||
]);
|
'Content-Type' => 'application/javascript',
|
||||||
$response->setContentSecurityPolicy(PageController::getCSP());
|
'Service-Worker-Allowed' => '/',
|
||||||
|
]);
|
||||||
|
|
||||||
return $response;
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new \Exception('Unknown static file');
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @var Http\Response $response */
|
||||||
|
$response->setContentSecurityPolicy(PageController::getCSP());
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -274,11 +274,11 @@ export default defineComponent({
|
||||||
// Use the window load event to keep the page load performant
|
// Use the window load event to keep the page load performant
|
||||||
window.addEventListener('load', async () => {
|
window.addEventListener('load', async () => {
|
||||||
try {
|
try {
|
||||||
const url = generateUrl('/apps/memories/service-worker.js');
|
const url = generateUrl('/apps/memories/static/service-worker.js');
|
||||||
const registration = await navigator.serviceWorker.register(url, {
|
const registration = await navigator.serviceWorker.register(url, {
|
||||||
scope: generateUrl('/apps/memories'),
|
scope: generateUrl('/apps/memories'),
|
||||||
});
|
});
|
||||||
console.log('SW registered: ', registration);
|
console.info('SW registered: ', registration);
|
||||||
|
|
||||||
// Check for updates
|
// Check for updates
|
||||||
const currentVersion = await staticConfig.get('version');
|
const currentVersion = await staticConfig.get('version');
|
||||||
|
|
Loading…
Reference in New Issue