From af712ff22e0e9968e987881b2e776b24da0912a7 Mon Sep 17 00:00:00 2001 From: chaitanyapotti Date: Thu, 1 Sep 2022 18:10:05 +0800 Subject: [PATCH] actually fix redirect catch url --- server.js | 32 ++++++++++++++++------- src/views/RedirectCatch/RedirectCatch.vue | 10 ++++++- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/server.js b/server.js index 0eca0e089..9d29b6843 100644 --- a/server.js +++ b/server.js @@ -1,3 +1,4 @@ +/* eslint-disable prefer-regex-literals */ /* eslint-disable import/no-extraneous-dependencies */ const express = require('express') const send = require('send') @@ -8,20 +9,31 @@ const path = require('path') log.enableAll() const app = express() -const fileExtensionRegexp = /[^/?]+\\.[^/]+$/ - const versionRegex = /^\/v\d+\.\d+\.\d+\// app.use('*', (req, res) => { - let finalPath = req.params[0].replace(versionRegex, '') - log.info('path', finalPath, req.params[0]) - if (!finalPath.match(fileExtensionRegexp)) { - finalPath = 'index.html' + const olduri = req.params[0] || '' + let newuri = '' + if (new RegExp(/^\/v\d+\.\d+\.\d+\/.+\.(js|css|png|PNG|svg|html|jpg|JPG|jpeg|JPEG|JSON|json|txt|gif)$/).test(olduri)) { + newuri = olduri.replace(versionRegex, '') + } else if (new RegExp(/^\/v\d+\.\d+\.\d+\/[^.]*$/).test(olduri)) { + const secondIndex = olduri.indexOf('/', 1) + newuri = `${olduri.slice(0, secondIndex)}/index.html` + } else if (new RegExp(/^\/v\d+\.\d+\.\d+\/?$/).test(olduri)) { + const secondIndex = olduri.indexOf('/', 1) + const slicedOrignal = secondIndex === -1 ? olduri : olduri.slice(0, secondIndex) + newuri = `${slicedOrignal}/index.html` + } else if (new RegExp(/^\/.+\.(js|css|png|PNG|svg|html|jpg|JPG|jpeg|JPEG|JSON|json|txt|gif)$/).test(olduri)) { + newuri = olduri + } else if (new RegExp(/^\/[^.]*$/).test(olduri)) { + newuri = '/index.html' + } else { + newuri = '/index.html' } - log.info('path2', finalPath, req.params[0]) - const dirPath = path.resolve(path.join(__dirname, 'dist', finalPath)) - log.info('dir', dirPath) - if (!fs.existsSync(dirPath)) return res.status(404).text('not found') + + const dirPath = path.resolve(path.join(__dirname, 'dist', newuri)) + log.info('req ->', req.params[0], '->', dirPath) + if (!fs.existsSync(dirPath)) return res.status(404).send('not found') return send(req, dirPath).pipe(res) }) diff --git a/src/views/RedirectCatch/RedirectCatch.vue b/src/views/RedirectCatch/RedirectCatch.vue index 2a07cd4a0..096ba9809 100644 --- a/src/views/RedirectCatch/RedirectCatch.vue +++ b/src/views/RedirectCatch/RedirectCatch.vue @@ -73,7 +73,15 @@ export default { bc.addEventListener('message', (ev) => { const { preopenInstanceId: oldId, payload, message } = ev.data if (oldId === queryParameters.preopenInstanceId && payload?.url) { - window.location.href = payload.url + const url = new URL(payload.url) + // if same origin, use router.push + if (url.origin === window.location.origin) { + const matchedRoute = this.$router.match(url.pathname.replace(/^\/v\d+\.\d+\.\d+\//, '')) + const query = Object.fromEntries(new URLSearchParams(url.search)) + this.$router.push({ query, hash: url.hash, name: matchedRoute.name }) + } else { + window.location.href = payload.url + } } else if (oldId === queryParameters.preopenInstanceId && message === 'setup_complete') { bc.postMessage({ data: {