feat: spa navigation

pull/1/head
Evan You 5 years ago
parent 43531267bd
commit 21d3cd8cbe

@ -6,23 +6,38 @@ import {
inject, inject,
watchEffect, watchEffect,
shallowRef shallowRef
} from 'vue' } from '/@modules/vue'
import { Layout } from '/@theme/index.js' import { Layout } from '/@theme/index.js'
const PathSymbol = Symbol() const PathSymbol = Symbol()
const App = { const App = {
setup() { setup() {
const path = ref(window.location.pathname) const path = ref(location.pathname)
// window.addEventListener('click', e => { window.addEventListener(
// if (e.target.tagName === 'A') { 'click',
// e.preventDefault() (e) => {
// if (e.target.href && e.target.href.indexOf(location.host)) { if (e.target.tagName === 'A') {
// history.pushState(null, '', e.target.href) const { href, target } = e.target
// } if (
// } target !== `_blank` &&
// }) href.startsWith(`${location.protocol}//${location.host}`)
) {
e.preventDefault()
// TODO save scroll position
history.pushState(null, '', href)
path.value = location.pathname
}
}
},
{ capture: true }
)
window.addEventListener('popstate', (e) => {
// TODO restore scroll position
path.value = location.pathname
})
provide(PathSymbol, path) provide(PathSymbol, path)
@ -40,16 +55,15 @@ const Content = {
watchEffect(() => { watchEffect(() => {
let pagePath = path.value.replace(/\.html$/, '') let pagePath = path.value.replace(/\.html$/, '')
if (pagePath.endsWith('/')) { if (pagePath.endsWith('/')) {
pagePath += 'index.md' pagePath += 'index'
} else {
pagePath += '.md'
} }
import(pagePath) // awlays force re-fetch content in dev
import(`${pagePath}.md?t=${Date.now()}`)
.then((m) => { .then((m) => {
comp.value = m.default comp.value = m.default
}) })
.catch(err => { .catch((err) => {
comp.value = Default404 comp.value = Default404
}) })
}) })

Loading…
Cancel
Save