Skip to content

Commit

Permalink
🎨 refactor prefetch
Browse files Browse the repository at this point in the history
  • Loading branch information
kuitos committed Mar 29, 2020
1 parent 16d3a3d commit af0304f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type Prefetch =
| boolean
| 'all'
| string[]
| ((apps: RegistrableApp[]) => { mainAppStartingAppsName: string[]; firstMountedAppsName: string[] });
| ((apps: RegistrableApp[]) => { criticalAppNames: string[]; minorAppsName: string[] });

export type StartOpts = {
prefetch?: Prefetch;
Expand Down
36 changes: 32 additions & 4 deletions src/prefetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
*/

import { Entry, importEntry, ImportEntryOpts } from 'import-html-entry';
import { flow, identity } from 'lodash';
import { flow, identity, isFunction } from 'lodash';
import { getMountedApps } from 'single-spa';
import { RegistrableApp } from './interfaces';
import { Prefetch, RegistrableApp } from './interfaces';
import { getDefaultTplWrapper } from './utils';

type RequestIdleCallbackHandle = any;
Expand Down Expand Up @@ -79,7 +79,7 @@ function prefetch(appName: string, entry: Entry, opts?: ImportEntryOpts): void {
});
}

export function prefetchAfterFirstMounted(apps: RegistrableApp[], opts?: ImportEntryOpts): void {
function prefetchAfterFirstMounted(apps: RegistrableApp[], opts?: ImportEntryOpts): void {
window.addEventListener('single-spa:first-mount', function listener() {
const mountedApps = getMountedApps();
const notMountedApps = apps.filter(app => mountedApps.indexOf(app.name) === -1);
Expand All @@ -94,7 +94,7 @@ export function prefetchAfterFirstMounted(apps: RegistrableApp[], opts?: ImportE
});
}

export function prefetchAll(apps: RegistrableApp[], opts?: ImportEntryOpts): void {
function prefetchAll(apps: RegistrableApp[], opts?: ImportEntryOpts): void {
window.addEventListener('single-spa:no-app-change', function listener() {
if (process.env.NODE_ENV === 'development') {
console.log('[qiankun] prefetch starting for all assets...', apps);
Expand All @@ -104,3 +104,31 @@ export function prefetchAll(apps: RegistrableApp[], opts?: ImportEntryOpts): voi
window.removeEventListener('single-spa:no-app-change', listener);
});
}

export function prefetchApps(apps: RegistrableApp[], prefetchAction: Prefetch, importEntryOpts: ImportEntryOpts) {
const appsName2Apps = (names: string[]): RegistrableApp[] => apps.filter(app => names.includes(app.name));

if (Array.isArray(prefetchAction)) {
prefetchAfterFirstMounted(appsName2Apps(prefetchAction as string[]), importEntryOpts);
} else if (isFunction(prefetchAction)) {
(async () => {
// critical rendering apps would be prefetch as earlier as possible
const { criticalAppNames = [], minorAppsName = [] } = await prefetchAction(apps);
prefetchAll(appsName2Apps(criticalAppNames), importEntryOpts);
prefetchAfterFirstMounted(appsName2Apps(minorAppsName), importEntryOpts);
})();
} else {
switch (prefetchAction) {
case true:
prefetchAfterFirstMounted(apps, importEntryOpts);
break;

case 'all':
prefetchAll(apps, importEntryOpts);
break;

default:
break;
}
}
}
33 changes: 6 additions & 27 deletions src/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { importEntry, ImportEntryOpts } from 'import-html-entry';
import { concat, flow, identity, isFunction, mergeWith } from 'lodash';
import { registerApplication, start as startSingleSpa } from 'single-spa';
import getAddOns from './addons';
import { RegistrableApp, StartOpts, Prefetch } from './interfaces';
import { prefetchAfterFirstMounted, prefetchAll } from './prefetch';
import { RegistrableApp, StartOpts } from './interfaces';
import { prefetchApps } from './prefetch';
import { genSandbox } from './sandbox';
import { getDefaultTplWrapper } from './utils';

Expand Down Expand Up @@ -62,6 +62,7 @@ let useJsSandbox = false;
const frameworkStartedDefer = new Deferred<void>();

let importLoaderConfiguration: ImportEntryOpts = {};

export function getImportLoaderConfiguration() {
return importLoaderConfiguration;
}
Expand Down Expand Up @@ -192,37 +193,15 @@ export function registerMicroApps<T extends object = {}>(apps: Array<Registrable
});
}

async function doPrefetch(prefetch: Prefetch, importEntryOpts: ImportEntryOpts) {
const appsName2Apps = (names: string[]): RegistrableApp[] => microApps.filter(app => names.includes(app.name));
if (Array.isArray(prefetch)) {
prefetchAfterFirstMounted(appsName2Apps(prefetch as string[]), importEntryOpts);
} else if (isFunction(prefetch)) {
const { firstMountedAppsName = [], mainAppStartingAppsName = [] } = await prefetch(microApps);
prefetchAfterFirstMounted(appsName2Apps(firstMountedAppsName), importEntryOpts);
prefetchAll(appsName2Apps(mainAppStartingAppsName), importEntryOpts);
} else {
switch (prefetch) {
case true:
prefetchAfterFirstMounted(microApps, importEntryOpts);
break;

case 'all':
prefetchAll(microApps, importEntryOpts);
break;

default:
break;
}
}
}

export function start(opts: StartOpts = {}) {
window.__POWERED_BY_QIANKUN__ = true;

const { prefetch = true, jsSandbox = true, singular: singularMode = true, ...importEntryOpts } = opts;
importLoaderConfiguration = importEntryOpts;

doPrefetch(prefetch, importLoaderConfiguration);
if (prefetch) {
prefetchApps(microApps, prefetch, importLoaderConfiguration);
}

if (singularMode) {
singular = singularMode;
Expand Down

0 comments on commit af0304f

Please sign in to comment.