-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: prefetch to preload and extract as utils
- Loading branch information
1 parent
aba626e
commit 786e746
Showing
8 changed files
with
102 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* NOTE: DO NOT USE ADVANCED SYNTAX IN THIS FILE, TO AVOID INSERT HELPERS TO REDUCE SCRIPT SIZE. | ||
*/ | ||
|
||
import { getPreloadRouteFiles } from '../features/routePrefetch/utils'; | ||
|
||
const basename = '{{basename}}'; | ||
const publicPath = '{{publicPath}}'; | ||
const pathname = location.pathname; | ||
const routePath = | ||
pathname.startsWith(basename) && | ||
decodeURI(`/${pathname.slice(basename.length)}`); | ||
|
||
// skip preload if basename not match | ||
if (routePath) { | ||
const map = '{{routeChunkFilesMap}}' as any; | ||
const doc = document; | ||
const head = doc.head; | ||
const createElement = doc.createElement.bind(doc); | ||
const files = getPreloadRouteFiles(routePath, map, { | ||
publicPath, | ||
}); | ||
|
||
files?.forEach((file) => { | ||
const type = file.type; | ||
const url = file.url; | ||
let tag: HTMLLinkElement | HTMLScriptElement; | ||
|
||
if (type === 'js') { | ||
tag = createElement('script'); | ||
tag.src = url; | ||
tag.async = true; | ||
} else if (type === 'css') { | ||
tag = createElement('link'); | ||
tag.href = url; | ||
tag.rel = 'preload'; | ||
tag.as = 'style'; | ||
} else { | ||
return; | ||
} | ||
|
||
file.attrs.forEach((attr) => { | ||
tag.setAttribute(attr[0], attr[1] || ''); | ||
}); | ||
|
||
head.appendChild(tag); | ||
}); | ||
} |
58 changes: 0 additions & 58 deletions
58
packages/preset-umi/src/features/routePrefetch/prefetchRouteFiles.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* NOTE: DO NOT USE ADVANCED SYNTAX IN THIS FILE, TO AVOID INSERT HELPERS TO REDUCE SCRIPT SIZE. | ||
*/ | ||
|
||
import type { IRouteChunkFilesMap } from './routePrefetch'; | ||
|
||
interface IPreloadRouteFile { | ||
type: 'js' | 'css' | unknown; | ||
url: string; | ||
attrs: ([string, string] | [string])[]; | ||
} | ||
|
||
export const PRELOAD_ROUTE_MAP_SCP_TYPE = 'umi-route-chunk-files-map'; | ||
|
||
export function getPreloadRouteFiles( | ||
path: string, | ||
map: IRouteChunkFilesMap, | ||
opts: { publicPath: string }, | ||
): IPreloadRouteFile[] | undefined { | ||
const matched: IRouteChunkFilesMap['r'][string] | undefined = | ||
// search for static route | ||
map.r[path] || | ||
// search for dynamic route | ||
Object.entries(map.r).find((p) => { | ||
const route = p[0]; | ||
const reg = new RegExp( | ||
// replace /:id to /[^/]+ | ||
// replace /* to /.+ | ||
`^${route.replace(/\/:[^/]+/g, '/[^/]+').replace('/*', '/.+')}$`, | ||
); | ||
|
||
return reg.test(path); | ||
})?.[1]; | ||
|
||
return matched?.map((i) => { | ||
const id = map.f[i][1]; | ||
const file = map.f[i][0]; | ||
const ext = file.split('.').pop(); | ||
|
||
return { | ||
type: ext, | ||
url: `${opts.publicPath}${file}`, | ||
attrs: [[`data-${map.b}`, `${map.p}:${id}`]], | ||
}; | ||
}); | ||
} |
1 change: 0 additions & 1 deletion
1
packages/preset-umi/templates/routePrefetch/prefetchRouteFilesScp.js
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
packages/preset-umi/templates/routePrefetch/preloadRouteFilesScp.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.