From e76a64dda173db84e34eefc6baaa2981fe4a6801 Mon Sep 17 00:00:00 2001 From: John Olheiser <42128690+jolheiser@users.noreply.github.com> Date: Mon, 17 Feb 2020 17:11:59 -0600 Subject: [PATCH] Inject SVG sprite via ajax (#10320) * AJAX SVG * Fix PWA * Remove unused PWA assets Signed-off-by: jolheiser Co-Authored-by: silverwind --- modules/templates/helper.go | 2 +- templates/pwa/serviceworker_js.tmpl | 4 +--- web_src/js/index.js | 9 +++++++++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 30ca9c163..a97431a69 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -287,7 +287,7 @@ func NewFuncMap() []template.FuncMap { return false }, "svg": func(icon string, size int) template.HTML { - return template.HTML(fmt.Sprintf(``, icon, size, size, setting.StaticURLPrefix, icon)) + return template.HTML(fmt.Sprintf(``, icon, size, size, icon)) }, }} } diff --git a/templates/pwa/serviceworker_js.tmpl b/templates/pwa/serviceworker_js.tmpl index f1dd63a25..5e4cb5798 100644 --- a/templates/pwa/serviceworker_js.tmpl +++ b/templates/pwa/serviceworker_js.tmpl @@ -22,7 +22,6 @@ var urlsToCache = [ '{{StaticUrlPrefix}}/css/swagger.css?v={{MD5 AppVer}}', '{{StaticUrlPrefix}}/fomantic/semantic.min.css?v={{MD5 AppVer}}', '{{StaticUrlPrefix}}/vendor/assets/font-awesome/css/font-awesome.min.css', - '{{StaticUrlPrefix}}/vendor/assets/octicons/octicons.min.css', '{{StaticUrlPrefix}}/vendor/plugins/dropzone/dropzone.css', '{{StaticUrlPrefix}}/vendor/plugins/jquery.datetimepicker/jquery.datetimepicker.css', '{{StaticUrlPrefix}}/vendor/plugins/jquery.minicolors/jquery.minicolors.css', @@ -41,11 +40,10 @@ var urlsToCache = [ '{{StaticUrlPrefix}}/img/gitea-lg.png', // svg - '{{StaticUrlPrefix}}/img/svg/icons.svg' + '{{StaticUrlPrefix}}/img/svg/icons.svg', // fonts '{{StaticUrlPrefix}}/fomantic/themes/default/assets/fonts/icons.woff2', - '{{StaticUrlPrefix}}/vendor/assets/octicons/octicons.woff2?ef21c39f0ca9b1b5116e5eb7ac5eabe6', '{{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', diff --git a/web_src/js/index.js b/web_src/js/index.js index 68fb4fddb..2f2baddcf 100644 --- a/web_src/js/index.js +++ b/web_src/js/index.js @@ -3581,3 +3581,12 @@ window.onOAuthLoginClick = function () { oauthNav.show(); }, 5000); }; + +// Pull SVGs via AJAX to workaround CORS issues with tags +// https://css-tricks.com/ajaxing-svg-sprite/ +$.get(`${window.config.StaticUrlPrefix}/img/svg/icons.svg`, (data) => { + const div = document.createElement('div'); + div.style.display = 'none'; + div.innerHTML = new XMLSerializer().serializeToString(data.documentElement); + document.body.insertBefore(div, document.body.childNodes[0]); +});