Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Progressive Web App #4730

Merged
merged 14 commits into from
Nov 27, 2018
12 changes: 12 additions & 0 deletions modules/templates/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ 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() *template.Template {
for _, funcs := range NewFuncMap() {
Expand Down
9 changes: 9 additions & 0 deletions modules/templates/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ 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() *template.Template {
for _, funcs := range NewFuncMap() {
Expand Down
9 changes: 9 additions & 0 deletions routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,15 @@ func RegisterRoutes(m *macaron.Macaron) {
}
})

// Progressive Web App
m.Get("/manifest.json", templates.JSONRenderer(), func(ctx *context.Context) {
ctx.HTML(200, "pwa/manifest_json")
})

m.Get("/serviceworker.js", templates.JSRenderer(), func(ctx *context.Context) {
ctx.HTML(200, "pwa/serviceworker_js")
})

// Not found handler.
m.NotFound(routers.NotFound)
}
17 changes: 17 additions & 0 deletions templates/base/head.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>{{if .Title}}{{.Title}} - {{end}}{{AppName}}</title>
<link rel="manifest" href="{{AppSubUrl}}/manifest.json">

<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('{{AppSubUrl}}/serviceworker.js').then(function(registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}, function(err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
});
}

</script>

<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}}" />
Expand Down
21 changes: 21 additions & 0 deletions templates/pwa/manifest_json.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"short_name": "Gitea",
"name": "Gitea - Git with a cup of tea",
"icons": [
{
"src": "{{AppSubUrl}}/img/gitea-lg.png",
"type": "image/png",
"sizes": "880x880"
},
{
"src": "{{AppSubUrl}}/img/gitea-sm.png",
"type": "image/png",
"sizes": "120x120"
}
],
"start_url": "{{AppSubUrl}}/",
"scope": "{{AppSubUrl}}/",
"background_color": "#FAFAFA",
"display": "standalone",
"theme_color": "{{ThemeColorMetaTag}}"
}
70 changes: 70 additions & 0 deletions templates/pwa/serviceworker_js.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
var STATIC_CACHE = 'static-cache-v1';
var urlsToCache = [
// js
'{{AppSubUrl}}/vendor/plugins/jquery.areyousure/jquery.are-you-sure.js',
'{{AppSubUrl}}/vendor/plugins/jquery/jquery.min.js',
'{{AppSubUrl}}/vendor/plugins/semantic/semantic.min.js',
'{{AppSubUrl}}/js/index.js?v={{MD5 AppVer}}',
'{{AppSubUrl}}/js/draw.js',
'{{AppSubUrl}}/vendor/plugins/clipboard/clipboard.min.js',
'{{AppSubUrl}}/vendor/plugins/gitgraph/gitgraph.js',
'{{AppSubUrl}}/vendor/plugins/vue/vue.min.js',
'{{AppSubUrl}}/vendor/plugins/emojify/emojify.min.js',
'{{AppSubUrl}}/vendor/plugins/cssrelpreload/loadCSS.min.js',
'{{AppSubUrl}}/vendor/plugins/cssrelpreload/cssrelpreload.min.js',
'{{AppSubUrl}}/vendor/plugins/dropzone/dropzone.js',
'{{AppSubUrl}}/vendor/plugins/highlight/highlight.pack.js',
'{{AppSubUrl}}/vendor/plugins/jquery.datetimepicker/jquery.datetimepicker.js',
'{{AppSubUrl}}/vendor/plugins/jquery.minicolors/jquery.minicolors.min.js',
'{{AppSubUrl}}/vendor/plugins/codemirror/addon/mode/loadmode.js',
'{{AppSubUrl}}/vendor/plugins/codemirror/mode/meta.js',
'{{AppSubUrl}}/vendor/plugins/simplemde/simplemde.min.js',

// css
'{{AppSubUrl}}/vendor/assets/font-awesome/css/font-awesome.min.css',
'{{AppSubUrl}}/vendor/assets/octicons/octicons.min.css',
'{{AppSubUrl}}/vendor/plugins/simplemde/simplemde.min.css',
'{{AppSubUrl}}/vendor/plugins/gitgraph/gitgraph.css',
'{{AppSubUrl}}/vendor/plugins/tribute/tribute.css',
'{{AppSubUrl}}/vendor/plugins/semantic/semantic.min.css',
'{{AppSubUrl}}/css/index.css?v={{MD5 AppVer}}',
'{{AppSubUrl}}/vendor/plugins/highlight/github.css',
'{{AppSubUrl}}/vendor/plugins/jquery.minicolors/jquery.minicolors.css',
'{{AppSubUrl}}/vendor/plugins/jquery.datetimepicker/jquery.datetimepicker.css',
'{{AppSubUrl}}/vendor/plugins/dropzone/dropzone.css',
'/css/theme-{{DefaultTheme}}.css',
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved

// img
'{{AppSubUrl}}/img/gitea-sm.png',
'{{AppSubUrl}}/img/gitea-lg.png',

// fonts
'{{AppSubUrl}}/vendor/plugins/semantic/themes/default/assets/fonts/icons.woff2',
'{{AppSubUrl}}/vendor/assets/octicons/octicons.woff2', // TODO: ?MD5
SohnyBohny marked this conversation as resolved.
Show resolved Hide resolved
'{{AppSubUrl}}/vendor/assets/lato-fonts/lato-v14-latin-regular.woff2',
'{{AppSubUrl}}/vendor/assets/lato-fonts/lato-v14-latin-700.woff2'
];

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);
}
)
);
});