-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirectHandler.js
62 lines (49 loc) · 1.78 KB
/
redirectHandler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import * as url from "url";
const redirectHandler = app => {
let appRoute = (() => {
let queryParams = url.parse(window.location.href, true).query
return {
get: () => queryParams.route?.toString(),
set: route => {
const location = window.location
location.assign(
location.href.split('?')[0] + '?route=' + route
)
}
}
})()
const init = () => {
setRoute()
app.$options.netlify = true
app.$options.updated = () => {
app.$options.updated()
setRoute()
handleInternalLinks()
};
['load', 'click'].forEach(event => window.addEventListener(event, handleInternalLinks))
}
const handleInternalLinks = () => {
const allLinks = [...document.querySelectorAll('a[href]')]
const internalLinks = allLinks.filter(link => {
const url = link.getAttribute('href')
return !url.startsWith('http')/* || url.includes(window.location.host)*/
})
internalLinks.forEach(link => {
const query = link.getAttribute('href')
link.addEventListener('click', () => {
redirect(query)
})
link.removeAttribute('href')
})
}
const redirect = (destination) => appRoute.set(destination)
const setRoute = () => {
if (!appRoute.get()) return;
const routeName = app.$router.resolve(appRoute.get()).route.name
app.$options.components.ComponentView =
app.$router.options.routes.find(route => route.name === routeName)?.component
app.$forceUpdate()
}
return {init}
}
export default vueComponent => redirectHandler(vueComponent).init()