Skip to content

Commit 21d3cd8

Browse files
committed
feat: spa navigation
1 parent 4353126 commit 21d3cd8

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

lib/app/index.js

+29-15
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,38 @@ import {
66
inject,
77
watchEffect,
88
shallowRef
9-
} from 'vue'
9+
} from '/@modules/vue'
1010
import { Layout } from '/@theme/index.js'
1111

1212
const PathSymbol = Symbol()
1313

1414
const App = {
1515
setup() {
16-
const path = ref(window.location.pathname)
16+
const path = ref(location.pathname)
1717

18-
// window.addEventListener('click', e => {
19-
// if (e.target.tagName === 'A') {
20-
// e.preventDefault()
21-
// if (e.target.href && e.target.href.indexOf(location.host)) {
22-
// history.pushState(null, '', e.target.href)
23-
// }
24-
// }
25-
// })
18+
window.addEventListener(
19+
'click',
20+
(e) => {
21+
if (e.target.tagName === 'A') {
22+
const { href, target } = e.target
23+
if (
24+
target !== `_blank` &&
25+
href.startsWith(`${location.protocol}//${location.host}`)
26+
) {
27+
e.preventDefault()
28+
// TODO save scroll position
29+
history.pushState(null, '', href)
30+
path.value = location.pathname
31+
}
32+
}
33+
},
34+
{ capture: true }
35+
)
36+
37+
window.addEventListener('popstate', (e) => {
38+
// TODO restore scroll position
39+
path.value = location.pathname
40+
})
2641

2742
provide(PathSymbol, path)
2843

@@ -40,16 +55,15 @@ const Content = {
4055
watchEffect(() => {
4156
let pagePath = path.value.replace(/\.html$/, '')
4257
if (pagePath.endsWith('/')) {
43-
pagePath += 'index.md'
44-
} else {
45-
pagePath += '.md'
58+
pagePath += 'index'
4659
}
4760

48-
import(pagePath)
61+
// awlays force re-fetch content in dev
62+
import(`${pagePath}.md?t=${Date.now()}`)
4963
.then((m) => {
5064
comp.value = m.default
5165
})
52-
.catch(err => {
66+
.catch((err) => {
5367
comp.value = Default404
5468
})
5569
})

0 commit comments

Comments
 (0)