From 1d22d2514f7be14d51ed2ab78e13df5da2646546 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 18 Jan 2021 11:16:37 +0100 Subject: Fix service worker --- client/src/app/app.module.ts | 5 ++++- client/src/main.ts | 5 ----- client/src/manifest.webmanifest | 8 ++++---- client/src/ngsw-config.json | 1 + scripts/build/client.sh | 3 +-- server/controllers/client.ts | 21 ++++----------------- 6 files changed, 14 insertions(+), 29 deletions(-) diff --git a/client/src/app/app.module.ts b/client/src/app/app.module.ts index 98dec4244..f790a6848 100644 --- a/client/src/app/app.module.ts +++ b/client/src/app/app.module.ts @@ -1,7 +1,9 @@ import 'focus-visible' +import { environment } from 'src/environments/environment' import { APP_BASE_HREF, registerLocaleData } from '@angular/common' import { NgModule } from '@angular/core' import { BrowserModule } from '@angular/platform-browser' +import { ServiceWorkerModule } from '@angular/service-worker' import { ServerService } from '@app/core' import localeOc from '@app/helpers/locales/oc' import { MetaLoader, MetaModule, MetaStaticLoader, PageTitlePositioning } from '@ngx-meta/core' @@ -11,7 +13,7 @@ import { CoreModule } from './core' import { EmptyComponent } from './empty.component' import { HeaderComponent, SearchTypeaheadComponent, SuggestionComponent } from './header' import { HighlightPipe } from './header/highlight.pipe' -import { NotificationComponent, LanguageChooserComponent, MenuComponent } from './menu' +import { LanguageChooserComponent, MenuComponent, NotificationComponent } from './menu' import { ConfirmComponent } from './modal/confirm.component' import { CustomModalComponent } from './modal/custom-modal.component' import { InstanceConfigWarningModalComponent } from './modal/instance-config-warning-modal.component' @@ -49,6 +51,7 @@ registerLocaleData(localeOc, 'oc') imports: [ BrowserModule, + ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production }), CoreModule, SharedMainModule, diff --git a/client/src/main.ts b/client/src/main.ts index 99976a3f7..84c82203d 100644 --- a/client/src/main.ts +++ b/client/src/main.ts @@ -11,11 +11,6 @@ if (environment.production) { const bootstrap = () => platformBrowserDynamic() .bootstrapModule(AppModule) .then(bootstrapModule => { - if ('serviceWorker' in navigator && environment.production) { - navigator.serviceWorker.register('/ngsw-worker.js') - .catch(err => console.error('Cannot register service worker.', err)) - } - if (!environment.production) { const applicationRef = bootstrapModule.injector.get(ApplicationRef) const componentRef = applicationRef.components[0] diff --git a/client/src/manifest.webmanifest b/client/src/manifest.webmanifest index 851e098a8..6e62856cd 100644 --- a/client/src/manifest.webmanifest +++ b/client/src/manifest.webmanifest @@ -1,4 +1,7 @@ { + "name": "PeerTube", + "short_name": "PeerTube", + "start_url": "/", "background_color": "#fff", "theme_color": "#fff", "description": "A federated video streaming platform using P2P", @@ -40,8 +43,5 @@ "sizes": "512x512", "type": "image/png" } - ], - "name": "PeerTube", - "short_name": "PeerTube", - "start_url": "/" + ] } diff --git a/client/src/ngsw-config.json b/client/src/ngsw-config.json index d68db79d0..af7554c00 100644 --- a/client/src/ngsw-config.json +++ b/client/src/ngsw-config.json @@ -1,4 +1,5 @@ { + "$schema": "../node_modules/@angular/service-worker/config/schema.json", "index": "/index.html", "assetGroups": [ { diff --git a/scripts/build/client.sh b/scripts/build/client.sh index e6f14a4e2..0e7925be5 100755 --- a/scripts/build/client.sh +++ b/scripts/build/client.sh @@ -69,8 +69,7 @@ else npm run ng build -- --localize=false --output-path "dist/$defaultLanguage/" --deploy-url "/client/$defaultLanguage/" --prod --stats-json $additionalParams fi -mv "./dist/$defaultLanguage/manifest.webmanifest" "./dist/manifest.webmanifest" -mv "./dist/$defaultLanguage/ngsw-worker.js" "./dist/" +cp "./dist/$defaultLanguage/manifest.webmanifest" "./dist/manifest.webmanifest" cd ../ && npm run build:embed && cd client/ diff --git a/server/controllers/client.ts b/server/controllers/client.ts index bd1f19f8c..7a279d8d5 100644 --- a/server/controllers/client.ts +++ b/server/controllers/client.ts @@ -1,9 +1,10 @@ import * as express from 'express' import { constants, promises as fs } from 'fs' +import { readFile } from 'fs-extra' import { join } from 'path' import { CONFIG } from '@server/initializers/config' -import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '@shared/core-utils/i18n' import { HttpStatusCode } from '@shared/core-utils' +import { buildFileLocale, getCompleteLocale, I18N_LOCALES, is18nLocale, LOCALE_FILES } from '@shared/core-utils/i18n' import { root } from '../helpers/core-utils' import { STATIC_MAX_AGE } from '../initializers/constants' import { ClientHtml, sendHTML, serveIndexHTML } from '../lib/client-html' @@ -46,22 +47,8 @@ const testEmbedController = (req: express.Request, res: express.Response) => res clientsRouter.use('/videos/test-embed', testEmbedController) clientsRouter.use('/video-playlists/test-embed', testEmbedController) -// Static HTML/CSS/JS client files -const staticClientFiles = [ - 'ngsw-worker.js', - 'ngsw.json' -] - -for (const staticClientFile of staticClientFiles) { - const path = join(root(), 'client', 'dist', staticClientFile) - - clientsRouter.get(`/${staticClientFile}`, (req: express.Request, res: express.Response) => { - res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER }) - }) -} - // Dynamic PWA manifest -clientsRouter.get('/manifest.webmanifest', asyncMiddleware(generateManifest)) +clientsRouter.get(/\/client\/[^/]+\/manifest.webmanifest/, asyncMiddleware(generateManifest)) // Static client overrides // Must be consistent with static client overrides redirections in /support/nginx/peertube @@ -149,7 +136,7 @@ async function generateVideoChannelHtmlPage (req: express.Request, res: express. async function generateManifest (req: express.Request, res: express.Response) { const manifestPhysicalPath = join(root(), 'client', 'dist', 'manifest.webmanifest') - const manifestJson = await fs.readFile(manifestPhysicalPath, 'utf8') + const manifestJson = await readFile(manifestPhysicalPath, 'utf8') const manifest = JSON.parse(manifestJson) manifest.name = CONFIG.INSTANCE.NAME -- cgit v1.2.3