From 88fe7b5a720365e5c8f0a347730f895263465311 Mon Sep 17 00:00:00 2001 From: silverwind Date: Fri, 22 May 2020 03:45:34 +0200 Subject: [PATCH] Move serviceworker to workbox and fix SSE interference (#11538) * Move serviceworker to workbox and fix SSE interference Instead of statically hardcoding every frontend asset, this uses a type-based approach to cache all js,css and manifest.json requests. This also fixes the issue that the service worker was interfering with EventSource because it was unconditionally handling all requests which this new implementation doesn't. Fixes: https://github.com/go-gitea/gitea/issues/11092 Fixes: https://github.com/go-gitea/gitea/issues/7372 * rethrow error instead of logging * await .register * Revert "rethrow error instead of logging" This reverts commit 043162ba1f18b98a4bf9635959fd28d16e839fc5. * improve comment * remove JSRenderer * add version-based cache invalidation * refactor * more refactor * remove comment * rename item to fit cache name Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> --- .eslintrc | 1 + modules/templates/dynamic.go | 12 ---- modules/templates/static.go | 9 --- package-lock.json | 22 ++++++++ package.json | 2 + routers/routes/routes.go | 4 -- templates/base/head.tmpl | 26 +-------- templates/pwa/serviceworker_js.tmpl | 83 ---------------------------- web_src/js/features/serviceworker.js | 43 ++++++++++++++ web_src/js/index.js | 2 + web_src/js/serviceworker.js | 16 ++++++ webpack.config.js | 9 ++- 12 files changed, 96 insertions(+), 133 deletions(-) delete mode 100644 templates/pwa/serviceworker_js.tmpl create mode 100644 web_src/js/features/serviceworker.js create mode 100644 web_src/js/serviceworker.js diff --git a/.eslintrc b/.eslintrc index 8f337baec5..3a731cbf6b 100644 --- a/.eslintrc +++ b/.eslintrc @@ -48,6 +48,7 @@ rules: no-cond-assign: [2, except-parens] no-console: [1, {allow: [info, warn, error]}] no-continue: [0] + no-empty: [2, {allowEmptyCatch: true}] no-eq-null: [2] no-mixed-operators: [0] no-multi-assign: [0] diff --git a/modules/templates/dynamic.go b/modules/templates/dynamic.go index 6153e8d027..bd1c4d06c5 100644 --- a/modules/templates/dynamic.go +++ b/modules/templates/dynamic.go @@ -48,18 +48,6 @@ func JSONRenderer() macaron.Handler { }) } -// JSRenderer implements the macaron handler for serving JS templates. -func JSRenderer() macaron.Handler { - return macaron.Renderer(macaron.RenderOptions{ - Funcs: NewFuncMap(), - Directory: path.Join(setting.StaticRootPath, "templates"), - AppendDirectories: []string{ - path.Join(setting.CustomPath, "templates"), - }, - HTMLContentType: "application/javascript", - }) -} - // Mailer provides the templates required for sending notification mails. func Mailer() (*texttmpl.Template, *template.Template) { for _, funcs := range NewTextFuncMap() { diff --git a/modules/templates/static.go b/modules/templates/static.go index 5bc4e33e1c..a3aff5e567 100644 --- a/modules/templates/static.go +++ b/modules/templates/static.go @@ -132,15 +132,6 @@ func JSONRenderer() macaron.Handler { }) } -// JSRenderer implements the macaron handler for serving JS templates. -func JSRenderer() macaron.Handler { - return macaron.Renderer(macaron.RenderOptions{ - Funcs: NewFuncMap(), - TemplateFileSystem: NewTemplateFileSystem(), - HTMLContentType: "application/javascript", - }) -} - // Mailer provides the templates required for sending notification mails. func Mailer() (*texttmpl.Template, *template.Template) { for _, funcs := range NewTextFuncMap() { diff --git a/package-lock.json b/package-lock.json index 89c41721c7..6206025e08 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14857,6 +14857,28 @@ "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.2.tgz", "integrity": "sha1-t5Zpu0LstAn4PVg8rVLKF+qhZD8=" }, + "workbox-core": { + "version": "5.1.3", + "resolved": "https://registry.npmjs.org/workbox-core/-/workbox-core-5.1.3.tgz", + "integrity": "sha512-TFSIPxxciX9sFaj0FDiohBeIKpwMcCyNduydi9i3LChItcndDS6TJpErxybv8aBWeCMraXt33TWtF6kKuIObNw==" + }, + "workbox-routing": { + "version": "5.1.3", + "resolved": "https://registry.npmjs.org/workbox-routing/-/workbox-routing-5.1.3.tgz", + "integrity": "sha512-F+sAp9Iy3lVl3BEG+pzXWVq4AftzjiFpHDaZ4Kf4vLoBoKQE0hIHet4zE5DpHqYdyw+Udhp4wrfHamX6PN6z1Q==", + "requires": { + "workbox-core": "^5.1.3" + } + }, + "workbox-strategies": { + "version": "5.1.3", + "resolved": "https://registry.npmjs.org/workbox-strategies/-/workbox-strategies-5.1.3.tgz", + "integrity": "sha512-wiXHfmOKnWABeIVW+/ye0e00+2CcS5y7SIj2f9zcdy2ZLEbcOf7B+yOl5OrWpBGlTUwRjIYhV++ZqiKm3Dc+8w==", + "requires": { + "workbox-core": "^5.1.3", + "workbox-routing": "^5.1.3" + } + }, "worker-farm": { "version": "1.7.0", "resolved": "https://registry.npmjs.org/worker-farm/-/worker-farm-1.7.0.tgz", diff --git a/package.json b/package.json index 1e81ea1448..e818e7d7c3 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,8 @@ "webpack": "4.43.0", "webpack-cli": "3.3.11", "webpack-fix-style-only-entries": "0.4.0", + "workbox-routing": "5.1.3", + "workbox-strategies": "5.1.3", "worker-loader": "2.0.0" }, "devDependencies": { diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 7f409eb576..d739f0b6ca 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -1048,10 +1048,6 @@ func RegisterRoutes(m *macaron.Macaron) { ctx.HTML(200, "pwa/manifest_json") }) - m.Get("/serviceworker.js", templates.JSRenderer(), func(ctx *context.Context) { - ctx.HTML(200, "pwa/serviceworker_js") - }) - // prometheus metrics endpoint if setting.Metrics.Enabled { c := metrics.NewCollector() diff --git a/templates/base/head.tmpl b/templates/base/head.tmpl index 8e58c07d23..a6a715531d 100644 --- a/templates/base/head.tmpl +++ b/templates/base/head.tmpl @@ -6,30 +6,6 @@ {{if .Title}}{{.Title | RenderEmojiPlain}} - {{end}} {{if .Repository.Name}}{{.Repository.Name}} - {{end}}{{AppName}} - {{if UseServiceWorker}} - - {{else}} - - {{end}} @@ -84,8 +60,10 @@