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

Fixed middleware's edge-chunks not being copied in copyTracedFiles #48723

Merged
merged 23 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
bb4366e
Fixed middleware's edge-chunks not being copied in copyTracedFiles
DuCanhGH Apr 22, 2023
5a4ef49
unrelated changes
DuCanhGH Apr 23, 2023
1760b29
Merge branch 'canary' into ducanhgh-fix-middleware-edge-chunks
DuCanhGH Apr 23, 2023
cf2cb22
Merge branch 'canary' into ducanhgh-fix-middleware-edge-chunks
DuCanhGH Apr 24, 2023
d3dc01b
Merge branch 'canary' into ducanhgh-fix-middleware-edge-chunks
DuCanhGH Apr 24, 2023
903223b
Merge branch 'canary' into ducanhgh-fix-middleware-edge-chunks
DuCanhGH Apr 24, 2023
a2d1232
Merge branch 'canary' into ducanhgh-fix-middleware-edge-chunks
DuCanhGH Apr 25, 2023
41253d0
add test
DuCanhGH Apr 26, 2023
a398d07
Merge branch 'canary' into ducanhgh-fix-middleware-edge-chunks
DuCanhGH Apr 26, 2023
26575bb
Merge branch 'canary' into ducanhgh-fix-middleware-edge-chunks
DuCanhGH Apr 26, 2023
4c988cb
Merge branch 'canary' into ducanhgh-fix-middleware-edge-chunks
DuCanhGH Apr 26, 2023
d317ee9
Merge branch 'canary' into ducanhgh-fix-middleware-edge-chunks
DuCanhGH Apr 27, 2023
4e448e1
Merge branch 'canary' into ducanhgh-fix-middleware-edge-chunks
DuCanhGH Apr 27, 2023
2b6dfbc
Merge branch 'canary' into ducanhgh-fix-middleware-edge-chunks
DuCanhGH Apr 27, 2023
2a695b1
Merge branch 'canary' into ducanhgh-fix-middleware-edge-chunks
DuCanhGH Apr 28, 2023
3e18ec3
Merge branch 'canary' into ducanhgh-fix-middleware-edge-chunks
DuCanhGH Apr 30, 2023
6f76645
use Promise.all, check if name is middleware
DuCanhGH May 1, 2023
d8a3cbd
Merge branch 'canary' into ducanhgh-fix-middleware-edge-chunks
DuCanhGH May 1, 2023
962312f
Merge branch 'canary' into ducanhgh-fix-middleware-edge-chunks
DuCanhGH May 1, 2023
b97c7e0
Merge branch 'canary' into ducanhgh-fix-middleware-edge-chunks
shuding May 1, 2023
0b5c054
Merge branch 'canary' into ducanhgh-fix-middleware-edge-chunks
shuding May 1, 2023
7731067
Merge branch 'canary' into ducanhgh-fix-middleware-edge-chunks
DuCanhGH May 1, 2023
fe6ed22
Merge branch 'canary' into ducanhgh-fix-middleware-edge-chunks
DuCanhGH May 1, 2023
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
51 changes: 24 additions & 27 deletions packages/next/src/build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import type {
CustomRoutes,
} from '../lib/load-custom-routes'
import type { UnwrapPromise } from '../lib/coalesced-function'
import type { MiddlewareManifest } from './webpack/plugins/middleware-plugin'
import type {
EdgeFunctionDefinition,
MiddlewareManifest,
} from './webpack/plugins/middleware-plugin'
import type { AppRouteUserlandModule } from '../server/future/route-modules/app-route/module'
import type { StaticGenerationAsyncStorage } from '../client/components/static-generation-async-storage'

Expand Down Expand Up @@ -1834,23 +1837,8 @@ export async function copyTracedFiles(
)
}

for (const middleware of Object.values(middlewareManifest.middleware) || []) {
if (isMiddlewareFilename(middleware.name)) {
for (const file of middleware.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 page of Object.values(middlewareManifest.functions)) {
for (const file of page.files) {
async function handleEdgeFunction(page: EdgeFunctionDefinition) {
async function handleFile(file: string) {
const originalPath = path.join(distDir, file)
const fileOutputPath = path.join(
outputPath,
Expand All @@ -1860,18 +1848,27 @@ export async function copyTracedFiles(
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)
await Promise.all([
page.files.map(handleFile),
page.wasm?.map((file) => handleFile(file.filePath)),
page.assets?.map((file) => handleFile(file.filePath)),
])
}

const edgeFunctionHandlers: Promise<any>[] = []

for (const middleware of Object.values(middlewareManifest.middleware)) {
if (isMiddlewareFilename(middleware.name)) {
edgeFunctionHandlers.push(handleEdgeFunction(middleware))
}
}

for (const page of Object.values(middlewareManifest.functions)) {
edgeFunctionHandlers.push(handleEdgeFunction(page))
}

await Promise.all(edgeFunctionHandlers)

for (const page of pageKeys) {
if (middlewareManifest.functions.hasOwnProperty(page)) {
continue
Expand Down
32 changes: 18 additions & 14 deletions packages/next/src/server/image-optimizer.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
import { mediaType } from 'next/dist/compiled/@hapi/accept'
import { createHash } from 'crypto'
import { promises } from 'fs'
import type { IncomingMessage, ServerResponse } from 'http'
import { mediaType } from 'next/dist/compiled/@hapi/accept'
import chalk from 'next/dist/compiled/chalk'
import contentDisposition from 'next/dist/compiled/content-disposition'
import { getOrientation, Orientation } from 'next/dist/compiled/get-orientation'
import imageSizeOf from 'next/dist/compiled/image-size'
import { IncomingMessage, ServerResponse } from 'http'
import isAnimated from 'next/dist/compiled/is-animated'
import contentDisposition from 'next/dist/compiled/content-disposition'
import { join } from 'path'
import nodeUrl, { UrlWithParsedQuery } from 'url'
import { NextConfigComplete } from './config-shared'
import nodeUrl, { type UrlWithParsedQuery } from 'url'

import { getImageBlurSvg } from '../shared/lib/image-blur-svg'
import type { ImageConfigComplete } from '../shared/lib/image-config'
import { hasMatch } from '../shared/lib/match-remote-pattern'
import type { NextConfigComplete } from './config-shared'
import { createRequestResponseMocks } from './lib/mock-request'
// Do not import anything other than types from this module
// because it will throw an error when using `outputFileTracing`
// because `jest-worker` is ignored in file tracing. Use `await import`
// as `jest-worker` is ignored in file tracing. Use `await import`
// or `require` instead.
import { Operation } from './lib/squoosh/main'
import type { Operation } from './lib/squoosh/main'
import type { NextUrlWithParsedQuery } from './request-meta'
import type {
IncrementalCacheEntry,
IncrementalCacheValue,
} from './response-cache'
import { sendEtagResponse } from './send-payload'
import { getContentType, getExtension } from './serve-static'
import chalk from 'next/dist/compiled/chalk'
import { NextUrlWithParsedQuery } from './request-meta'
import { IncrementalCacheEntry, IncrementalCacheValue } from './response-cache'
import { createRequestResponseMocks } from './lib/mock-request'
import { hasMatch } from '../shared/lib/match-remote-pattern'
import { getImageBlurSvg } from '../shared/lib/image-blur-svg'
import { ImageConfigComplete } from '../shared/lib/image-config'

type XCacheHeader = 'MISS' | 'HIT' | 'STALE'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { NextResponse } from 'next/server'
import { ImageResponse, NextResponse } from 'next/server'

export async function middleware(req) {
console.log('middleware', req.url)
if (req.nextUrl.pathname === '/a-non-existent-page/to-test-with-middleware') {
return new ImageResponse(<div>Hello world</div>, {
width: 1200,
height: 600,
})
}
return NextResponse.next()
}
Original file line number Diff line number Diff line change
Expand Up @@ -1254,12 +1254,14 @@ describe('should set-up next', () => {
expect(envVariables.envLocal).toBeUndefined()
})

it('should run middleware correctly without minimalMode', async () => {
it('should run middleware correctly (without minimalMode, with wasm)', async () => {
await next.destroy()
await killApp(server)
await setupNext({ nextEnv: false, minimalMode: false })

const testServer = join(next.testDir, 'standalone/server.js')
const standaloneDir = join(next.testDir, 'standalone')

const testServer = join(standaloneDir, 'server.js')
await fs.writeFile(
testServer,
(
Expand Down Expand Up @@ -1290,9 +1292,19 @@ describe('should set-up next', () => {
expect(res.status).toBe(200)
expect(await res.text()).toContain('index page')

expect(fs.existsSync(join(standaloneDir, '.next/server/edge-chunks'))).toBe(
true
)

const resImageResponse = await fetchViaHTTP(
appPort,
'/a-non-existent-page/to-test-with-middleware'
)

expect(resImageResponse.status).toBe(200)
expect(resImageResponse.headers.get('content-type')).toBe('image/png')

// when not in next env should be compress: true
expect(
await fs.readFileSync(join(next.testDir, 'standalone/server.js'), 'utf8')
).toContain('"compress":true')
expect(fs.readFileSync(testServer, 'utf8')).toContain('"compress":true')
})
})