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#placesSetup', 'url' => '/api/occ/places-setup', 'verb' => 'POST'],
|
||||
|
||||
// Service worker
|
||||
['name' => 'Other#serviceWorker', 'url' => '/service-worker.js', 'verb' => 'GET'],
|
||||
// Service worker and assets
|
||||
['name' => 'Other#static', 'url' => '/static/{name}', 'verb' => 'GET'],
|
||||
],
|
||||
];
|
||||
|
|
|
@ -157,15 +157,26 @@ class OtherController extends GenericApiController
|
|||
*
|
||||
* @NoCSRFRequired
|
||||
*/
|
||||
public function serviceWorker(): StreamResponse
|
||||
public function static(string $name): Http\Response
|
||||
{
|
||||
$response = new StreamResponse(__DIR__.'/../../js/memories-service-worker.js');
|
||||
$response->setHeaders([
|
||||
'Content-Type' => 'application/javascript',
|
||||
'Service-Worker-Allowed' => '/',
|
||||
]);
|
||||
$response->setContentSecurityPolicy(PageController::getCSP());
|
||||
return Util::guardEx(static function () use ($name) {
|
||||
switch ($name) {
|
||||
case 'service-worker.js':
|
||||
$response = (new StreamResponse(__DIR__.'/../../js/memories-service-worker.js'))->setHeaders([
|
||||
'Content-Type' => 'application/javascript',
|
||||
'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
|
||||
window.addEventListener('load', async () => {
|
||||
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, {
|
||||
scope: generateUrl('/apps/memories'),
|
||||
});
|
||||
console.log('SW registered: ', registration);
|
||||
console.info('SW registered: ', registration);
|
||||
|
||||
// Check for updates
|
||||
const currentVersion = await staticConfig.get('version');
|
||||
|
|
Loading…
Reference in New Issue