@@ -19,44 +19,35 @@ function loadNested (path, qs, file, next, vm, first) {
1919
2020export function fetchMixin ( proto ) {
2121 let last
22- proto . _fetch = function ( cb = noop ) {
23- const { path, query } = this . route
24- const qs = stringifyQuery ( query , [ 'id' ] )
25- const { loadNavbar, loadSidebar, requestHeaders, fallbackLanguages } = this . config
26- // Abort last request
27- last && last . abort && last . abort ( )
28-
29- const file = this . router . getFile ( path )
30- last = get ( file + qs , true , requestHeaders )
31-
32- // Current page is html
33- this . isHTML = / \. h t m l $ / g. test ( file )
34-
35- const getFallBackPage = ( file ) => {
36- if ( ! fallbackLanguages ) {
37- return false
38- }
3922
40- const local = file . split ( '/' ) [ 1 ]
41-
42- if ( fallbackLanguages . indexOf ( local ) === - 1 ) {
43- return false
44- }
45-
46- file = file . replace ( new RegExp ( `^/${ local } ` ) , '' )
23+ const abort = ( ) => last && last . abort && last . abort ( )
24+ const request = ( url , hasbar , requestHeaders ) => {
25+ abort ( )
26+ last = get ( url , true , requestHeaders )
27+ return last
28+ }
4729
48- return get ( file + qs , true , requestHeaders )
49- . then (
50- ( text , opt ) => {
51- this . _renderMain ( text , opt , loadSideAndNav )
52- } ,
53- _ => {
54- return this . _renderMain ( null , { } , loadSideAndNav )
55- }
56- )
30+ const get404Path = ( path , config ) => {
31+ const { notFoundPage, ext } = config
32+ const defaultPath = '_404' + ( ext || '.md' )
33+
34+ switch ( typeof notFoundPage ) {
35+ case 'boolean' :
36+ return defaultPath
37+ case 'string' :
38+ return notFoundPage
39+ case 'object' :
40+ const key = Object
41+ . keys ( notFoundPage )
42+ . sort ( ( a , b ) => b . length - a . length )
43+ . find ( ( key ) => path . match ( new RegExp ( '^' + key ) ) )
44+
45+ return key && notFoundPage [ key ] || defaultPath
5746 }
47+ }
5848
59- const loadSideAndNav = ( ) => {
49+ proto . _loadSideAndNav = function ( path , qs , loadSidebar , cb ) {
50+ return ( ) => {
6051 if ( ! loadSidebar ) return cb ( )
6152
6253 const fn = result => {
@@ -67,28 +58,39 @@ export function fetchMixin (proto) {
6758 // Load sidebar
6859 loadNested ( path , qs , loadSidebar , fn , this , true )
6960 }
61+ }
62+
63+ proto . _fetch = function ( cb = noop ) {
64+ const { path, query } = this . route
65+ const qs = stringifyQuery ( query , [ 'id' ] )
66+ const { loadNavbar, requestHeaders, loadSidebar } = this . config
67+ // Abort last request
68+
69+ const file = this . router . getFile ( path )
70+ const req = request ( file + qs , true , requestHeaders )
71+
72+ // Current page is html
73+ this . isHTML = / \. h t m l $ / g. test ( file )
7074
7175 // Load main content
72- last
76+ req
7377 . then (
74- ( text , opt ) => {
75- this . _renderMain ( text , opt , loadSideAndNav )
76- } ,
78+ ( text , opt ) => this . _renderMain ( text , opt , this . _loadSideAndNav ( path , qs , loadSidebar , cb ) ) ,
7779 _ => {
78- return getFallBackPage ( file ) || this . _renderMain ( null , { } , loadSideAndNav )
80+ this . _fetchFallbackPage ( file , qs , cb ) || this . _fetch404 ( file , qs , cb )
7981 }
8082 )
8183
8284 // Load nav
8385 loadNavbar &&
84- loadNested (
85- path ,
86- qs ,
87- loadNavbar ,
88- text => this . _renderNav ( text ) ,
89- this ,
90- true
91- )
86+ loadNested (
87+ path ,
88+ qs ,
89+ loadNavbar ,
90+ text => this . _renderNav ( text ) ,
91+ this ,
92+ true
93+ )
9294 }
9395
9496 proto . _fetchCover = function ( ) {
@@ -141,6 +143,54 @@ export function fetchMixin (proto) {
141143 } )
142144 }
143145 }
146+
147+ proto . _fetchFallbackPage = function ( path , qs , cb = noop ) {
148+ const { requestHeaders, fallbackLanguages, loadSidebar } = this . config
149+
150+ if ( ! fallbackLanguages ) {
151+ return false
152+ }
153+
154+ const local = path . split ( '/' ) [ 1 ]
155+
156+ if ( fallbackLanguages . indexOf ( local ) === - 1 ) {
157+ return false
158+ }
159+ const newPath = path . replace ( new RegExp ( `^/${ local } ` ) , '' )
160+ const req = request ( newPath + qs , true , requestHeaders )
161+
162+ req . then (
163+ ( text , opt ) => this . _renderMain ( text , opt , this . _loadSideAndNav ( path , qs , loadSidebar , cb ) ) ,
164+ ( ) => this . _fetch404 ( path , qs , cb )
165+ )
166+
167+ return true
168+ }
169+ /**
170+ * Load the 404 page
171+ * @param path
172+ * @param qs
173+ * @param cb
174+ * @returns {* }
175+ * @private
176+ */
177+ proto . _fetch404 = function ( path , qs , cb = noop ) {
178+ const { loadSidebar, requestHeaders, notFoundPage } = this . config
179+
180+ const fnLoadSideAndNav = this . _loadSideAndNav ( path , qs , loadSidebar , cb )
181+
182+ if ( notFoundPage ) {
183+ request ( get404Path ( path , this . config ) , true , requestHeaders )
184+ . then (
185+ ( text , opt ) => this . _renderMain ( text , opt , fnLoadSideAndNav ) ,
186+ ( ) => this . _renderMain ( null , { } , fnLoadSideAndNav )
187+ )
188+ return true
189+ }
190+
191+ this . _renderMain ( null , { } , fnLoadSideAndNav )
192+ return false
193+ }
144194}
145195
146196export function initFetch ( vm ) {
0 commit comments