Skip to content

Commit ee355da

Browse files
committed
Add USE_SERVICE_WORKER setting
This will be very useful setting for anyone doing frontend work. Fixes: #9044
1 parent d7ac972 commit ee355da

File tree

6 files changed

+36
-16
lines changed

6 files changed

+36
-16
lines changed

custom/conf/app.ini.sample

+3-1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ THEMES = gitea,arc-green
153153
DEFAULT_SHOW_FULL_NAME = false
154154
; Whether to search within description at repository search on explore page.
155155
SEARCH_REPO_DESCRIPTION = true
156+
; Whether to enable a Service Worker to cache frontend assets
157+
USE_SERVICE_WORKER = true
156158

157159
[ui.admin]
158160
; Number of users that are displayed on one page
@@ -897,4 +899,4 @@ QUEUE_CONN_STR = "addrs=127.0.0.1:6379 db=0"
897899
; Max attempts per http/https request on migrations.
898900
MAX_ATTEMPTS = 3
899901
; Backoff time per http/https request retry (seconds)
900-
RETRY_BACKOFF = 3
902+
RETRY_BACKOFF = 3

docs/content/doc/advanced/config-cheat-sheet.en-us.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,9 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
118118
- `DEFAULT_THEME`: **gitea**: \[gitea, arc-green\]: Set the default theme for the Gitea install.
119119
- `THEMES`: **gitea,arc-green**: All available themes. Allow users select personalized themes
120120
regardless of the value of `DEFAULT_THEME`.
121-
- `DEFAULT_SHOW_FULL_NAME`: false: Whether the full name of the users should be shown where possible. If the full name isn't set, the username will be used.
122-
- `SEARCH_REPO_DESCRIPTION`: true: Whether to search within description at repository search on explore page.
121+
- `DEFAULT_SHOW_FULL_NAME`: **false**: Whether the full name of the users should be shown where possible. If the full name isn't set, the username will be used.
122+
- `SEARCH_REPO_DESCRIPTION`: **true**: Whether to search within description at repository search on explore page.
123+
- `USE_SERVICE_WORKER`: **true**: Whether to enable a Service Worker to cache frontend assets.
123124

124125
### UI - Admin (`ui.admin`)
125126

docs/content/doc/advanced/hacking-on-gitea.en-us.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -138,22 +138,24 @@ make revive vet misspell-check
138138

139139
### Updating CSS
140140

141-
To generate the CSS, you will need [Node.js](https://nodejs.org/) 8.0 or greater with npm. At present we use [less](http://lesscss.org/) and [postcss](https://postcss.org) to generate our CSS. Do **not** edit the files in `public/css` directly, as they are generated from `lessc` from the files in `public/less`.
141+
To generate the CSS, you need [Node.js](https://nodejs.org/) 8.0 or greater with npm. We use [less](http://lesscss.org/) and [postcss](https://postcss.org) to generate our CSS. Do **not** edit the files in `public/css` directly, as they are generated from `lessc` from the files in `public/less`.
142142

143-
Edit files in `public/less`, run the linter, regenerate the CSS and commit all changed files:
143+
Edit files in `public/less`, and then run the linter and build the CSS files via:
144144

145145
```bash
146146
make css
147147
```
148148

149149
### Updating JS
150150

151-
To run the JavaScript linter you will need [Node.js](https://nodejs.org/) 8.0 or greater with npm. Edit files in `public/js` and run the linter:
151+
To generate the JS files, you need [Node.js](https://nodejs.org/) 8.0 or greater with npm. Edit files in `public/js`, run the linter and build the JS files via:
152152

153153
```bash
154154
make js
155155
```
156156

157+
Note: When working on frontend code, it is advisable to set `USE_SERVICE_WORKER` to `false` in `app.ini` which will prevent undesirable caching of frontend assets.
158+
157159
### Updating the API
158160

159161
When creating new API routes or modifying existing API routes, you **MUST**

modules/setting/setting.go

+2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ var (
170170
DefaultTheme string
171171
Themes []string
172172
SearchRepoDescription bool
173+
UseServiceWorker bool
173174

174175
Admin struct {
175176
UserPagingNum int
@@ -968,6 +969,7 @@ func NewContext() {
968969
UI.ShowUserEmail = Cfg.Section("ui").Key("SHOW_USER_EMAIL").MustBool(true)
969970
UI.DefaultShowFullName = Cfg.Section("ui").Key("DEFAULT_SHOW_FULL_NAME").MustBool(false)
970971
UI.SearchRepoDescription = Cfg.Section("ui").Key("SEARCH_REPO_DESCRIPTION").MustBool(true)
972+
UI.UseServiceWorker = Cfg.Section("ui").Key("USE_SERVICE_WORKER").MustBool(true)
971973

972974
HasRobotsTxt = com.IsFile(path.Join(CustomPath, "robots.txt"))
973975

modules/templates/helper.go

+3
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ func NewFuncMap() []template.FuncMap {
148148
"MetaKeywords": func() string {
149149
return setting.UI.Meta.Keywords
150150
},
151+
"UseServiceWorker": func() bool {
152+
return setting.UI.UseServiceWorker
153+
},
151154
"FilenameIsImage": func(filename string) bool {
152155
mimeType := mime.TypeByExtension(filepath.Ext(filename))
153156
return strings.HasPrefix(mimeType, "image/")

templates/base/head.tmpl

+20-10
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,30 @@
77
<title>{{if .Title}}{{.Title}} - {{end}} {{if .Repository.Name}}{{.Repository.Name}} - {{end}}{{AppName}}</title>
88
<link rel="manifest" href="{{AppSubUrl}}/manifest.json" crossorigin="use-credentials">
99

10+
{{if UseServiceWorker}}
1011
<script>
1112
if ('serviceWorker' in navigator) {
12-
window.addEventListener('load', function() {
13-
navigator.serviceWorker.register('{{AppSubUrl}}/serviceworker.js').then(function(registration) {
14-
// Registration was successful
15-
console.log('ServiceWorker registration successful with scope: ', registration.scope);
16-
}, function(err) {
17-
// registration failed :(
18-
console.log('ServiceWorker registration failed: ', err);
19-
});
20-
});
13+
navigator.serviceWorker.register('{{AppSubUrl}}/serviceworker.js').then(function(registration) {
14+
// Registration was successful
15+
console.info('ServiceWorker registration successful with scope: ', registration.scope);
16+
}, function(err) {
17+
// registration failed :(
18+
console.info('ServiceWorker registration failed: ', err);
19+
});
2120
}
22-
2321
</script>
22+
{{else}}
23+
<script>
24+
if ('serviceWorker' in navigator) {
25+
navigator.serviceWorker.getRegistrations().then(function(registrations) {
26+
for (const registration of registrations) {
27+
registration.unregister();
28+
console.info('ServiceWorker unregistered');
29+
}
30+
});
31+
}
32+
</script>
33+
{{end}}
2434

2535
<meta name="theme-color" content="{{ThemeColorMetaTag}}">
2636
<meta name="author" content="{{if .Repository}}{{.Owner.Name}}{{else}}{{MetaAuthor}}{{end}}" />

0 commit comments

Comments
 (0)