Skip to content

Commit 039df8c

Browse files
committed
Fix 'abstract' router for SSR in both browser and server-side
1 parent 20e0066 commit 039df8c

File tree

4 files changed

+44
-17
lines changed

4 files changed

+44
-17
lines changed

src/core/render/embed.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function walkFetchEmbed({ embedTokens, compile, fetch }, cb) {
3838

3939
// This may contain YAML front matter and will need to be stripped.
4040
const frontMatterInstalled =
41-
($docsify.frontMatter || {}).installed || false;
41+
(((global || window || {}).$docsify || {}).frontMatter || {}).installed || false;
4242
if (frontMatterInstalled === true) {
4343
text = $docsify.frontMatter.parseMarkdown(text);
4444
}

src/core/router/history/abstract.js

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,52 @@
1+
import { noop } from '../../util/core';
2+
import { inBrowser } from '../../util/env';
3+
import { on } from '../../util/dom';
14
import { parseQuery } from '../util';
25
import { History } from './base';
6+
import { HashHistory } from './hash';
7+
import { HTML5History } from './html5';
38

49
export class AbstractHistory extends History {
510
constructor(config) {
611
super(config);
7-
this.mode = 'abstract';
12+
this.mode = this.config.routerMode;
13+
}
14+
15+
onchange(cb = noop) {
16+
if(inBrowser) {
17+
on('hashchange', e => {
18+
let hash = e.target.location.hash;
19+
let parsed = this.parse(hash);
20+
let path = parsed.path;
21+
22+
window.open(path, '_self');
23+
});
24+
}
825
}
926

1027
parse(path = '') {
11-
let query = '';
28+
if(this.mode === 'history') {
29+
let query = '';
30+
31+
const queryIndex = path.indexOf('?');
32+
if (queryIndex >= 0) {
33+
query = path.slice(queryIndex + 1);
34+
path = path.slice(0, queryIndex);
35+
}
1236

13-
const queryIndex = path.indexOf('?');
14-
if (queryIndex >= 0) {
15-
query = path.slice(queryIndex + 1);
16-
path = path.slice(0, queryIndex);
37+
return {
38+
path,
39+
file: this.getFile(path),
40+
query: parseQuery(query),
41+
};
42+
} else {
43+
return HashHistory.prototype.parse.bind(this)(path);
1744
}
45+
}
1846

19-
return {
20-
path,
21-
file: this.getFile(path),
22-
query: parseQuery(query),
23-
};
47+
toURL(path, params, currentRoute = '') {
48+
return this.mode === 'history' ?
49+
HTML5History.prototype.toURL.bind(this)(path, params, currentRoute) :
50+
HashHistory.prototype.toURL.bind(this)(path, params, currentRoute);
2451
}
2552
}

src/core/router/history/base.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class History {
6565

6666
parse() {}
6767

68-
toURL(path, params, currentRoute = '') {
68+
toURL(path = '', params = {}, currentRoute = '') {
6969
const local = currentRoute && path[0] === '#';
7070
const route = this.parse(replaceSlug(path));
7171

src/core/router/util.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ export function stringifyQuery(obj, ignores = []) {
4040
return qs.length ? `?${qs.join('&')}` : '';
4141
}
4242

43-
export const isAbsolutePath = cached(path => {
43+
export const isAbsolutePath = cached((path = '') => {
4444
return /(:|(\/{2}))/g.test(path);
4545
});
4646

47-
export const removeParams = cached(path => {
47+
export const removeParams = cached((path = '') => {
4848
return path.split(/[?#]/)[0];
4949
});
5050

@@ -57,11 +57,11 @@ export const getParentPath = cached((path = '') => {
5757
return matchingParts ? matchingParts[1] : '';
5858
});
5959

60-
export const cleanPath = cached(path => {
60+
export const cleanPath = cached((path = '') => {
6161
return path.replace(/^\/+/, '/').replace(/([^:])\/{2,}/g, '$1/');
6262
});
6363

64-
export const resolvePath = cached(path => {
64+
export const resolvePath = cached((path = '') => {
6565
const segments = path.replace(/^\//, '').split('/');
6666
let resolved = [];
6767
for (let i = 0, len = segments.length; i < len; i++) {

0 commit comments

Comments
 (0)