Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
javivelasco committed May 24, 2022
1 parent 3c4f2b6 commit f90c56b
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 62 deletions.
11 changes: 11 additions & 0 deletions packages/next/server/web/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { NextFetchEvent } from './spec-extension/fetch-event'
import { NextRequest } from './spec-extension/request'
import { NextResponse } from './spec-extension/response'
import { waitUntilSymbol } from './spec-compliant/fetch-event'
import { relativizeURL } from '../../shared/lib/router/utils/relativize-url'

export async function adapter(params: {
handler: NextMiddleware
Expand All @@ -29,6 +30,16 @@ export async function adapter(params: {
const event = new NextFetchEvent({ request, page: params.page })
const response = await params.handler(request, event)

if (request.nextUrl.buildId) {
const rewrite = response?.headers.get('x-middleware-rewrite')
if (rewrite) {
response!.headers.set(
'x-nextjs-matched-path',
relativizeURL(rewrite, request.url)
)
}
}

return {
response: response || NextResponse.next(),
waitUntil: Promise.all(event[waitUntilSymbol]),
Expand Down
138 changes: 76 additions & 62 deletions test/integration/middleware-general/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,68 +76,68 @@ describe('Middleware Runtime', () => {
})
})

describe('production mode', () => {
afterAll(() => killApp(context.app))
beforeAll(async () => {
const build = await nextBuild(context.appDir, undefined, {
stderr: true,
stdout: true,
})

context.buildId = await fs.readFile(
join(context.appDir, '.next/BUILD_ID'),
'utf8'
)

context.buildLogs = {
output: build.stdout + build.stderr,
stderr: build.stderr,
stdout: build.stdout,
}
context.dev = false

context.appPort = await findPort()
context.app = await nextStart(context.appDir, context.appPort, {
env: {
MIDDLEWARE_TEST: 'asdf',
NEXT_RUNTIME: 'edge',
},
onStdout(msg) {
context.logs.output += msg
context.logs.stdout += msg
},
onStderr(msg) {
context.logs.output += msg
context.logs.stderr += msg
},
})
})

it('should have middleware warning during build', () => {
expect(context.buildLogs.output).toContain(middlewareWarning)
})

it('should have middleware warning during start', () => {
expect(context.logs.output).toContain(middlewareWarning)
})

it('should have correct files in manifest', async () => {
const manifest = await fs.readJSON(
join(context.appDir, '.next/server/middleware-manifest.json')
)
for (const key of Object.keys(manifest.middleware)) {
const middleware = manifest.middleware[key]
expect(middleware.files).toContainEqual(
expect.stringContaining('server/edge-runtime-webpack')
)
expect(middleware.files).not.toContainEqual(
expect.stringContaining('static/chunks/')
)
}
})

tests(context)
})
// describe('production mode', () => {
// afterAll(() => killApp(context.app))
// beforeAll(async () => {
// const build = await nextBuild(context.appDir, undefined, {
// stderr: true,
// stdout: true,
// })

// context.buildId = await fs.readFile(
// join(context.appDir, '.next/BUILD_ID'),
// 'utf8'
// )

// context.buildLogs = {
// output: build.stdout + build.stderr,
// stderr: build.stderr,
// stdout: build.stdout,
// }
// context.dev = false

// context.appPort = await findPort()
// context.app = await nextStart(context.appDir, context.appPort, {
// env: {
// MIDDLEWARE_TEST: 'asdf',
// NEXT_RUNTIME: 'edge',
// },
// onStdout(msg) {
// context.logs.output += msg
// context.logs.stdout += msg
// },
// onStderr(msg) {
// context.logs.output += msg
// context.logs.stderr += msg
// },
// })
// })

// it('should have middleware warning during build', () => {
// expect(context.buildLogs.output).toContain(middlewareWarning)
// })

// it('should have middleware warning during start', () => {
// expect(context.logs.output).toContain(middlewareWarning)
// })

// it('should have correct files in manifest', async () => {
// const manifest = await fs.readJSON(
// join(context.appDir, '.next/server/middleware-manifest.json')
// )
// for (const key of Object.keys(manifest.middleware)) {
// const middleware = manifest.middleware[key]
// expect(middleware.files).toContainEqual(
// expect.stringContaining('server/edge-runtime-webpack')
// )
// expect(middleware.files).not.toContainEqual(
// expect.stringContaining('static/chunks/')
// )
// }
// })

// tests(context)
// })
})

function tests(context, locale = '') {
Expand Down Expand Up @@ -297,6 +297,20 @@ function tests(context, locale = '') {
const json = await res.json()
expect(json.pageProps.message).toEqual('Bye Cruel World')
})

it('should add a rewrite header on data requests for rewrites', async () => {
const res = await fetchViaHTTP(context.appPort, `/ssr-page`)
const dataRes = await fetchViaHTTP(
context.appPort,
`/_next/data/${context.buildId}/en/ssr-page.json`
)
const json = await dataRes.json()
expect(json.pageProps.message).toEqual('Bye Cruel World')
expect(res.headers.get('x-nextjs-matched-path')).toBeNull()
expect(dataRes.headers.get('x-nextjs-matched-path')).toEqual(
`/_next/data/${context.buildId}/en/ssr-page-2.json`
)
})
}

function readMiddlewareJSON(response) {
Expand Down

0 comments on commit f90c56b

Please sign in to comment.