Unverified Commit a4bc6f7a authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

address route loader review feedback

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 5b4b95e2
...@@ -24,6 +24,18 @@ declare let __turbopack_load__: any ...@@ -24,6 +24,18 @@ declare let __turbopack_load__: any
type PageChunkData = string | { path: string } type PageChunkData = string | { path: string }
function isChunkScriptInDocument(chunkPath: string): boolean {
const expectedPath = '/_next/' + chunkPath
return Array.from(document.scripts).some((script) => {
if (!script.src) return false
try {
return new URL(script.src).pathname.endsWith(expectedPath)
} catch {
return false
}
})
}
// Map of page route -> promise that settles once the page's executable chunks // Map of page route -> promise that settles once the page's executable chunks
// finish loading. The route loader consumes these promises in production. // finish loading. The route loader consumes these promises in production.
// Creating the map eagerly also lets the route loader detect Turbopack. // Creating the map eagerly also lets the route loader detect Turbopack.
...@@ -37,27 +49,27 @@ initialize({}) ...@@ -37,27 +49,27 @@ initialize({})
page: string, page: string,
chunksData: PageChunkData[] chunksData: PageChunkData[]
) => { ) => {
const chunkLoads = chunksData.map((chunkData) => ({ const chunkLoads = chunksData.map((chunkData) => {
chunkData, const chunkPath =
promise: __turbopack_load__(chunkData), typeof chunkData === 'string' ? chunkData : chunkData.path
})) const shouldDelayTimeout =
!chunkPath.endsWith('.css') && !isChunkScriptInDocument(chunkPath)
return {
shouldDelayTimeout,
promise: __turbopack_load__(chunkData),
}
})
// Preserve loading and error reporting for every chunk. CSS remains // Preserve loading and error reporting for every chunk. CSS remains
// subject to the route timeout, and the shared runtime is already active, // subject to the route timeout, and scripts already in the document do
// so only executable page chunks postpone that timeout. // not represent new page work. Only newly requested executable chunks
// postpone that timeout.
Promise.all(chunkLoads.map(({ promise }) => promise)).catch((err) => Promise.all(chunkLoads.map(({ promise }) => promise)).catch((err) =>
console.error('failed to load chunks for page ' + page, err) console.error('failed to load chunks for page ' + page, err)
) )
const chunksPromise = Promise.all( const chunksPromise = Promise.all(
chunkLoads chunkLoads
.filter(({ chunkData }) => { .filter(({ shouldDelayTimeout }) => shouldDelayTimeout)
const chunkPath =
typeof chunkData === 'string' ? chunkData : chunkData.path
return (
!chunkPath.endsWith('.css') &&
!/(?:^|\/)turbopack-[^/]+\.js$/.test(chunkPath)
)
})
.map(({ promise }) => promise) .map(({ promise }) => promise)
).catch(() => {}) ).catch(() => {})
turbopackPageChunkPromises.set(page, chunksPromise) turbopackPageChunkPromises.set(page, chunksPromise)
......
...@@ -21,7 +21,9 @@ describe('pages-router-route-loader-chunk-delay', () => { ...@@ -21,7 +21,9 @@ describe('pages-router-route-loader-chunk-delay', () => {
files: __dirname, files: __dirname,
}) })
it('should not time out when /_next/static chunks load slowly', async () => { async function loadTrackWithDelayedAssets(
pattern: string
): Promise<{ ok: boolean; message?: string }> {
const delayMs = 5000 const delayMs = 5000
let playwrightPage: Page | undefined let playwrightPage: Page | undefined
...@@ -36,24 +38,35 @@ describe('pages-router-route-loader-chunk-delay', () => { ...@@ -36,24 +38,35 @@ describe('pages-router-route-loader-chunk-delay', () => {
}) })
if (!playwrightPage) throw new Error('playwrightPage was not captured') if (!playwrightPage) throw new Error('playwrightPage was not captured')
await playwrightPage.route('**/_next/static/**/*.js*', async (route) => { await playwrightPage.route(pattern, async (route) => {
await new Promise((resolve) => setTimeout(resolve, delayMs)) await new Promise((resolve) => setTimeout(resolve, delayMs))
await route.continue() await route.continue()
}) })
// `router.push` masks this failure by falling back to a hard navigation, // `router.push` masks this failure by falling back to a hard navigation,
// so call the underlying page loader directly. // so call the underlying page loader directly.
const result: { ok: boolean; message?: string } = await browser.eval<any>( return browser.eval<any>(async () => {
async () => { try {
try { await (window as any).next.router.pageLoader.loadPage('/track')
await (window as any).next.router.pageLoader.loadPage('/track') return { ok: true }
return { ok: true } } catch (err: any) {
} catch (err: any) { return { ok: false, message: err?.message ?? String(err) }
return { ok: false, message: err?.message ?? String(err) }
}
} }
) })
}
it('should not time out when JavaScript chunks load slowly', async () => {
expect(
await loadTrackWithDelayedAssets('**/_next/static/**/*.js*')
).toEqual({ ok: true })
})
expect(result).toEqual({ ok: true }) it('should time out when CSS chunks load slowly', async () => {
expect(
await loadTrackWithDelayedAssets('**/_next/static/**/*.css*')
).toEqual({
ok: false,
message: 'Route did not complete loading: /track',
})
}) })
}) })
import styles from './track.module.css'
export default function Track() { export default function Track() {
return <p id="track-content">track page</p> return (
<p id="track-content" className={styles.track}>
track page
</p>
)
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment