Skip to content

Commit

Permalink
fix: use <a> if the page doesn't exist
Browse files Browse the repository at this point in the history
fix #506
  • Loading branch information
egoist committed Oct 6, 2019
1 parent 1cc0757 commit 8e3daa9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
13 changes: 11 additions & 2 deletions packages/saber/vue-renderer/app/components/SaberLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default {
render(h, { data, children, parent }) {
const attrs = { ...data.attrs }
const isExternal = typeof attrs.to === 'string' && isAbsoluteUrl(attrs.to)
let tag = 'a'

if (isExternal) {
if (HTTP_RE.test(attrs.to)) {
Expand All @@ -29,8 +30,16 @@ export default {
delete attrs.to
} else {
if (typeof attrs.to === 'string') {
attrs.to = parent.$saber.getPageLink(attrs.to)
const link = parent.$saber.getPageLink(attrs.to)
if (link) {
tag = 'router-link'
attrs.to = link
} else {
attrs.href = attrs.to
delete attrs.to
}
} else {
tag = 'router-link'
const { route } = parent.$router.resolve(attrs.to)
attrs.to = parent.$saber.getPageLink(route.fullPath)
}
Expand All @@ -39,7 +48,7 @@ export default {
delete attrs.openLinkInNewTab

return h(
isExternal ? 'a' : 'router-link',
tag,
{
...data,
attrs
Expand Down
13 changes: 6 additions & 7 deletions packages/saber/vue-renderer/app/create-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@ export default context => {
throw new TypeError(`Expect link to be a string`)
}

// Already a route path, directly return
if (/^\//.test(link)) {
return link
}

const matched = /^([^#?]+)([#?].*)?$/.exec(link)

if (!matched) {
Expand All @@ -145,6 +140,9 @@ export default context => {
)
const extra = matched[2] || ''
for (const route of this.$router.options.routes) {
if (route.path === matched[1]) {
return link
}
if (
route.meta &&
route.meta.__relative &&
Expand All @@ -153,8 +151,9 @@ export default context => {
return `${route.path}${extra}`
}
}
// Not a page, return the whole link directly
return `${matched[1]}${extra}`

// Not a page
return undefined
}
}
}
Expand Down

0 comments on commit 8e3daa9

Please sign in to comment.