From 30f991bf6e36df715093fd7a1c64f77c61c0aa11 Mon Sep 17 00:00:00 2001 From: lishutong Date: Wed, 28 Jul 2021 16:54:09 +0000 Subject: [PATCH] Fix a bug: redirect to homepage for some url. In a single page application, the url sometimes does not correspond to the actual resources on the server. Thus, it might cause 404 when users refresh the page. By redirecting this kind of url back to the homepage, the proper page can be rendered. If the url does not exist, the front end will redirect to a Not Found page. Test: mannual tested. Change-Id: I36beff436a450ae7fcabe9172df9c7cc217d7305 --- tools/ota_analysis/src/router/index.js | 6 ++++ tools/ota_analysis/src/views/NotFound.vue | 21 ++++++++++++ tools/otagui/src/components/OTAJobTable.vue | 2 +- tools/otagui/src/router/index.js | 8 ++++- tools/otagui/src/views/NotFound.vue | 21 ++++++++++++ tools/otagui/web_server.py | 36 +++++++++++---------- 6 files changed, 75 insertions(+), 19 deletions(-) create mode 100644 tools/ota_analysis/src/views/NotFound.vue create mode 100644 tools/otagui/src/views/NotFound.vue diff --git a/tools/ota_analysis/src/router/index.js b/tools/ota_analysis/src/router/index.js index 83aedd077..0a16a50eb 100644 --- a/tools/ota_analysis/src/router/index.js +++ b/tools/ota_analysis/src/router/index.js @@ -2,6 +2,7 @@ import { createRouter, createWebHistory } from 'vue-router' import PackageAnalysis from '@/views/PackageAnalysis.vue' import Demo from'@/views/Demo.vue' import About from '@/views/About.vue' +import NotFound from '@/views/NotFound.vue' const routes = [ { @@ -21,6 +22,11 @@ const routes = [ path: '/about', name: 'About', component: About + }, + { + path: '/:catchAll(.*)', + name: 'Not Found', + component: NotFound } ] diff --git a/tools/ota_analysis/src/views/NotFound.vue b/tools/ota_analysis/src/views/NotFound.vue new file mode 100644 index 000000000..2424a76e4 --- /dev/null +++ b/tools/ota_analysis/src/views/NotFound.vue @@ -0,0 +1,21 @@ + + + \ No newline at end of file diff --git a/tools/otagui/src/components/OTAJobTable.vue b/tools/otagui/src/components/OTAJobTable.vue index 6e96d3d3e..19106b982 100644 --- a/tools/otagui/src/components/OTAJobTable.vue +++ b/tools/otagui/src/components/OTAJobTable.vue @@ -45,7 +45,7 @@ export default { sortable: true, display: function (row) { return ( - "' + "' + row.status + "" ); diff --git a/tools/otagui/src/router/index.js b/tools/otagui/src/router/index.js index a68ac7506..5699f970c 100644 --- a/tools/otagui/src/router/index.js +++ b/tools/otagui/src/router/index.js @@ -3,6 +3,7 @@ import JobList from '@/views/JobList.vue' import JobDetails from '@/views/JobDetails.vue' import About from '@/views/About.vue' import JobConfigure from '@/views/JobConfigure.vue' +import NotFound from '@/views/NotFound.vue' const routes = [ { @@ -11,7 +12,7 @@ const routes = [ component: JobList }, { - path: '/check/:id', + path: '/check-job/:id', name: 'JobDetails', props: true, component: JobDetails @@ -25,6 +26,11 @@ const routes = [ path: '/create', name: 'Create', component: JobConfigure + }, + { + path: '/:catchAll(.*)', + name: 'Not Found', + component: NotFound } ] diff --git a/tools/otagui/src/views/NotFound.vue b/tools/otagui/src/views/NotFound.vue new file mode 100644 index 000000000..2424a76e4 --- /dev/null +++ b/tools/otagui/src/views/NotFound.vue @@ -0,0 +1,21 @@ + + + \ No newline at end of file diff --git a/tools/otagui/web_server.py b/tools/otagui/web_server.py index bffc46f77..e4ddb6188 100644 --- a/tools/otagui/web_server.py +++ b/tools/otagui/web_server.py @@ -64,22 +64,20 @@ class RequestHandler(CORSSimpleHTTPHandler): self.end_headers() def do_GET(self): - if self.path.startswith('/check'): - if self.path == '/check' or self.path == '/check/': - statuses = jobs.get_status() - self._set_response(type='application/json') - self.wfile.write( - json.dumps([status.to_dict_basic() - for status in statuses]).encode() - ) - else: - id = self.path[7:] - status = jobs.get_status_by_ID(id=id) - self._set_response(type='application/json') - self.wfile.write( - json.dumps(status.to_dict_detail(target_lib)).encode() - ) - return + if self.path == '/check' or self.path == '/check/': + statuses = jobs.get_status() + self._set_response(type='application/json') + self.wfile.write( + json.dumps([status.to_dict_basic() + for status in statuses]).encode() + ) + elif self.path.startswith('/check/'): + id = self.path[7:] + status = jobs.get_status_by_ID(id=id) + self._set_response(type='application/json') + self.wfile.write( + json.dumps(status.to_dict_detail(target_lib)).encode() + ) elif self.path.startswith('/file'): if self.path == '/file' or self.path == '/file/': file_list = target_lib.get_builds() @@ -99,7 +97,11 @@ class RequestHandler(CORSSimpleHTTPHandler): self.path = self.path[10:] return CORSSimpleHTTPHandler.do_GET(self) else: - self.path = '/dist' + self.path + if not os.path.exists('dist' + self.path): + logging.info('redirect to dist') + self.path = '/dist/' + else: + self.path = '/dist' + self.path return CORSSimpleHTTPHandler.do_GET(self) def do_POST(self):