Skip to content

Commit

Permalink
fix(preset-umi): disable route preload feature if unnecessary (#12186)
Browse files Browse the repository at this point in the history
* fix(preset-umi): route preload feature error in vite mode

* refactor: skip unnecessary route preload logic
  • Loading branch information
PeachScript committed Mar 11, 2024
1 parent 1f24af3 commit a46726c
Showing 1 changed file with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,21 @@ async function getRoutePathFilesMap(
export default (api: IApi) => {
let routeChunkFilesMap: IRouteChunkFilesMap;

// enable when package name available
// because preload script use package name as attribute prefix value
api.describe({
enableBy: () => Boolean(api.pkg.name),
enableBy: () =>
// enable when package name available
// because preload script use package name as attribute prefix value
Boolean(api.pkg.name) &&
// vite mode is not supported currently
!api.config.vite &&
// mpa mode is unnecessary
!api.config.mpa &&
// only esm router needs this feature
api.config.routeLoader?.moduleType === 'esm',
});

api.addHTMLHeadScripts(() => {
if (api.name === 'build') {
if (api.name === 'build' && routeChunkFilesMap) {
// internal tern app use map mode
return api.config.tern
? // map mode
Expand Down Expand Up @@ -278,22 +285,24 @@ export default (api: IApi) => {
);

// 3. generate final route chunk files map
routeChunkFilesMap = {
p: api.pkg.name!,
b: api.appData.bundler!,
f: Object.entries(routeChunkFiles)
.sort((a, b) => a[1].index - b[1].index)
.map(([k, { id }]) => [k, id]),
// sort similar to react-router@6
r: lodash(routeFilesMap)
.toPairs()
.sort(
([a]: [string, number[]], [b]: [string, number[]]) =>
computeRouteScore(a) - computeRouteScore(b),
)
.fromPairs()
.value() as any,
};
if (!lodash.isEmpty(routeChunkFiles) && !lodash.isEmpty(routeFilesMap)) {
routeChunkFilesMap = {
p: api.pkg.name!,
b: api.appData.bundler!,
f: Object.entries(routeChunkFiles)
.sort((a, b) => a[1].index - b[1].index)
.map(([k, { id }]) => [k, id]),
// sort similar to react-router@6
r: lodash(routeFilesMap)
.toPairs()
.sort(
([a]: [string, number[]], [b]: [string, number[]]) =>
computeRouteScore(a) - computeRouteScore(b),
)
.fromPairs()
.value() as any,
};
}
}
});
};

0 comments on commit a46726c

Please sign in to comment.