Skip to content

Commit b38a716

Browse files
committed
Some dead code unraveling and removal
1 parent 8cddc52 commit b38a716

File tree

2 files changed

+1
-501
lines changed

2 files changed

+1
-501
lines changed

shell/initialize/client.js

-234
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,10 @@ import fetch from 'unfetch';
55
import middleware from '../config/middleware.js';
66
import {
77
middlewareSeries,
8-
sanitizeComponent,
9-
resolveRouteComponents,
108
getMatchedComponents,
11-
getMatchedComponentsInstances,
129
flatMapComponents,
1310
setContext,
14-
compile,
15-
getQueryDiff,
1611
globalHandleError,
17-
isSamePath,
1812
urlJoin
1913
} from '../utils/nuxt.js';
2014
import { createApp } from './index.js';
@@ -40,12 +34,9 @@ if (!global.fetch) {
4034
}
4135

4236
// Global shared references
43-
let _lastPaths = [];
4437
let app;
4538
let router;
4639

47-
const NUXT = {};
48-
4940
const $config = nuxt.publicRuntimeConfig || {}; // eslint-disable-line no-undef
5041

5142
if ($config._app) {
@@ -98,81 +89,6 @@ const errorHandler = Vue.config.errorHandler || console.error; // eslint-disable
9889
// Create and mount App
9990
createApp(nuxt.publicRuntimeConfig).then(mountApp).catch(errorHandler); // eslint-disable-line no-undef
10091

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 (/^Loading( CSS)? chunk (\d)+ failed\./.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-
17692
function callMiddleware(Components, context) {
17793
let midd = ['i18n'];
17894
let unknownMiddleware = false;
@@ -207,16 +123,6 @@ async function render(to, from, next) {
207123
return next();
208124
}
209125

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-
220126
// nextCalled is true when redirected
221127
let nextCalled = false;
222128
const _next = (path) => {
@@ -327,70 +233,6 @@ async function render(to, from, next) {
327233
return next();
328234
}
329235

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-
394236
// If not redirected
395237
if (!nextCalled) {
396238
if (this.$loading.finish && !this.$loading.manual) {
@@ -402,8 +244,6 @@ async function render(to, from, next) {
402244
} catch (err) {
403245
const error = err || {};
404246

405-
_lastPaths = [];
406-
407247
globalHandleError(error);
408248

409249
this.error(error);
@@ -432,52 +272,6 @@ function checkForErrors(app) {
432272
}
433273
}
434274

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-
481275
async function mountApp(__app) {
482276
// Set global variables
483277
app = __app.app;
@@ -492,31 +286,12 @@ async function mountApp(__app) {
492286

493287
// Add afterEach router hooks
494288
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-
});
503289
};
504290

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-
512291
// Initialize error handler
513292
_app.$loading = {}; // To avoid error while _app.$nuxt does not exist
514-
if (NUXT.error) {
515-
_app.error(NUXT.error);
516-
}
517293

518294
// Add beforeEach router hooks
519-
router.beforeEach(loadAsyncComponents.bind(_app));
520295
router.beforeEach(render.bind(_app));
521296
router.beforeEach((from, to, next) => {
522297
if (from?.name !== to?.name) {
@@ -526,19 +301,10 @@ async function mountApp(__app) {
526301
next();
527302
});
528303

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-
537304
// First render on client-side
538305
const clientFirstMount = () => {
539306
normalizeComponents(router.currentRoute, router.currentRoute);
540307
checkForErrors(_app);
541-
// Don't call fixPrepatch.call(_app, router.currentRoute, router.currentRoute) since it's first render
542308
mount();
543309
};
544310

0 commit comments

Comments
 (0)