refactor: generalize sw route

Signed-off-by: Varun Patil <radialapps@gmail.com>
pull/888/head
Varun Patil 2023-10-20 12:21:41 -07:00
parent 05b8534c47
commit eb1e224bfa
3 changed files with 23 additions and 12 deletions

View File

@ -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'],
],
];

View File

@ -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;
});
}
}

View File

@ -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');