Skip to content

Commit

Permalink
actually fix redirect catch url
Browse files Browse the repository at this point in the history
  • Loading branch information
chaitanyapotti committed Sep 1, 2022
1 parent 79f0b79 commit af712ff
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
32 changes: 22 additions & 10 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable prefer-regex-literals */
/* eslint-disable import/no-extraneous-dependencies */
const express = require('express')
const send = require('send')
Expand All @@ -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)
})

Expand Down
10 changes: 9 additions & 1 deletion src/views/RedirectCatch/RedirectCatch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit af712ff

Please sign in to comment.