diff options
author | silverwind <me@silverwind.io> | 2020-05-22 03:45:34 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-21 21:45:34 -0400 |
commit | 88fe7b5a720365e5c8f0a347730f895263465311 (patch) | |
tree | 7d9de5b5c7d78878787dbe12c40ebe97791b2b52 /templates | |
parent | f6f49700cc0f5d66b0c1ff5dc3e310c777583e85 (diff) | |
download | gitea-88fe7b5a720365e5c8f0a347730f895263465311.tar.gz gitea-88fe7b5a720365e5c8f0a347730f895263465311.zip |
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>
Diffstat (limited to 'templates')
-rw-r--r-- | templates/base/head.tmpl | 26 | ||||
-rw-r--r-- | templates/pwa/serviceworker_js.tmpl | 83 |
2 files changed, 2 insertions, 107 deletions
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 @@ <meta http-equiv="x-ua-compatible" content="ie=edge"> <title>{{if .Title}}{{.Title | RenderEmojiPlain}} - {{end}} {{if .Repository.Name}}{{.Repository.Name}} - {{end}}{{AppName}} </title> <link rel="manifest" href="{{AppSubUrl}}/manifest.json" crossorigin="use-credentials"> - {{if UseServiceWorker}} - <script> - if ('serviceWorker' in navigator) { - navigator.serviceWorker.register('{{AppSubUrl}}/serviceworker.js').then(function(registration) { - // Registration was successful - console.info('ServiceWorker registration successful with scope: ', registration.scope); - }, function(err) { - // registration failed :( - console.info('ServiceWorker registration failed: ', err); - }); - } - </script> - {{else}} - <script> - if ('serviceWorker' in navigator) { - navigator.serviceWorker.getRegistrations().then(function(registrations) { - registrations.forEach(function(registration) { - registration.unregister(); - console.info('ServiceWorker unregistered'); - }); - }); - } - </script> - {{end}} <meta name="theme-color" content="{{ThemeColorMetaTag}}"> <meta name="author" content="{{if .Repository}}{{.Owner.Name}}{{else}}{{MetaAuthor}}{{end}}" /> <meta name="description" content="{{if .Repository}}{{.Repository.Name}}{{if .Repository.Description}} - {{.Repository.Description}}{{end}}{{else}}{{MetaDescription}}{{end}}" /> @@ -84,8 +60,10 @@ </script> <script> window.config = { + AppVer: '{{AppVer}}', AppSubUrl: '{{AppSubUrl}}', StaticUrlPrefix: '{{StaticUrlPrefix}}', + UseServiceWorker: {{UseServiceWorker}}, csrf: '{{.CsrfToken}}', HighlightJS: {{if .RequireHighlightJS}}true{{else}}false{{end}}, Minicolors: {{if .RequireMinicolors}}true{{else}}false{{end}}, diff --git a/templates/pwa/serviceworker_js.tmpl b/templates/pwa/serviceworker_js.tmpl deleted file mode 100644 index a074879f3e..0000000000 --- a/templates/pwa/serviceworker_js.tmpl +++ /dev/null @@ -1,83 +0,0 @@ -var STATIC_CACHE = 'static-cache-v1'; -var urlsToCache = [ - // js - '{{StaticUrlPrefix}}/fomantic/semantic.min.js?v={{MD5 AppVer}}', - '{{StaticUrlPrefix}}/js/clipboard.js', - '{{StaticUrlPrefix}}/js/gitgraph.js', - '{{StaticUrlPrefix}}/js/highlight.js', - '{{StaticUrlPrefix}}/js/index.js?v={{MD5 AppVer}}', - '{{StaticUrlPrefix}}/js/jquery.js?v={{MD5 AppVer}}', - '{{StaticUrlPrefix}}/js/swagger.js?v={{MD5 AppVer}}', - '{{StaticUrlPrefix}}/js/dropzone.js', - '{{StaticUrlPrefix}}/js/datetimepicker.js', - '{{StaticUrlPrefix}}/js/tribute.js', - '{{StaticUrlPrefix}}/vendor/plugins/codemirror/addon/mode/loadmode.js', - '{{StaticUrlPrefix}}/vendor/plugins/codemirror/mode/meta.js', - '{{StaticUrlPrefix}}/vendor/plugins/jquery.minicolors/jquery.minicolors.min.js', - '{{StaticUrlPrefix}}/vendor/plugins/simplemde/simplemde.min.js', - - // css - '{{StaticUrlPrefix}}/css/index.css?v={{MD5 AppVer}}', - '{{StaticUrlPrefix}}/css/swagger.css?v={{MD5 AppVer}}', - '{{StaticUrlPrefix}}/css/dropzone.css', - '{{StaticUrlPrefix}}/css/datetimepicker.css', - '{{StaticUrlPrefix}}/fomantic/semantic.min.css?v={{MD5 AppVer}}', - '{{StaticUrlPrefix}}/vendor/assets/font-awesome/css/font-awesome.min.css', - '{{StaticUrlPrefix}}/vendor/plugins/jquery.minicolors/jquery.minicolors.css', - '{{StaticUrlPrefix}}/vendor/plugins/simplemde/simplemde.min.css', -{{if .IsSigned }} - {{ if ne .SignedUser.Theme "gitea" }} - '{{StaticUrlPrefix}}/css/theme-{{.SignedUser.Theme}}.css?v={{MD5 AppVer}}', - {{end}} -{{else if ne DefaultTheme "gitea"}} - '{{StaticUrlPrefix}}/css/theme-{{DefaultTheme}}.css?v={{MD5 AppVer}}', -{{end}} - - // img - '{{StaticUrlPrefix}}/img/gitea-sm.png', - '{{StaticUrlPrefix}}/img/gitea-lg.png', - - // svg - '{{StaticUrlPrefix}}/img/svg/icons.svg', - - // fonts - '{{StaticUrlPrefix}}/fomantic/themes/default/assets/fonts/icons.woff2', - '{{StaticUrlPrefix}}/vendor/assets/roboto-fonts/roboto-v20-latin-ext_cyrillic-ext_latin_greek_vietnamese_cyrillic_greek-ext-regular.woff2', - '{{StaticUrlPrefix}}/vendor/assets/roboto-fonts/roboto-v20-latin-ext_cyrillic-ext_latin_greek_vietnamese_cyrillic_greek-ext-italic.woff2', - '{{StaticUrlPrefix}}/vendor/assets/roboto-fonts/roboto-v20-latin-ext_cyrillic-ext_latin_greek_vietnamese_cyrillic_greek-ext-700.woff2', - '{{StaticUrlPrefix}}/vendor/assets/roboto-fonts/roboto-v20-latin-ext_cyrillic-ext_latin_greek_vietnamese_cyrillic_greek-ext-700italic.woff2', - - // monaco - '{{StaticUrlPrefix}}/css/monaco.css', - '{{StaticUrlPrefix}}/fonts/codicon.ttf', - '{{StaticUrlPrefix}}/js/monaco-css.worker.js', - '{{StaticUrlPrefix}}/js/monaco-editor.worker.js', - '{{StaticUrlPrefix}}/js/monaco-html.worker.js', - '{{StaticUrlPrefix}}/js/monaco-json.worker.js', - '{{StaticUrlPrefix}}/js/monaco.js', - '{{StaticUrlPrefix}}/js/monaco-ts.worker.js' -]; - -self.addEventListener('install', function (event) { - // Perform install steps - event.waitUntil( - caches.open(STATIC_CACHE) - .then(function (cache) { - return cache.addAll(urlsToCache); - }) - ); -}); - -self.addEventListener('fetch', function (event) { - event.respondWith( - caches.match(event.request) - .then(function (response) { - // Cache hit - return response - if (response) { - return response; - } - return fetch(event.request); - } - ) - ); -}); |