Unverified Commit a6d950a4 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: fix static asset skew protection for edge and prerenders (#90238)

- Edge Runtime was missing `globalThis.NEXT_CLIENT_ASSET_SUFFIX` at runtime
- Prerenders were always running `globalThis.NEXT_CLIENT_ASSET_SUFFIX` set

Fixups after #88828
parent d7732c57
...@@ -241,7 +241,7 @@ pub async fn get_edge_chunking_context_with_client_assets( ...@@ -241,7 +241,7 @@ pub async fn get_edge_chunking_context_with_client_assets(
) )
.asset_base_path(Some(asset_prefix)) .asset_base_path(Some(asset_prefix))
.default_url_behavior(UrlBehavior { .default_url_behavior(UrlBehavior {
suffix: AssetSuffix::Inferred, suffix: AssetSuffix::FromGlobal(rcstr!("NEXT_CLIENT_ASSET_SUFFIX")),
static_suffix: css_url_suffix.to_resolved().await?, static_suffix: css_url_suffix.to_resolved().await?,
}) })
.minify_type(if *turbo_minify.await? { .minify_type(if *turbo_minify.await? {
...@@ -329,7 +329,7 @@ pub async fn get_edge_chunking_context( ...@@ -329,7 +329,7 @@ pub async fn get_edge_chunking_context(
) )
.default_url_behavior(UrlBehavior { .default_url_behavior(UrlBehavior {
suffix: AssetSuffix::Inferred, suffix: AssetSuffix::Inferred,
static_suffix: css_url_suffix, static_suffix: ResolvedVc::cell(None),
}) })
// Since one can't read files in edge directly, any asset need to be fetched // Since one can't read files in edge directly, any asset need to be fetched
// instead. This special blob url is handled by the custom fetch // instead. This special blob url is handled by the custom fetch
......
...@@ -1053,7 +1053,7 @@ pub async fn get_server_chunking_context_with_client_assets( ...@@ -1053,7 +1053,7 @@ pub async fn get_server_chunking_context_with_client_assets(
) )
.default_url_behavior(UrlBehavior { .default_url_behavior(UrlBehavior {
suffix: AssetSuffix::Inferred, suffix: AssetSuffix::Inferred,
static_suffix: css_url_suffix, static_suffix: ResolvedVc::cell(None),
}) })
.minify_type(if *minify.await? { .minify_type(if *minify.await? {
MinifyType::Minify { MinifyType::Minify {
...@@ -1152,7 +1152,7 @@ pub async fn get_server_chunking_context( ...@@ -1152,7 +1152,7 @@ pub async fn get_server_chunking_context(
) )
.default_url_behavior(UrlBehavior { .default_url_behavior(UrlBehavior {
suffix: AssetSuffix::Inferred, suffix: AssetSuffix::Inferred,
static_suffix: css_url_suffix, static_suffix: ResolvedVc::cell(None),
}) })
.minify_type(if *minify.await? { .minify_type(if *minify.await? {
MinifyType::Minify { MinifyType::Minify {
......
...@@ -840,15 +840,21 @@ export function createStaticWorker( ...@@ -840,15 +840,21 @@ export function createStaticWorker(
isolatedMemory: true, isolatedMemory: true,
enableWorkerThreads: config.experimental.workerThreads, enableWorkerThreads: config.experimental.workerThreads,
exposedMethods: staticWorkerExposedMethods, exposedMethods: staticWorkerExposedMethods,
forkOptions: process.env.NEXT_CPU_PROF forkOptions: {
? { env: {
env: { ...(process.env.NEXT_CPU_PROF
NEXT_CPU_PROF: '1', ? {
NEXT_CPU_PROF_DIR: process.env.NEXT_CPU_PROF_DIR, NEXT_CPU_PROF: '1',
__NEXT_PRIVATE_CPU_PROFILE: 'build-static-worker', NEXT_CPU_PROF_DIR: process.env.NEXT_CPU_PROF_DIR,
}, __NEXT_PRIVATE_CPU_PROFILE: 'build-static-worker',
} }
: undefined, : undefined),
// worker.ts copies this value into globalThis.NEXT_CLIENT_ASSET_SUFFIX
__NEXT_PRERENDER_CLIENT_ASSET_SUFFIX: config.deploymentId
? `?dpl=${config.deploymentId}`
: '',
},
},
}) as StaticWorker }) as StaticWorker
} }
...@@ -2068,6 +2074,7 @@ export default async function build( ...@@ -2068,6 +2074,7 @@ export default async function build(
pprConfig: config.experimental.ppr, pprConfig: config.experimental.ppr,
cacheLifeProfiles: config.cacheLife, cacheLifeProfiles: config.cacheLife,
buildId, buildId,
deploymentId: config.deploymentId,
sriEnabled, sriEnabled,
cacheMaxMemorySize: config.cacheMaxMemorySize, cacheMaxMemorySize: config.cacheMaxMemorySize,
}) })
...@@ -2290,6 +2297,7 @@ export default async function build( ...@@ -2290,6 +2297,7 @@ export default async function build(
pprConfig: config.experimental.ppr, pprConfig: config.experimental.ppr,
cacheLifeProfiles: config.cacheLife, cacheLifeProfiles: config.cacheLife,
buildId, buildId,
deploymentId: config.deploymentId,
sriEnabled, sriEnabled,
}) })
} }
......
...@@ -757,6 +757,7 @@ export async function isPageStatic({ ...@@ -757,6 +757,7 @@ export async function isPageStatic({
cacheLifeProfiles, cacheLifeProfiles,
pprConfig, pprConfig,
buildId, buildId,
deploymentId,
sriEnabled, sriEnabled,
}: { }: {
dir: string dir: string
...@@ -783,6 +784,7 @@ export async function isPageStatic({ ...@@ -783,6 +784,7 @@ export async function isPageStatic({
nextConfigOutput: 'standalone' | 'export' | undefined nextConfigOutput: 'standalone' | 'export' | undefined
pprConfig: ExperimentalPPRConfig | undefined pprConfig: ExperimentalPPRConfig | undefined
buildId: string buildId: string
deploymentId: string
sriEnabled: boolean sriEnabled: boolean
}): Promise<PageIsStaticResult> { }): Promise<PageIsStaticResult> {
// Skip page data collection for synthetic _global-error routes // Skip page data collection for synthetic _global-error routes
...@@ -836,6 +838,7 @@ export async function isPageStatic({ ...@@ -836,6 +838,7 @@ export async function isPageStatic({
name: edgeInfo.name, name: edgeInfo.name,
useCache: true, useCache: true,
distDir, distDir,
deploymentId,
}) })
const mod = ( const mod = (
await runtime.context._ENTRIES[`middleware_${edgeInfo.name}`] await runtime.context._ENTRIES[`middleware_${edgeInfo.name}`]
......
...@@ -2,6 +2,10 @@ import '../server/require-hook' ...@@ -2,6 +2,10 @@ import '../server/require-hook'
// Import cpu-profile to start profiling early if enabled // Import cpu-profile to start profiling early if enabled
import '../server/lib/cpu-profile' import '../server/lib/cpu-profile'
// Set the global asset suffix for Turbopack compiled code to use during prerendering
;(globalThis as any).NEXT_CLIENT_ASSET_SUFFIX =
process.env.__NEXT_PRERENDER_CLIENT_ASSET_SUFFIX
export { export {
getDefinedNamedExports, getDefinedNamedExports,
hasCustomGetInitialProps, hasCustomGetInitialProps,
......
...@@ -343,14 +343,8 @@ export async function exportPages( ...@@ -343,14 +343,8 @@ export async function exportPages(
nextConfig, nextConfig,
options, options,
renderResumeDataCachesByPage = {}, renderResumeDataCachesByPage = {},
deploymentId,
} = input } = input
// Set the global asset suffix for Turbopack compiled code to use during prerendering
;(globalThis as any).NEXT_CLIENT_ASSET_SUFFIX = deploymentId
? `?dpl=${deploymentId}`
: ''
installGlobalBehaviors(nextConfig) installGlobalBehaviors(nextConfig)
if (nextConfig.enablePrerenderSourceMaps) { if (nextConfig.enablePrerenderSourceMaps) {
......
...@@ -1717,6 +1717,7 @@ export default class NextNodeServer extends BaseServer< ...@@ -1717,6 +1717,7 @@ export default class NextNodeServer extends BaseServer<
request: requestData, request: requestData,
useCache: true, useCache: true,
onWarning: params.onWarning, onWarning: params.onWarning,
deploymentId: this.deploymentId,
}) })
} }
...@@ -2014,6 +2015,7 @@ export default class NextNodeServer extends BaseServer< ...@@ -2014,6 +2015,7 @@ export default class NextNodeServer extends BaseServer<
params.req, params.req,
'serverComponentsHmrCache' 'serverComponentsHmrCache'
), ),
deploymentId: this.deploymentId,
}) })
if (result.fetchMetrics) { if (result.fetchMetrics) {
......
...@@ -37,6 +37,7 @@ interface RunnerFnParams { ...@@ -37,6 +37,7 @@ interface RunnerFnParams {
distDir: string distDir: string
incrementalCache?: any incrementalCache?: any
serverComponentsHmrCache?: ServerComponentsHmrCache serverComponentsHmrCache?: ServerComponentsHmrCache
deploymentId: string
} }
type RunnerFn = (params: RunnerFnParams) => Promise<FetchEventResult> type RunnerFn = (params: RunnerFnParams) => Promise<FetchEventResult>
...@@ -96,6 +97,12 @@ export async function getRuntimeContext( ...@@ -96,6 +97,12 @@ export async function getRuntimeContext(
params.serverComponentsHmrCache params.serverComponentsHmrCache
} }
if (params.deploymentId) {
runtime.context.globalThis.NEXT_CLIENT_ASSET_SUFFIX = params.deploymentId
? `?dpl=${params.deploymentId}`
: ''
}
for (const paramPath of params.paths) { for (const paramPath of params.paths) {
evaluateInContext(paramPath) evaluateInContext(paramPath)
} }
......
import imported from '../../public/test.png'
const url = new URL('../../public/test.png', import.meta.url).toString()
export function GET() {
return Response.json({ imported, url })
}
'use client'
import imported from '../../public/test.png'
const url = new URL('../../public/test.png', import.meta.url).toString()
export default function Page() {
return (
<main>
<p id="imported-src">{imported.src}</p>
<p id="new-url">{url}</p>
</main>
)
}
import { headers } from 'next/headers'
export default async function Layout({ children }) {
// Use headers() to opt into dynamic rendering
await headers()
return children
}
'use client'
import imported from '../../public/test.png'
const url = new URL('../../public/test.png', import.meta.url).toString()
export default function Page() {
return (
<main>
<p id="imported-src">{imported.src}</p>
<p id="new-url">{url}</p>
</main>
)
}
import { headers } from 'next/headers'
import imported from '../../public/test.png'
const url = new URL('../../public/test.png', import.meta.url).toString()
export default async function Page() {
// Use headers() to opt into dynamic rendering
await headers()
return (
<main>
<p id="imported-src">{imported.src}</p>
<p id="new-url">{url}</p>
</main>
)
}
export default function RootLayout({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import imported from '../public/test.png'
const url = new URL('../public/test.png', import.meta.url).toString()
export default function Page() {
return (
<main>
<p id="imported-src">{imported.src}</p>
<p id="new-url">{url}</p>
</main>
)
}
import { nextTestSetup } from 'e2e-utils'
// Skip for webpack - dpl suffix in asset URLs is only implemented for Turbopack
;(process.env.IS_TURBOPACK_TEST ? describe : describe.skip)(
'URL asset references with deploymentId',
() => {
const { next, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})
if (skipped) {
return
}
const deploymentId = 'test-deployment-id'
describe('import src attribute', () => {
it('should include dpl query in RSC page', async () => {
const $ = await next.render$('/')
const src = $('#imported-src').text()
expect(src).toContain('dpl=' + deploymentId)
})
it('should include dpl query in client page', async () => {
const $ = await next.render$('/client')
const src = $('#imported-src').text()
expect(src).toContain('dpl=' + deploymentId)
})
it('should include dpl query in client page (after hydration)', async () => {
const browser = await next.browser('/client')
const url = await browser.elementByCss('#imported-src').text()
expect(url).toContain('dpl=' + deploymentId)
})
})
describe('new URL() pattern', () => {
it('should include dpl query in RSC page', async () => {
const $ = await next.render$('/')
const url = $('#new-url').text()
expect(url).toContain('dpl=' + deploymentId)
})
it('should include dpl query in client page', async () => {
const $ = await next.render$('/client')
const url = $('#new-url').text()
expect(url).toContain('dpl=' + deploymentId)
})
it('should include dpl query in client page (after hydration)', async () => {
const browser = await next.browser('/client')
const url = await browser.elementByCss('#new-url').text()
expect(url).toContain('dpl=' + deploymentId)
})
})
describe('dynamic RSC page (headers)', () => {
it('should include dpl query in dynamic RSC page', async () => {
const $ = await next.render$('/dynamic')
const src = $('#imported-src').text()
expect(src).toContain('dpl=' + deploymentId)
})
it('should include dpl query in new URL pattern', async () => {
const $ = await next.render$('/dynamic')
const url = $('#new-url').text()
expect(url).toContain('dpl=' + deploymentId)
})
})
describe('dynamic client page (headers in layout)', () => {
it('should include dpl query in dynamic client page', async () => {
const $ = await next.render$('/dynamic-client')
const src = $('#imported-src').text()
expect(src).toContain('dpl=' + deploymentId)
})
it('should include dpl query in new URL pattern', async () => {
const $ = await next.render$('/dynamic-client')
const url = $('#new-url').text()
expect(url).toContain('dpl=' + deploymentId)
})
it('should include dpl query after hydration', async () => {
const browser = await next.browser('/dynamic-client')
const url = await browser.elementByCss('#imported-src').text()
expect(url).toContain('dpl=' + deploymentId)
})
})
describe('API route', () => {
it('should return import src with dpl query', async () => {
const data = await next
.fetch('/api')
.then((res) => res.ok && res.json())
expect(data.imported.src).toContain('dpl=' + deploymentId)
})
it('should return new URL with dpl query', async () => {
const data = await next
.fetch('/api')
.then((res) => res.ok && res.json())
expect(data.url).not.toContain('dpl=')
})
})
}
)
import fs from 'fs'
import { fileURLToPath } from 'url'
import imported from '../../public/vercel.png' import imported from '../../public/vercel.png'
const url = new URL('../../public/vercel.png', import.meta.url).toString() const url = new URL('../../public/vercel.png', import.meta.url)
export function GET(req, res) { export function GET(req, res) {
return Response.json({ imported, url }) let size
try {
size = fs.readFileSync(fileURLToPath(url)).length
} catch (e) {
size = e.message
}
return Response.json({ imported: imported.src, url: url.toString(), size })
} }
...@@ -5,5 +5,5 @@ export const contentType = 'text/json' ...@@ -5,5 +5,5 @@ export const contentType = 'text/json'
// Image generation // Image generation
export default async function Image() { export default async function Image() {
return Response.json({ imported, url }) return Response.json({ imported: imported.src, url })
} }
...@@ -6,7 +6,7 @@ const url = new URL('./public/vercel.png', import.meta.url).toString() ...@@ -6,7 +6,7 @@ const url = new URL('./public/vercel.png', import.meta.url).toString()
export async function middleware(req: NextRequest) { export async function middleware(req: NextRequest) {
if (req.nextUrl.toString().endsWith('/middleware')) { if (req.nextUrl.toString().endsWith('/middleware')) {
return Response.json({ imported, url }) return Response.json({ imported: imported.src, url })
} }
return NextResponse.next() return NextResponse.next()
......
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
module.exports = { const nextConfig = {}
deploymentId: 'test-deployment-id',
} module.exports = nextConfig
...@@ -4,7 +4,7 @@ const url = new URL('../../../public/vercel.png', import.meta.url) ...@@ -4,7 +4,7 @@ const url = new URL('../../../public/vercel.png', import.meta.url)
export default (req, res) => { export default (req, res) => {
return new Response( return new Response(
JSON.stringify({ JSON.stringify({
imported, imported: imported.src,
url: url.toString(), url: url.toString(),
}) })
) )
......
import fs from 'fs' import fs from 'fs'
import { fileURLToPath } from 'url'
import imported from '../../../public/vercel.png' import imported from '../../../public/vercel.png'
const url = new URL('../../../public/vercel.png', import.meta.url) const url = new URL('../../../public/vercel.png', import.meta.url)
...@@ -6,13 +7,13 @@ const url = new URL('../../../public/vercel.png', import.meta.url) ...@@ -6,13 +7,13 @@ const url = new URL('../../../public/vercel.png', import.meta.url)
export default (req, res) => { export default (req, res) => {
let size let size
try { try {
size = fs.readFileSync(url).length size = fs.readFileSync(fileURLToPath(url)).length
} catch (e) { } catch (e) {
size = e.message size = e.message
} }
res.send({ res.send({
imported, imported: imported.src,
url: url.toString(), url: url.toString(),
size, size,
}) })
......
import { retry } from 'next-test-utils' import { retry } from 'next-test-utils'
import { nextTestSetup } from 'e2e-utils' import { isNextDev, isNextStart, nextTestSetup } from 'e2e-utils'
// | | Pages Client | Pages Server (SSR,RSC) | API Routes/Middleware/Metadata | // | | Pages Client | Pages Server (SSR,RSC) | API Routes/Middleware/Metadata |
// |---------|-------------------------|-------------------------|--------------------------------| // |---------|-------------------------|-------------------------|--------------------------------|
...@@ -11,9 +11,12 @@ import { nextTestSetup } from 'e2e-utils' ...@@ -11,9 +11,12 @@ import { nextTestSetup } from 'e2e-utils'
// - a bug where App Router API routes (and Metadata) return client assets for `new URL`s. // - a bug where App Router API routes (and Metadata) return client assets for `new URL`s.
// - a bug where Edge Page routes return client assets for `new URL`s. // - a bug where Edge Page routes return client assets for `new URL`s.
describe(`Handle new URL asset references`, () => { describe(`Handle new URL asset references`, () => {
const { next, skipped } = nextTestSetup({ const { next, skipped, isTurbopack } = nextTestSetup({
files: __dirname, files: __dirname,
// Workaround for `Error: invariant: htmlFsRef != null && jsonFsRef != null /ssg` errors env: {
// rely on skew protection when deployed
NEXT_DEPLOYMENT_ID: isNextStart ? 'test-deployment-id' : undefined,
},
skipDeployment: true, skipDeployment: true,
}) })
...@@ -21,81 +24,89 @@ describe(`Handle new URL asset references`, () => { ...@@ -21,81 +24,89 @@ describe(`Handle new URL asset references`, () => {
return return
} }
const serverFilePath = expect.stringMatching( const serverFileRegex = expect.stringMatching(
/file:.*\/.next(\/dev)?\/server\/.*\/vercel\.[0-9a-f]{8}\.png$/ /file:.*\/.next(\/dev)?\/server\/.*\/vercel.HASH.png$/
)
const serverEdgeUrl = expect.stringMatching(
/^blob:.*vercel\.[0-9a-f]{8,}\.png$/
)
const clientFilePath = expect.stringMatching(
/^\/_next\/static\/media\/vercel\.[0-9a-f]{8}\.png$/
) )
const serverEdgeUrl = isTurbopack
? `blob:server/edge/assets/vercel.HASH.png`
: `blob:vercel.HASH.png`
let clientUrl: string
const expectedPageContent = (count: number) =>
'Hello ' + Array(count).fill(clientUrl).join('+')
beforeAll(() => {
let expectedToken
if (isNextDev || !isTurbopack) {
expectedToken = undefined
} else {
expectedToken = next.deploymentId
if (!expectedToken) {
throw new Error('Missing deployment id')
}
}
clientUrl = `/_next/static/media/vercel.HASH.png${expectedToken ? `?dpl=${expectedToken}` : ''}`
})
it('should respond on middleware api', async () => { it('should respond on middleware api', async () => {
const data = await next const data = await next
.fetch('/middleware') .fetch('/middleware')
.then((res) => res.ok && res.json()) .then((res) => res.ok && res.text())
const json = JSON.parse(stripVercelPngHash(data))
expect(data).toEqual({ expect(json).toEqual({
imported: expect.objectContaining({ imported: clientUrl,
src: clientFilePath,
}),
url: serverEdgeUrl, url: serverEdgeUrl,
}) })
}) })
const expectedPage =
/^Hello \/_next\/static\/media\/vercel\.[0-9a-f]{8}\.png(\+\/_next\/static\/media\/vercel\.[0-9a-f]{8}\.png(\+\/_next\/static\/media\/vercel\.[0-9a-f]{8}\.png)?)?$/
describe('app router', () => { describe('app router', () => {
it('should respond on webmanifest', async () => { it('should respond on webmanifest', async () => {
const data = await next const data = await next
.fetch('/manifest.webmanifest') .fetch('/manifest.webmanifest')
.then((res) => res.ok && res.json()) .then((res) => res.ok && res.text())
const json = JSON.parse(stripVercelPngHash(data))
expect(data).toEqual({ expect(json).toEqual({
short_name: 'Next.js', short_name: 'Next.js',
name: 'Next.js', name: 'Next.js',
icons: [ icons: [
{ {
src: clientFilePath, src: clientUrl,
type: 'image/png', type: 'image/png',
sizes: '512x512', sizes: '512x512',
}, },
], ],
// TODO Webpack bug? // TODO Webpack bug?
description: process.env.IS_TURBOPACK_TEST description: isTurbopack ? serverFileRegex : clientUrl,
? serverFilePath
: clientFilePath,
}) })
}) })
it('should respond on opengraph-image', async () => { it('should respond on opengraph-image', async () => {
const data = await next const data = await next
.fetch('/opengraph-image') .fetch('/opengraph-image')
.then((res) => res.ok && res.json()) .then((res) => res.ok && res.text())
const json = JSON.parse(stripVercelPngHash(data))
expect(data).toEqual({ expect(json).toEqual({
imported: expect.objectContaining({ imported: clientUrl,
src: clientFilePath,
}),
// TODO Webpack bug? // TODO Webpack bug?
url: process.env.IS_TURBOPACK_TEST ? serverFilePath : clientFilePath, url: isTurbopack ? serverFileRegex : clientUrl,
}) })
}) })
for (const page of ['/rsc', '/rsc-edge', '/client', '/client-edge']) { for (const page of ['/rsc', '/rsc-edge', '/client', '/client-edge']) {
// TODO Webpack bug? // TODO Webpack bug?
let shouldSkip = process.env.IS_TURBOPACK_TEST let shouldSkip = isTurbopack ? false : page.includes('edge')
? false
: page.includes('edge')
;(shouldSkip ? it.skip : it)( ;(shouldSkip ? it.skip : it)(
`should render the ${page} page`, `should render the ${page} page`,
async () => { async () => {
const $ = await next.render$(page) const $ = await next.render$(page)
// eslint-disable-next-line jest/no-standalone-expect // eslint-disable-next-line jest/no-standalone-expect
expect($('main').text()).toMatch(expectedPage) expect(stripVercelPngHash($('main').text())).toEqual(
expectedPageContent(2)
)
} }
) )
;(shouldSkip ? it.skip : it)( ;(shouldSkip ? it.skip : it)(
...@@ -103,46 +114,46 @@ describe(`Handle new URL asset references`, () => { ...@@ -103,46 +114,46 @@ describe(`Handle new URL asset references`, () => {
async () => { async () => {
const browser = await next.browser(page) const browser = await next.browser(page)
await retry(async () => await retry(async () =>
expect(await browser.elementByCss('main').text()).toMatch( expect(
expectedPage stripVercelPngHash(await browser.elementByCss('main').text())
) ).toEqual(expectedPageContent(2))
) )
} }
) )
} }
it('should respond on API', async () => { it('should respond on API', async () => {
const data = await next.fetch('/api').then((res) => res.ok && res.json()) const data = await next.fetch('/api').then((res) => res.ok && res.text())
const json = JSON.parse(stripVercelPngHash(data))
expect(data).toEqual({ expect(json).toEqual({
imported: expect.objectContaining({ imported: clientUrl,
src: clientFilePath,
}),
// TODO Webpack bug? // TODO Webpack bug?
url: process.env.IS_TURBOPACK_TEST ? serverFilePath : clientFilePath, url: isTurbopack ? serverFileRegex : clientUrl,
size: isTurbopack ? 30079 : expect.toBeString(),
}) })
}) })
}) })
describe('pages router', () => { describe('pages router', () => {
for (const page of [ for (const [page, count] of [
'/pages/static', ['/pages/static', 2],
'/pages/ssr', ['/pages/ssr', 3],
'/pages/ssg', ['/pages/ssg', 3],
'/pages-edge/static', ['/pages-edge/static', 2],
'/pages-edge/ssr', ['/pages-edge/ssr', 3],
]) { ] as const) {
// TODO Webpack bug? // TODO Webpack bug?
let shouldSkip = process.env.IS_TURBOPACK_TEST let shouldSkip = isTurbopack ? false : page.includes('edge')
? false
: page.includes('edge')
;(shouldSkip ? it.skip : it)( ;(shouldSkip ? it.skip : it)(
`should render the ${page} page`, `should render the ${page} page`,
async () => { async () => {
const $ = await next.render$(page) const $ = await next.render$(page)
// eslint-disable-next-line jest/no-standalone-expect // eslint-disable-next-line jest/no-standalone-expect
expect($('main').text()).toMatch(expectedPage) expect(stripVercelPngHash($('main').text())).toEqual(
expectedPageContent(count)
)
} }
) )
;(shouldSkip ? it.skip : it)( ;(shouldSkip ? it.skip : it)(
...@@ -150,9 +161,9 @@ describe(`Handle new URL asset references`, () => { ...@@ -150,9 +161,9 @@ describe(`Handle new URL asset references`, () => {
async () => { async () => {
const browser = await next.browser(page) const browser = await next.browser(page)
await retry(async () => await retry(async () =>
expect(await browser.elementByCss('main').text()).toMatch( expect(
expectedPage stripVercelPngHash(await browser.elementByCss('main').text())
) ).toEqual(expectedPageContent(count))
) )
} }
) )
...@@ -161,13 +172,12 @@ describe(`Handle new URL asset references`, () => { ...@@ -161,13 +172,12 @@ describe(`Handle new URL asset references`, () => {
it('should respond on API', async () => { it('should respond on API', async () => {
const data = await next const data = await next
.fetch('/api/pages/') .fetch('/api/pages/')
.then((res) => res.ok && res.json()) .then((res) => res.ok && res.text())
const json = JSON.parse(stripVercelPngHash(data))
expect(data).toEqual({ expect(json).toEqual({
imported: expect.objectContaining({ imported: clientUrl,
src: clientFilePath, url: serverFileRegex,
}),
url: serverFilePath,
size: 30079, size: 30079,
}) })
}) })
...@@ -175,14 +185,17 @@ describe(`Handle new URL asset references`, () => { ...@@ -175,14 +185,17 @@ describe(`Handle new URL asset references`, () => {
it('should respond on edge API', async () => { it('should respond on edge API', async () => {
const data = await next const data = await next
.fetch('/api/pages-edge/') .fetch('/api/pages-edge/')
.then((res) => res.ok && res.json()) .then((res) => res.ok && res.text())
const json = JSON.parse(stripVercelPngHash(data))
expect(data).toEqual({ expect(json).toEqual({
imported: expect.objectContaining({ imported: clientUrl,
src: clientFilePath,
}),
url: serverEdgeUrl, url: serverEdgeUrl,
}) })
}) })
}) })
}) })
function stripVercelPngHash(text: string) {
return text.replace(/vercel\.[0-9a-f]{8,}\.png/g, 'vercel.HASH.png')
}
...@@ -284,7 +284,7 @@ export class NextInstance { ...@@ -284,7 +284,7 @@ export class NextInstance {
? // since we can't get the build id as a build artifact, ? // since we can't get the build id as a build artifact,
// add it in build logs // add it in build logs
{ {
'post-build': `node -e 'console.log("BUILD" + "_ID: " + require("fs").readFileSync("${this.distDir}/BUILD_ID"))'`, 'post-build': `node -e 'console.log("BUILD" + "_ID: " + fs.readFileSync("${this.distDir}/BUILD_ID") + "\\nDEPLOYMENT_ID: " + process.env.NEXT_DEPLOYMENT_ID)'`,
} }
: {}), : {}),
...pkgScripts, ...pkgScripts,
...@@ -639,6 +639,14 @@ export class NextInstance { ...@@ -639,6 +639,14 @@ export class NextInstance {
return '' return ''
} }
public get deploymentId(): string | undefined {
return undefined
}
public get deploymentIdQuery(): string {
return this.deploymentId ? `?dpl=${this.deploymentId}` : ''
}
public get cliOutput(): string { public get cliOutput(): string {
return '' return ''
} }
......
...@@ -9,6 +9,7 @@ import { Span } from 'next/dist/trace' ...@@ -9,6 +9,7 @@ import { Span } from 'next/dist/trace'
export class NextDeployInstance extends NextInstance { export class NextDeployInstance extends NextInstance {
private _cliOutput: string private _cliOutput: string
private _buildId: string private _buildId: string
private _deploymentId: string | undefined
private _writtenHostsLine: string | null = null private _writtenHostsLine: string | null = null
protected throwIfUnavailable(): void | never { protected throwIfUnavailable(): void | never {
...@@ -33,6 +34,10 @@ export class NextDeployInstance extends NextInstance { ...@@ -33,6 +34,10 @@ export class NextDeployInstance extends NextInstance {
return this._buildId return this._buildId
} }
public get deploymentId() {
return this._deploymentId
}
private async deployUsingCustomScript(): Promise<{ url: string }> { private async deployUsingCustomScript(): Promise<{ url: string }> {
const deployScriptPath = process.env.NEXT_TEST_DEPLOY_SCRIPT_PATH! const deployScriptPath = process.env.NEXT_TEST_DEPLOY_SCRIPT_PATH!
...@@ -162,13 +167,23 @@ export class NextDeployInstance extends NextInstance { ...@@ -162,13 +167,23 @@ export class NextDeployInstance extends NextInstance {
} }
const buildId = this._cliOutput.match(/BUILD_ID: (.+)/)?.[1]?.trim() const buildId = this._cliOutput.match(/BUILD_ID: (.+)/)?.[1]?.trim()
if (!buildId) { if (!buildId) {
throw new Error(`Failed to get buildId from logs ${this._cliOutput}`) throw new Error(`Failed to get buildId from logs ${this._cliOutput}`)
} }
this._buildId = buildId this._buildId = buildId
const deploymentId = this._cliOutput
.match(/DEPLOYMENT_ID: (.+)/)?.[1]
?.trim()
if (!deploymentId) {
throw new Error(
`Failed to get deploymentId from logs ${this._cliOutput}`
)
}
this._deploymentId = deploymentId
require('console').log(`Got buildId: ${this._buildId}`) require('console').log(
`Got buildId: ${this._buildId}, deploymentId: ${this._deploymentId}`
)
return return
} }
......
...@@ -8,6 +8,7 @@ import { quote as shellQuote } from 'shell-quote' ...@@ -8,6 +8,7 @@ import { quote as shellQuote } from 'shell-quote'
export class NextStartInstance extends NextInstance { export class NextStartInstance extends NextInstance {
private _buildId: string private _buildId: string
private _deploymentId: string | undefined
private _cliOutput: string = '' private _cliOutput: string = ''
private _prerenderFinishedTimeMS: number | null = null private _prerenderFinishedTimeMS: number | null = null
...@@ -16,6 +17,10 @@ export class NextStartInstance extends NextInstance { ...@@ -16,6 +17,10 @@ export class NextStartInstance extends NextInstance {
return this._buildId return this._buildId
} }
public get deploymentId() {
return this._deploymentId
}
public get cliOutput() { public get cliOutput() {
return this._cliOutput return this._cliOutput
} }
...@@ -113,6 +118,21 @@ export class NextStartInstance extends NextInstance { ...@@ -113,6 +118,21 @@ export class NextStartInstance extends NextInstance {
) )
.catch(() => '') .catch(() => '')
).trim() ).trim()
try {
const requiredServerFiles = JSON.parse(
await fs.readFile(
path.join(
this.testDir,
this.nextConfig?.distDir || '.next',
'required-server-files.json'
),
'utf8'
)
)
this._deploymentId =
requiredServerFiles.config?.deploymentId || undefined
} catch {}
} }
console.log('running', shellQuote(startArgs)) console.log('running', shellQuote(startArgs))
...@@ -252,6 +272,20 @@ export class NextStartInstance extends NextInstance { ...@@ -252,6 +272,20 @@ export class NextStartInstance extends NextInstance {
.catch(() => '') .catch(() => '')
).trim() ).trim()
try {
const requiredServerFiles = JSON.parse(
await fs.readFile(
path.join(
this.testDir,
this.nextConfig?.distDir || '.next',
'required-server-files.json'
),
'utf8'
)
)
this._deploymentId = requiredServerFiles.config?.deploymentId || undefined
} catch {}
return result return result
} }
......
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