Skip to content

Commit f0aaffe

Browse files
silverwindtechknowlogick
authored andcommitted
Add USE_SERVICE_WORKER setting (#9110)
* Add USE_SERVICE_WORKER setting This will be very useful setting for anyone doing frontend work. Fixes: #9044 * prevent potential syntax error on old browsers
1 parent cbaa1de commit f0aaffe

File tree

6 files changed

+35
-17
lines changed

6 files changed

+35
-17
lines changed

Diff for: custom/conf/app.ini.sample

+2
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

Diff for: 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

Diff for: 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**

Diff for: 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
@@ -969,6 +970,7 @@ func NewContext() {
969970
UI.ShowUserEmail = Cfg.Section("ui").Key("SHOW_USER_EMAIL").MustBool(true)
970971
UI.DefaultShowFullName = Cfg.Section("ui").Key("DEFAULT_SHOW_FULL_NAME").MustBool(false)
971972
UI.SearchRepoDescription = Cfg.Section("ui").Key("SEARCH_REPO_DESCRIPTION").MustBool(true)
973+
UI.UseServiceWorker = Cfg.Section("ui").Key("USE_SERVICE_WORKER").MustBool(true)
972974

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

Diff for: 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/")

Diff for: templates/base/head.tmpl

+20-12
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,30 @@
66
<meta http-equiv="x-ua-compatible" content="ie=edge">
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">
9-
9+
{{if UseServiceWorker}}
1010
<script>
1111
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-
});
12+
navigator.serviceWorker.register('{{AppSubUrl}}/serviceworker.js').then(function(registration) {
13+
// Registration was successful
14+
console.info('ServiceWorker registration successful with scope: ', registration.scope);
15+
}, function(err) {
16+
// registration failed :(
17+
console.info('ServiceWorker registration failed: ', err);
18+
});
2119
}
22-
2320
</script>
24-
21+
{{else}}
22+
<script>
23+
if ('serviceWorker' in navigator) {
24+
navigator.serviceWorker.getRegistrations().then(function(registrations) {
25+
registrations.forEach(function(registration) {
26+
registration.unregister();
27+
console.info('ServiceWorker unregistered');
28+
});
29+
});
30+
}
31+
</script>
32+
{{end}}
2533
<meta name="theme-color" content="{{ThemeColorMetaTag}}">
2634
<meta name="author" content="{{if .Repository}}{{.Owner.Name}}{{else}}{{MetaAuthor}}{{end}}" />
2735
<meta name="description" content="{{if .Repository}}{{.Repository.Name}}{{if .Repository.Description}} - {{.Repository.Description}}{{end}}{{else}}{{MetaDescription}}{{end}}" />

0 commit comments

Comments
 (0)