@@ -5,16 +5,10 @@ import fetch from 'unfetch';
5
5
import middleware from '../config/middleware.js' ;
6
6
import {
7
7
middlewareSeries ,
8
- sanitizeComponent ,
9
- resolveRouteComponents ,
10
8
getMatchedComponents ,
11
- getMatchedComponentsInstances ,
12
9
flatMapComponents ,
13
10
setContext ,
14
- compile ,
15
- getQueryDiff ,
16
11
globalHandleError ,
17
- isSamePath ,
18
12
urlJoin
19
13
} from '../utils/nuxt.js' ;
20
14
import { createApp } from './index.js' ;
@@ -40,12 +34,9 @@ if (!global.fetch) {
40
34
}
41
35
42
36
// Global shared references
43
- let _lastPaths = [ ] ;
44
37
let app ;
45
38
let router ;
46
39
47
- const NUXT = { } ;
48
-
49
40
const $config = nuxt . publicRuntimeConfig || { } ; // eslint-disable-line no-undef
50
41
51
42
if ( $config . _app ) {
@@ -98,81 +89,6 @@ const errorHandler = Vue.config.errorHandler || console.error; // eslint-disable
98
89
// Create and mount App
99
90
createApp ( nuxt . publicRuntimeConfig ) . then ( mountApp ) . catch ( errorHandler ) ; // eslint-disable-line no-undef
100
91
101
- async function loadAsyncComponents ( to , from , next ) {
102
- // Check if route changed (this._routeChanged), only if the page is not an error (for validate())
103
- this . _routeChanged = Boolean ( app . nuxt . err ) || from . name !== to . name ;
104
- this . _paramChanged = ! this . _routeChanged && from . path !== to . path ;
105
- this . _queryChanged = ! this . _paramChanged && from . fullPath !== to . fullPath ;
106
- this . _diffQuery = ( this . _queryChanged ? getQueryDiff ( to . query , from . query ) : [ ] ) ;
107
-
108
- if ( ( this . _routeChanged || this . _paramChanged ) && this . $loading . start && ! this . $loading . manual ) {
109
- this . $loading . start ( ) ;
110
- }
111
-
112
- try {
113
- if ( this . _queryChanged ) {
114
- const Components = await resolveRouteComponents (
115
- to ,
116
- ( Component , instance ) => ( { Component, instance } )
117
- ) ;
118
- // Add a marker on each component that it needs to refresh or not
119
- const startLoader = Components . some ( ( { Component, instance } ) => {
120
- const watchQuery = Component . options . watchQuery ;
121
-
122
- if ( watchQuery === true ) {
123
- return true ;
124
- }
125
- if ( Array . isArray ( watchQuery ) ) {
126
- return watchQuery . some ( ( key ) => this . _diffQuery [ key ] ) ;
127
- }
128
- if ( typeof watchQuery === 'function' ) {
129
- return watchQuery . apply ( instance , [ to . query , from . query ] ) ;
130
- }
131
-
132
- return false ;
133
- } ) ;
134
-
135
- if ( startLoader && this . $loading . start && ! this . $loading . manual ) {
136
- this . $loading . start ( ) ;
137
- }
138
- }
139
- // Call next()
140
- next ( ) ;
141
- } catch ( error ) {
142
- const err = error || { } ;
143
- const statusCode = err . statusCode || err . status || ( err . response && err . response . status ) || 500 ;
144
- const message = err . message || '' ;
145
-
146
- // Handle chunk loading errors
147
- // This may be due to a new deployment or a network problem
148
- if ( / ^ L o a d i n g ( C S S ) ? c h u n k ( \d ) + f a i l e d \. / . test ( message ) ) {
149
- window . location . reload ( true /* skip cache */ ) ;
150
-
151
- return ; // prevent error page blinking for user
152
- }
153
-
154
- this . error ( { statusCode, message } ) ;
155
- next ( ) ;
156
- }
157
- }
158
-
159
- // Get matched components
160
- function resolveComponents ( route ) {
161
- return flatMapComponents ( route , async ( Component , _ , match , key , index ) => {
162
- // If component is not resolved yet, resolve it
163
- if ( typeof Component === 'function' && ! Component . options ) {
164
- Component = await Component ( ) ;
165
- }
166
-
167
- // Sanitize it and save it
168
- Component . _Ctor = sanitizeComponent ( Component ) ;
169
-
170
- match . components [ key ] = Component ;
171
-
172
- return Component ;
173
- } ) ;
174
- }
175
-
176
92
function callMiddleware ( Components , context ) {
177
93
let midd = [ 'i18n' ] ;
178
94
let unknownMiddleware = false ;
@@ -207,16 +123,6 @@ async function render(to, from, next) {
207
123
return next ( ) ;
208
124
}
209
125
210
- if ( to === from ) {
211
- _lastPaths = [ ] ;
212
- } else {
213
- const fromMatches = [ ] ;
214
-
215
- _lastPaths = getMatchedComponents ( from , fromMatches ) . map ( ( Component , i ) => {
216
- return compile ( from . matched [ fromMatches [ i ] ] . path ) ( from . params ) ;
217
- } ) ;
218
- }
219
-
220
126
// nextCalled is true when redirected
221
127
let nextCalled = false ;
222
128
const _next = ( path ) => {
@@ -269,13 +175,6 @@ async function render(to, from, next) {
269
175
return next ( ) ;
270
176
}
271
177
272
- // Update ._data and other properties if hot reloaded
273
- Components . forEach ( ( Component ) => {
274
- if ( Component . _Ctor && Component . _Ctor . options ) {
275
- Component . options . fetch = Component . _Ctor . options . fetch ;
276
- }
277
- } ) ;
278
-
279
178
try {
280
179
// Call middleware
281
180
await callMiddleware . call ( this , Components , app . context ) ;
@@ -327,70 +226,6 @@ async function render(to, from, next) {
327
226
return next ( ) ;
328
227
}
329
228
330
- let instances ;
331
-
332
- // Call fetch hooks on components matched by the route.
333
- await Promise . all ( Components . map ( ( Component , i ) => {
334
- // Check if only children route changed
335
- Component . _path = compile ( to . matched [ matches [ i ] ] . path ) ( to . params ) ;
336
- Component . _dataRefresh = false ;
337
- const childPathChanged = Component . _path !== _lastPaths [ i ] ;
338
-
339
- // Refresh component (call fetch) when:
340
- // Route path changed part includes current component
341
- // Or route param changed part includes current component and watchParam is not `false`
342
- // Or route query is changed and watchQuery returns `true`
343
- if ( this . _routeChanged && childPathChanged ) {
344
- Component . _dataRefresh = true ;
345
- } else if ( this . _paramChanged && childPathChanged ) {
346
- const watchParam = Component . options . watchParam ;
347
-
348
- Component . _dataRefresh = watchParam !== false ;
349
- } else if ( this . _queryChanged ) {
350
- const watchQuery = Component . options . watchQuery ;
351
-
352
- if ( watchQuery === true ) {
353
- Component . _dataRefresh = true ;
354
- } else if ( Array . isArray ( watchQuery ) ) {
355
- Component . _dataRefresh = watchQuery . some ( ( key ) => this . _diffQuery [ key ] ) ;
356
- } else if ( typeof watchQuery === 'function' ) {
357
- if ( ! instances ) {
358
- instances = getMatchedComponentsInstances ( to ) ;
359
- }
360
- Component . _dataRefresh = watchQuery . apply ( instances [ i ] , [ to . query , from . query ] ) ;
361
- }
362
- }
363
- if ( ! this . _hadError && this . _isMounted && ! Component . _dataRefresh ) {
364
- return ;
365
- }
366
-
367
- const promises = [ ] ;
368
-
369
- const hasFetch = Boolean ( Component . options . fetch ) && Component . options . fetch . length ;
370
-
371
- const loadingIncrease = hasFetch ? 30 : 45 ;
372
-
373
- // Check disabled page loading
374
- this . $loading . manual = Component . options . loading === false ;
375
-
376
- // Call fetch(context)
377
- if ( hasFetch ) {
378
- let p = Component . options . fetch ( app . context ) ;
379
-
380
- if ( ! p || ( ! ( p instanceof Promise ) && ( typeof p . then !== 'function' ) ) ) {
381
- p = Promise . resolve ( p ) ;
382
- }
383
- p . then ( ( fetchResult ) => {
384
- if ( this . $loading . increase ) {
385
- this . $loading . increase ( loadingIncrease ) ;
386
- }
387
- } ) ;
388
- promises . push ( p ) ;
389
- }
390
-
391
- return Promise . all ( promises ) ;
392
- } ) ) ;
393
-
394
229
// If not redirected
395
230
if ( ! nextCalled ) {
396
231
if ( this . $loading . finish && ! this . $loading . manual ) {
@@ -402,8 +237,6 @@ async function render(to, from, next) {
402
237
} catch ( err ) {
403
238
const error = err || { } ;
404
239
405
- _lastPaths = [ ] ;
406
-
407
240
globalHandleError ( error ) ;
408
241
409
242
this . error ( error ) ;
@@ -432,52 +265,6 @@ function checkForErrors(app) {
432
265
}
433
266
}
434
267
435
- // When navigating on a different route but the same component is used, Vue.js
436
- // Will not update the instance data, so we have to update $data ourselves
437
- function fixPrepatch ( to , ___ ) {
438
- if ( this . _routeChanged === false && this . _paramChanged === false && this . _queryChanged === false ) {
439
- return ;
440
- }
441
-
442
- const instances = getMatchedComponentsInstances ( to ) ;
443
- const Components = getMatchedComponents ( to ) ;
444
-
445
- Vue . nextTick ( ( ) => {
446
- instances . forEach ( ( instance , i ) => {
447
- if ( ! instance || instance . _isDestroyed ) {
448
- return ;
449
- }
450
-
451
- if (
452
- instance . constructor . _dataRefresh &&
453
- Components [ i ] === instance . constructor &&
454
- instance . $vnode . data . keepAlive !== true &&
455
- typeof instance . constructor . options . data === 'function'
456
- ) {
457
- const newData = instance . constructor . options . data . call ( instance ) ;
458
-
459
- for ( const key in newData ) {
460
- Vue . set ( instance . $data , key , newData [ key ] ) ;
461
- }
462
- }
463
- } ) ;
464
-
465
- checkForErrors ( this ) ;
466
- } ) ;
467
- }
468
-
469
- function nuxtReady ( _app ) {
470
- window . onNuxtReadyCbs . forEach ( ( cb ) => {
471
- if ( typeof cb === 'function' ) {
472
- cb ( _app ) ;
473
- }
474
- } ) ;
475
- // Special JSDOM
476
- if ( typeof window . _onNuxtLoaded === 'function' ) {
477
- window . _onNuxtLoaded ( _app ) ;
478
- }
479
- }
480
-
481
268
async function mountApp ( __app ) {
482
269
// Set global variables
483
270
app = __app . app ;
@@ -492,31 +279,12 @@ async function mountApp(__app) {
492
279
493
280
// Add afterEach router hooks
494
281
router . afterEach ( normalizeComponents ) ;
495
-
496
- router . afterEach ( fixPrepatch . bind ( _app ) ) ;
497
-
498
- // Listen for first Vue update
499
- Vue . nextTick ( ( ) => {
500
- // Call window.{{globals.readyCallback}} callbacks
501
- nuxtReady ( _app ) ;
502
- } ) ;
503
282
} ;
504
283
505
- // Resolve route components
506
- const Components = await Promise . all ( resolveComponents ( app . context . route ) ) ;
507
-
508
- if ( Components . length ) {
509
- _lastPaths = router . currentRoute . matched . map ( ( route ) => compile ( route . path ) ( router . currentRoute . params ) ) ;
510
- }
511
-
512
284
// Initialize error handler
513
285
_app . $loading = { } ; // To avoid error while _app.$nuxt does not exist
514
- if ( NUXT . error ) {
515
- _app . error ( NUXT . error ) ;
516
- }
517
286
518
287
// Add beforeEach router hooks
519
- router . beforeEach ( loadAsyncComponents . bind ( _app ) ) ;
520
288
router . beforeEach ( render . bind ( _app ) ) ;
521
289
router . beforeEach ( ( from , to , next ) => {
522
290
if ( from ?. name !== to ?. name ) {
@@ -526,19 +294,10 @@ async function mountApp(__app) {
526
294
next ( ) ;
527
295
} ) ;
528
296
529
- // Fix in static: remove trailing slash to force hydration
530
- // Full static, if server-rendered: hydrate, to allow custom redirect to generated page
531
-
532
- // Fix in static: remove trailing slash to force hydration
533
- if ( NUXT . serverRendered && isSamePath ( NUXT . routePath , _app . context . route . path ) ) {
534
- return mount ( ) ;
535
- }
536
-
537
297
// First render on client-side
538
298
const clientFirstMount = ( ) => {
539
299
normalizeComponents ( router . currentRoute , router . currentRoute ) ;
540
300
checkForErrors ( _app ) ;
541
- // Don't call fixPrepatch.call(_app, router.currentRoute, router.currentRoute) since it's first render
542
301
mount ( ) ;
543
302
} ;
544
303
0 commit comments