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

View File

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

View File

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