提交 f4cf3b40 编写于 作者: E Evan You

refactor: avoid loading bar if page has been loaded

上级 33624f31
import { pathToComponentName, getTitle, getLang } from './util'
export default {
created () {
if (this.$ssrContext) {
this.$ssrContext.title = getTitle(this)
this.$ssrContext.lang = getLang(this)
this.$ssrContext.title = getTitle(this.$page)
this.$ssrContext.lang = getLang(this.$page)
}
},
mounted () {
let currentMetaTags
beforeMount () {
this.currentMetaTags = []
const updateMeta = () => {
document.title = getTitle(this)
document.documentElement.lang = getLang(this)
currentMetaTags = updateMetaTags(this, currentMetaTags)
document.title = getTitle(this.$page)
document.documentElement.lang = getLang(this.$page)
this.currentMetaTags = updateMetaTags(this.$page, this.currentMetaTags)
}
this.$watch('$page', updateMeta)
updateMeta()
},
beforeDestroy () {
updateMetaTags(null, this.currentMetaTags)
},
render (h) {
return h(pathToComponentName(this.$page.path))
}
}
function pathToComponentName (path) {
if (path.charAt(path.length - 1) === '/') {
return `page${path.replace(/\//g, '-') + 'index'}`
} else {
return `page${path.replace(/\//g, '-').replace(/\.html$/, '')}`
}
}
function getTitle (vm) {
const selfTitle = (
vm.$page.frontmatter.title || // explicit title
vm.$page.title // inferred title
)
const siteTitle = vm.$site.title
return siteTitle
? selfTitle
? (siteTitle + ' | ' + selfTitle)
: siteTitle
: selfTitle || 'VuePress'
}
function getLang (vm) {
return vm.$page.frontmatter.lang || 'en'
}
function updateMetaTags (vm, current) {
function updateMetaTags (page, current) {
if (current) {
current.forEach(c => {
document.head.removeChild(c)
})
}
const data = vm.$page.frontmatter.meta
const data = page && page.frontmatter.meta
if (data) {
return data.map(m => {
const tag = document.createElement('meta')
......
import { siteData } from './.temp/siteData'
import { findPageForPath } from './util'
siteData.pages.forEach(page => {
if (!page.frontmatter) {
......@@ -12,13 +13,7 @@ export default {
return siteData
},
$page () {
const pages = siteData.pages
for (let i = 0; i < pages.length; i++) {
const page = pages[i]
if (page.path === this.$route.path) {
return page
}
}
return findPageForPath(this.$route.path)
}
}
}
import { siteData } from './.temp/siteData'
export function pathToComponentName (path) {
if (path.charAt(path.length - 1) === '/') {
return `page${path.replace(/\//g, '-') + 'index'}`
} else {
return `page${path.replace(/\//g, '-').replace(/\.html$/, '')}`
}
}
export function findPageForPath (path) {
const pages = siteData.pages
for (let i = 0; i < pages.length; i++) {
const page = pages[i]
if (page.path === path) {
return page
}
}
}
export function getTitle (page) {
const selfTitle = (
page.frontmatter.title || // explicit title
page.title // inferred title
)
const siteTitle = siteData.title
return siteTitle
? selfTitle
? (siteTitle + ' | ' + selfTitle)
: siteTitle
: selfTitle || 'VuePress'
}
export function getLang (page) {
return page.frontmatter.lang || 'en'
}
......@@ -7,10 +7,12 @@
</template>
<script>
import Vue from 'vue'
import nprogress from 'nprogress'
import Navbar from './Navbar.vue'
import Page from './Page.vue'
import Sidebar from './Sidebar.vue'
import { pathToComponentName } from '../app/util'
export default {
components: { Page, Sidebar, Navbar },
......@@ -18,7 +20,7 @@ export default {
nprogress.configure({ showSpinner: false })
this.$router.beforeEach((to, from, next) => {
if (to.path !== from.path) {
if (to.path !== from.path && !Vue.component(pathToComponentName(to.path))) {
nprogress.start()
}
next()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册