forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(turbopack): runtime code support publicPath runtime #63
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
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,20 @@ declare var TURBOPACK_NEXT_CHUNK_URLS: ChunkUrl[] | undefined | |
| declare var CHUNK_BASE_PATH: string | ||
| declare var CHUNK_SUFFIX_PATH: string | ||
|
|
||
| // Support runtime public path from window.publicPath | ||
| function getRuntimeChunkBasePath(): string { | ||
| if (CHUNK_BASE_PATH === "__RUNTIME_PUBLIC_PATH__") { | ||
| if (typeof globalThis !== 'undefined' && typeof (globalThis as any).publicPath === "string") { | ||
| return (globalThis as any).publicPath; | ||
| } | ||
| console.warn( | ||
| "publicPath is set to 'runtime' but window.publicPath is not defined or not a string, falling back to '/'" | ||
|
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. |
||
| ); | ||
| return "/"; | ||
| } | ||
| return CHUNK_BASE_PATH; | ||
| } | ||
|
|
||
| interface TurbopackBrowserBaseContext<M> extends TurbopackBaseContext<M> { | ||
| R: ResolvePathFromModule | ||
| } | ||
|
|
@@ -344,7 +358,7 @@ function instantiateRuntimeModule( | |
| * Returns the URL relative to the origin where a chunk can be fetched from. | ||
| */ | ||
| function getChunkRelativeUrl(chunkPath: ChunkPath | ChunkListPath): ChunkUrl { | ||
| return `${CHUNK_BASE_PATH}${chunkPath | ||
| return `${getRuntimeChunkBasePath()}${chunkPath | ||
| .split('/') | ||
| .map((p) => encodeURIComponent(p)) | ||
| .join('/')}${CHUNK_SUFFIX_PATH}` as ChunkUrl | ||
|
|
@@ -368,8 +382,9 @@ function getPathFromScript( | |
| ? TURBOPACK_NEXT_CHUNK_URLS.pop()! | ||
| : chunkScript.getAttribute('src')! | ||
| const src = decodeURIComponent(chunkUrl.replace(/[?#].*$/, '')) | ||
| const path = src.startsWith(CHUNK_BASE_PATH) | ||
| ? src.slice(CHUNK_BASE_PATH.length) | ||
| const runtimeBasePath = getRuntimeChunkBasePath() | ||
| const path = src.startsWith(runtimeBasePath) | ||
| ? src.slice(runtimeBasePath.length) | ||
| : src | ||
| return path as ChunkPath | ChunkListPath | ||
| } | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
从
globalThis.publicPath获取的publicPath被直接使用。如果它不以/结尾,在与 chunk 路径拼接时可能会导致错误的 URL。例如,如果publicPath是"/assets",而 chunk 路径是my-chunk.js,结果 URL 将是"/assetsmy-chunk.js",而不是正确的"/assets/my-chunk.js"。为防止此类问题,确保基本路径始终以斜杠结尾非常重要。回退值
"/"已经遵循了这个模式。