Skip to content
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

Fix Failed to copy traced files for Edge functions and handle its files with middleware-manifest.json #43326

Merged
merged 9 commits into from Dec 1, 2022
8 changes: 2 additions & 6 deletions packages/next/build/index.ts
Expand Up @@ -868,11 +868,6 @@ export default async function build(
)

const manifestPath = path.join(distDir, SERVER_DIRECTORY, PAGES_MANIFEST)
const appManifestPath = path.join(
distDir,
SERVER_DIRECTORY,
APP_PATHS_MANIFEST
)

const requiredServerFiles = nextBuildSpan
.traceChild('generate-required-server-files')
Expand Down Expand Up @@ -912,7 +907,8 @@ export default async function build(
),
]
: []),
path.relative(distDir, appManifestPath),
path.join(SERVER_DIRECTORY, APP_PATHS_MANIFEST),
APP_BUILD_MANIFEST,
path.join(SERVER_DIRECTORY, FLIGHT_MANIFEST + '.js'),
path.join(SERVER_DIRECTORY, FLIGHT_MANIFEST + '.json'),
path.join(
Expand Down
30 changes: 30 additions & 0 deletions packages/next/build/utils.ts
Expand Up @@ -1669,7 +1669,33 @@ export async function copyTracedFiles(
}
}

for (const page of Object.values(middlewareManifest.functions)) {
for (const file of page.files) {
const originalPath = path.join(distDir, file)
const fileOutputPath = path.join(
outputPath,
path.relative(tracingRoot, distDir),
file
)
await fs.mkdir(path.dirname(fileOutputPath), { recursive: true })
await fs.copyFile(originalPath, fileOutputPath)
}
for (const file of [...(page.wasm ?? []), ...(page.assets ?? [])]) {
const originalPath = path.join(distDir, file.filePath)
const fileOutputPath = path.join(
outputPath,
path.relative(tracingRoot, distDir),
file.filePath
)
await fs.mkdir(path.dirname(fileOutputPath), { recursive: true })
await fs.copyFile(originalPath, fileOutputPath)
}
}

for (const page of pageKeys) {
if (middlewareManifest.functions.hasOwnProperty(page)) {
continue
}
const pageFile = path.join(
distDir,
'server',
Expand All @@ -1683,6 +1709,9 @@ export async function copyTracedFiles(
}
if (appPageKeys) {
for (const page of appPageKeys) {
if (middlewareManifest.functions.hasOwnProperty(page)) {
continue
}
const pageFile = path.join(distDir, 'server', 'app', `${page}`, 'page.js')
const pageTraceFile = `${pageFile}.nft.json`
await handleTraceFiles(pageTraceFile).catch((err) => {
Expand Down Expand Up @@ -1760,6 +1789,7 @@ server.listen(currentPort, (err) => {
})`
)
}

export function isReservedPage(page: string) {
return RESERVED_PAGE.test(page)
}
Expand Down
18 changes: 18 additions & 0 deletions test/e2e/og-api/index.test.ts
@@ -1,6 +1,7 @@
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { fetchViaHTTP, renderViaHTTP } from 'next-test-utils'
import fs from 'fs-extra'
import { join } from 'path'

describe('og-api', () => {
Expand All @@ -27,4 +28,21 @@ describe('og-api', () => {
const body = await res.blob()
expect(body.size).toBeGreaterThan(0)
})

if ((global as any).isNextStart) {
it('should copy files correctly', async () => {
expect(next.cliOutput).not.toContain('Failed to copy traced files')

expect(
await fs.pathExists(
join(next.testDir, '.next/standalone/.next/server/pages/api/og.js')
)
).toBe(true)
expect(
await fs.pathExists(
join(next.testDir, '.next/standalone/.next/server/edge-chunks')
)
).toBe(true)
})
}
})