From 3d369bb014d7dff01677c72a71ff5b46d6c5f925 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 2 Mar 2022 19:00:26 +0800 Subject: [PATCH 01/11] Show messages for users if the ROOT_URL is wrong --- templates/base/footer.tmpl | 2 +- templates/base/head.tmpl | 1 + web_src/js/features/common-global.js | 27 ++++++++++++++++++++++++++- web_src/js/index.js | 3 +++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/templates/base/footer.tmpl b/templates/base/footer.tmpl index 1aabfa2f5c64..9bf16f8aa5b5 100644 --- a/templates/base/footer.tmpl +++ b/templates/base/footer.tmpl @@ -22,7 +22,7 @@ {{end}} {{end}} - + {{template "custom/footer" .}} diff --git a/templates/base/head.tmpl b/templates/base/head.tmpl index 32e206a95d58..8c2f10051a4a 100644 --- a/templates/base/head.tmpl +++ b/templates/base/head.tmpl @@ -19,6 +19,7 @@ window.config = { appVer: '{{AppVer}}', + appUrl: '{{AppUrl}}', appSubUrl: '{{AppSubUrl}}', assetUrlPrefix: '{{AssetUrlPrefix}}', runModeIsProd: {{.RunModeIsProd}}, diff --git a/web_src/js/features/common-global.js b/web_src/js/features/common-global.js index a9baf9be0c68..eab213a39539 100644 --- a/web_src/js/features/common-global.js +++ b/web_src/js/features/common-global.js @@ -3,8 +3,9 @@ import 'jquery.are-you-sure'; import {mqBinarySearch} from '../utils.js'; import createDropzone from './dropzone.js'; import {initCompColorPicker} from './comp/ColorPicker.js'; +import {htmlEscape} from 'escape-goat'; -const {csrfToken} = window.config; +const {appUrl, csrfToken} = window.config; export function initGlobalFormDirtyLeaveConfirm() { // Warn users that try to leave a page after entering data into a form. @@ -343,3 +344,27 @@ export function initGlobalButtons() { }); }); } + +/** + * Too many users set their ROOT_URL to wrong value, and it causes a lot of problems: + * * Cross-origin API request without correct cookie + * * Incorrect href in + * * ... + * So we check whether current URL starts with AppUrl(ROOT_URL). + * If they don't match, show a warning to users. + */ +export function checkAppUrl() { + const curUrl = window.location.href; + if (curUrl.startsWith(appUrl)) { + return; + } + const $pageContent = $('.page-content'); + if (!$pageContent.length) { + return; + } + const $tip = $(`
+ Your ROOT_URL in app.ini is ${htmlEscape(appUrl)} but you are visiting ${htmlEscape(curUrl)}
+ You should set ROOT_URL correctly, otherwise the web may not work correctly. +
`); + $($pageContent[0]).prepend($tip); +} diff --git a/web_src/js/index.js b/web_src/js/index.js index b7eba5e6649a..64c485d28321 100644 --- a/web_src/js/index.js +++ b/web_src/js/index.js @@ -39,6 +39,7 @@ import { } from './features/repo-issue.js'; import {initRepoEllipsisButton, initRepoCommitLastCommitLoader} from './features/repo-commit.js'; import { + checkAppUrl, initFootLanguageMenu, initGlobalButtonClickOnEnter, initGlobalButtons, @@ -169,4 +170,6 @@ $(document).ready(() => { initUserAuthWebAuthn(); initUserAuthWebAuthnRegister(); initUserSettings(); + + checkAppUrl(); }); From 94e6862d0519a19a60f437e9d95668425cf5f40f Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 29 Mar 2022 16:45:58 +0800 Subject: [PATCH 02/11] add global error handler --- templates/base/head.tmpl | 1 + web_src/js/features/common-global.js | 33 ++++++++++++++++++++-------- web_src/js/index.js | 3 ++- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/templates/base/head.tmpl b/templates/base/head.tmpl index 8c2f10051a4a..4b294a6a2c37 100644 --- a/templates/base/head.tmpl +++ b/templates/base/head.tmpl @@ -17,6 +17,7 @@ {{end}} + + {{template "base/head_script" .}} +