Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Comment on lines +27 to +29

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

globalThis.publicPath 获取的 publicPath 被直接使用。如果它不以 / 结尾,在与 chunk 路径拼接时可能会导致错误的 URL。例如,如果 publicPath"/assets",而 chunk 路径是 my-chunk.js,结果 URL 将是 "/assetsmy-chunk.js",而不是正确的 "/assets/my-chunk.js"

为防止此类问题,确保基本路径始终以斜杠结尾非常重要。回退值 "/" 已经遵循了这个模式。

    if (typeof globalThis !== 'undefined' && typeof (globalThis as any).publicPath === "string") {
      const publicPath = (globalThis as any).publicPath as string;
      return publicPath.endsWith('/') ? publicPath : `${publicPath}/`;
    }

console.warn(
"publicPath is set to 'runtime' but window.publicPath is not defined or not a string, falling back to '/'"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

警告信息中引用了 window.publicPath,但代码中正确地使用了 globalThis.publicPath 以支持 window 可能不存在的各种环境(如 Web Workers)。为了保持一致性和清晰性,建议在警告信息中也使用 globalThis.publicPath

      "publicPath is set to 'runtime' but globalThis.publicPath is not defined or not a string, falling back to '/'"

);
return "/";
}
return CHUNK_BASE_PATH;
}

interface TurbopackBrowserBaseContext<M> extends TurbopackBaseContext<M> {
R: ResolvePathFromModule
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
Loading