-
Notifications
You must be signed in to change notification settings - Fork 921
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: implement custom routes (#1447)
Co-authored-by: Xinyu Liu <[email protected]> BREAKING CHANGE: vue-router's route records have been replaced by custom route records to get better performance. It should not break common usage, but could be a potential breaking change for some themes. vue-router is suitable for SPAs, but not for static sites. It has a negative impact on the performance of vuepress sites, especially large-scale ones. In the long run we'll replace vue-router with a light-weighted custom router totally.
- Loading branch information
1 parent
5281a42
commit 7d37350
Showing
65 changed files
with
703 additions
and
543 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
routeMeta: | ||
foo: bar | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
routeMeta: | ||
a: 0 | ||
c: 3 | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
## resolve | ||
|
||
### Path | ||
|
||
#### Index | ||
|
||
- Clean URL: {{ JSON.stringify(resolveRoute('/')) }} | ||
- HTML: {{ JSON.stringify(resolveRoute('/index.html')) }} | ||
- Markdown: {{ JSON.stringify(resolveRoute('/README.md')) }} | ||
|
||
#### Non-Index | ||
|
||
- Clean URL: {{ JSON.stringify(resolveRoute('/router/resolve-route')) }} | ||
- HTML: {{ JSON.stringify(resolveRoute('/router/resolve-route.html')) }} | ||
- Markdown: {{ JSON.stringify(resolveRoute('/router/resolve-route.md')) }} | ||
|
||
#### Non-ASCII | ||
|
||
- Clean URL: {{ JSON.stringify(resolveRoute('/routes/non-ascii-paths/中文目录名/中文文件名')) }} | ||
- HTML: {{ JSON.stringify(resolveRoute('/routes/non-ascii-paths/中文目录名/中文文件名.html')) }} | ||
- Markdown: {{ JSON.stringify(resolveRoute('/routes/non-ascii-paths/中文目录名/中文文件名.md')) }} | ||
|
||
#### Non-ASCII Encoded | ||
|
||
- Clean URL: {{ JSON.stringify(resolveRoute(encodeURI('/routes/non-ascii-paths/中文目录名/中文文件名'))) }} | ||
- HTML: {{ JSON.stringify(resolveRoute(encodeURI('/routes/non-ascii-paths/中文目录名/中文文件名.html'))) }} | ||
- Markdown: {{ JSON.stringify(resolveRoute(encodeURI('/routes/non-ascii-paths/中文目录名/中文文件名.md'))) }} | ||
|
||
#### Non-Existent | ||
|
||
- Clean URL: {{ JSON.stringify(resolveRoute('/non-existent')) }} | ||
- HTML: {{ JSON.stringify(resolveRoute('/non-existent.html')) }} | ||
- Markdown: {{ JSON.stringify(resolveRoute('/non-existent.md')) }} | ||
|
||
#### Route Meta | ||
|
||
- Clean URL: {{ JSON.stringify(resolveRoute('/page-data/route-meta')) }} | ||
- HTML: {{ JSON.stringify(resolveRoute('/page-data/route-meta.html')) }} | ||
- Markdown: {{ JSON.stringify(resolveRoute('/page-data/route-meta.md')) }} | ||
|
||
<script setup> | ||
import { resolveRoute } from 'vuepress/client' | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const testCases = [ | ||
{ | ||
selector: '#index', | ||
expected: { | ||
path: '/', | ||
meta: {}, | ||
notFound: false, | ||
}, | ||
}, | ||
{ | ||
selector: '#non-index', | ||
expected: { | ||
path: '/router/resolve-route.html', | ||
meta: {}, | ||
notFound: false, | ||
}, | ||
}, | ||
{ | ||
selector: '#non-ascii', | ||
expected: { | ||
path: encodeURI('/routes/non-ascii-paths/中文目录名/中文文件名.html'), | ||
meta: {}, | ||
notFound: false, | ||
}, | ||
}, | ||
{ | ||
selector: '#non-ascii-encoded', | ||
expected: { | ||
path: encodeURI('/routes/non-ascii-paths/中文目录名/中文文件名.html'), | ||
meta: {}, | ||
notFound: false, | ||
}, | ||
}, | ||
{ | ||
selector: '#non-existent', | ||
expected: { | ||
path: '/non-existent.html', | ||
meta: { foo: 'bar' }, | ||
notFound: true, | ||
}, | ||
}, | ||
{ | ||
selector: '#route-meta', | ||
expected: { | ||
path: '/page-data/route-meta.html', | ||
meta: { a: 0, b: 2, c: 3 }, | ||
notFound: false, | ||
}, | ||
}, | ||
] | ||
|
||
const parseResolvedRouteFromElement = (el: Cypress.JQueryWithSelector) => | ||
JSON.parse(/: (\{.*\})\s*$/.exec(el.text())![1]) | ||
|
||
it('should resolve routes correctly', () => { | ||
cy.visit('/router/resolve-route.html') | ||
|
||
testCases.forEach(({ selector, expected }) => { | ||
cy.get(`.e2e-theme-content ${selector} + ul > li`).each((el) => { | ||
const resolvedRoute = parseResolvedRouteFromElement(el) | ||
expect(resolvedRoute.path).to.equal(expected.path) | ||
expect(resolvedRoute.meta).to.deep.equal(expected.meta) | ||
expect(resolvedRoute.notFound).to.equal(expected.notFound) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { h } from 'vue' | ||
import type { FunctionalComponent, VNode } from 'vue' | ||
import { useRouter } from 'vue-router' | ||
import { withBase } from '../helpers/index.js' | ||
import { resolveRoutePath } from '../router/index.js' | ||
|
||
/** | ||
* Forked from https://github.com/vuejs/router/blob/941b2131e80550009e5221d4db9f366b1fea3fd5/packages/router/src/RouterLink.ts#L293 | ||
*/ | ||
const guardEvent = (event: MouseEvent): boolean | void => { | ||
// don't redirect with control keys | ||
if (event.metaKey || event.altKey || event.ctrlKey || event.shiftKey) return | ||
// don't redirect when preventDefault called | ||
if (event.defaultPrevented) return | ||
// don't redirect on right click | ||
if (event.button !== undefined && event.button !== 0) return | ||
// don't redirect if `target="_blank"` | ||
if (event.currentTarget) { | ||
const target = (event.currentTarget as HTMLElement).getAttribute('target') | ||
if (target?.match(/\b_blank\b/i)) return | ||
} | ||
event.preventDefault() | ||
return true | ||
} | ||
|
||
export interface VPLinkProps { | ||
to: string | ||
} | ||
|
||
export const VPLink: FunctionalComponent< | ||
VPLinkProps, | ||
Record<never, never>, | ||
{ | ||
default: () => string | VNode | (string | VNode)[] | ||
} | ||
> = ({ to = '' }, { slots }) => { | ||
const router = useRouter() | ||
const path = withBase(resolveRoutePath(to)) | ||
|
||
return h( | ||
'a', | ||
{ | ||
class: 'vp-link', | ||
href: path, | ||
onClick: (event: MouseEvent = {} as MouseEvent) => { | ||
guardEvent(event) ? router.push(to).catch() : Promise.resolve() | ||
}, | ||
}, | ||
slots.default?.(), | ||
) | ||
} | ||
|
||
VPLink.displayName = 'VPLink' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './ClientOnly.js' | ||
export * from './Content.js' | ||
export * from './VPLink.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.