-
Notifications
You must be signed in to change notification settings - Fork 897
feat: implement 'as-needed' prefetch and preload strategy with chunk file mapping #1711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mister-Hope
wants to merge
6
commits into
main
Choose a base branch
from
as-needed
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
83b87ba
feat: implement 'as-needed' prefetch and preload strategy with chunk …
Mister-Hope 33e8292
feat!: change default options
Mister-Hope eb05f10
chore: update jsdocs
Mister-Hope 2b96681
Update packages/bundler-vite/src/build/build.ts
Mister-Hope 8fa2771
Update packages/bundler-webpack/src/build/build.ts
Mister-Hope f7c3f7b
Merge branch 'main' into as-needed
Mister-Hope File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| import type { App } from '@vuepress/core' | ||
| import type { PageChunkFilesMap } from '@vuepress/bundlerutils' | ||
| import { resolveLinkRoutePath } from '@vuepress/bundlerutils' | ||
| import type { App, Page } from '@vuepress/core' | ||
| import type { OutputChunk } from 'rolldown' | ||
|
|
||
| /** | ||
|
|
@@ -8,10 +10,14 @@ export const renderPagePrefetchLinks = ({ | |
| app, | ||
| outputEntryChunk, | ||
| pageChunkFiles, | ||
| page, | ||
| pageChunkFilesMap, | ||
| }: { | ||
| app: App | ||
| outputEntryChunk: OutputChunk | ||
| pageChunkFiles: string[] | ||
| page: Page | ||
| pageChunkFilesMap: PageChunkFilesMap | ||
| }): string => { | ||
| // shouldPrefetch option | ||
| const { shouldPrefetch } = app.options | ||
|
|
@@ -21,12 +27,37 @@ export const renderPagePrefetchLinks = ({ | |
| return '' | ||
| } | ||
|
|
||
| // dynamic imports excluding current page chunks | ||
| const prefetchFiles = outputEntryChunk.dynamicImports.filter( | ||
| (item) => !pageChunkFiles.some((file) => file === item), | ||
| ) | ||
| let candidateFiles: string[] | ||
|
|
||
| return prefetchFiles | ||
| if (shouldPrefetch === 'as-needed') { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe extract this snippet to bundlerutils?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How? The data structures are different accross bundlers, and rely on bundler types. |
||
| // collect linked page chunk file names | ||
| const linkedFileNames = new Set<string>() | ||
| for (const link of page.links) { | ||
| const routePath = resolveLinkRoutePath(link.absolute, app.options.base) | ||
| if (routePath) { | ||
| const targetChunks = pageChunkFilesMap.get(routePath) | ||
| if (targetChunks) { | ||
| for (const file of targetChunks) { | ||
| linkedFileNames.add(file) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // dynamic imports excluding current page chunks | ||
| // filtered to only linked pages' chunk files | ||
| candidateFiles = outputEntryChunk.dynamicImports.filter( | ||
| (item) => | ||
| linkedFileNames.has(item) && | ||
| !pageChunkFiles.some((file) => file === item), | ||
| ) | ||
| } else { | ||
| // dynamic imports excluding current page chunks | ||
| candidateFiles = outputEntryChunk.dynamicImports.filter( | ||
| (item) => !pageChunkFiles.some((file) => file === item), | ||
| ) | ||
| } | ||
|
|
||
| return candidateFiles | ||
| .map((item) => { | ||
| // resolve file type | ||
| const type = item.endsWith('.js') | ||
|
|
@@ -36,7 +67,11 @@ export const renderPagePrefetchLinks = ({ | |
| : '' | ||
|
|
||
| // user wants to explicitly control what to prefetch | ||
| if (shouldPrefetch !== true && !shouldPrefetch(item, type)) { | ||
| if ( | ||
| shouldPrefetch !== true && | ||
| shouldPrefetch !== 'as-needed' && | ||
| !shouldPrefetch(item, type) | ||
| ) { | ||
| return '' | ||
| } | ||
| return `<link rel="prefetch" href="${app.options.base}${item}" as="${type}">` | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.