-
Notifications
You must be signed in to change notification settings - Fork 27.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure src/middleware handles correctly (#75702)
This is a follow-up to #75624 which ensures middleware behaves correctly in the `src/` directory. This previously wasn't an issue since edge runtime output location isn't picky since it's just output in the middleware-manifest although with node runtime it is particular.
- Loading branch information
Showing
8 changed files
with
169 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
experimental: { | ||
nodeMiddleware: true, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { NextResponse } from 'next/server' | ||
|
||
export const config = { | ||
runtime: 'nodejs', | ||
} | ||
|
||
export default function () { | ||
const response = NextResponse.next() | ||
response.headers.set('X-From-Src-Middleware', 'true') | ||
return response | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { NextResponse } from 'next/server' | ||
|
||
export const config = { | ||
runtime: 'nodejs', | ||
} | ||
|
||
export default function () { | ||
const response = NextResponse.next() | ||
response.headers.set('X-From-Src-Middleware-TS', 'true') | ||
return response | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const Page = () => <h1>Hi from SRC</h1> | ||
export default Page |
130 changes: 130 additions & 0 deletions
130
test/integration/middleware-src-node/test/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* eslint-env jest */ | ||
|
||
import fs from 'fs-extra' | ||
import { join } from 'path' | ||
import { | ||
fetchViaHTTP, | ||
File, | ||
findPort, | ||
launchApp, | ||
killApp, | ||
nextBuild, | ||
} from 'next-test-utils' | ||
|
||
let app | ||
let appPort | ||
const appDir = join(__dirname, '../') | ||
const nextConfig = new File(join(appDir, 'next.config.js')) | ||
const srcHeader = 'X-From-Src-Middleware' | ||
const rootHeader = 'X-From-Root-Middleware' | ||
const rootMiddlewareJSFile = join(appDir, 'middleware.js') | ||
const rootMiddlewareTSFile = join(appDir, 'middleware.ts') | ||
|
||
function runSingleMiddlewareTests() { | ||
it('loads an runs src middleware', async () => { | ||
const response = await fetchViaHTTP(appPort, '/post-1') | ||
expect(response.headers.has(srcHeader)).toBe(false) | ||
expect(response.headers.has(`${srcHeader}-TS`)).toBe(true) | ||
}) | ||
} | ||
|
||
function runDoubleMiddlewareTests() { | ||
it('loads and runs only root middleware', async () => { | ||
const response = await fetchViaHTTP(appPort, '/post-1') | ||
expect(response.headers.has(srcHeader)).toBe(false) | ||
expect(response.headers.has(`${srcHeader}-TS`)).toBe(false) | ||
expect(response.headers.has(rootHeader)).toBe(false) | ||
expect(response.headers.has(`${rootHeader}-TS`)).toBe(true) | ||
}) | ||
} | ||
|
||
async function writeRootMiddleware() { | ||
await fs.copy(join(appDir, 'src/pages'), join(appDir, 'pages'), { | ||
force: true, | ||
recursive: true, | ||
}) | ||
await fs.writeFile( | ||
rootMiddlewareJSFile, | ||
` | ||
import { NextResponse } from 'next/server' | ||
export default function () { | ||
const response = NextResponse.next() | ||
response.headers.set('${rootHeader}', 'true') | ||
return response | ||
}` | ||
) | ||
await fs.writeFile( | ||
rootMiddlewareTSFile, | ||
` | ||
import { NextResponse } from 'next/server' | ||
export default function () { | ||
const response = NextResponse.next() | ||
response.headers.set('${rootHeader}-TS', 'true') | ||
return response | ||
}` | ||
) | ||
} | ||
|
||
async function removeRootMiddleware() { | ||
await fs.remove(rootMiddlewareJSFile, { force: true }) | ||
await fs.remove(rootMiddlewareTSFile, { force: true }) | ||
await fs.remove(join(appDir, 'pages'), { force: true, recursive: true }) | ||
} | ||
|
||
describe.each([ | ||
{ | ||
title: 'Middleware in src/ folder', | ||
setup() {}, | ||
teardown() {}, | ||
runTest: runSingleMiddlewareTests, | ||
}, | ||
{ | ||
title: 'Middleware in src/ and / folders', | ||
setup: writeRootMiddleware, | ||
teardown: removeRootMiddleware, | ||
runTest: runDoubleMiddlewareTests, | ||
}, | ||
])('$title', ({ setup, teardown, runTest }) => { | ||
beforeAll(() => setup()) | ||
afterAll(() => teardown()) | ||
;(process.env.TURBOPACK_BUILD ? describe.skip : describe)( | ||
'development mode', | ||
() => { | ||
beforeAll(async () => { | ||
appPort = await findPort() | ||
app = await launchApp(appDir, appPort) | ||
}) | ||
afterAll(() => killApp(app)) | ||
|
||
runTest() | ||
} | ||
) | ||
;(process.env.TURBOPACK_DEV ? describe.skip : describe)( | ||
'production mode', | ||
() => { | ||
let exportOutput = '' | ||
|
||
beforeAll(async () => { | ||
nextConfig.write(`module.exports = { output: 'export' }`) | ||
const result = await nextBuild(appDir, [], { | ||
stderr: true, | ||
stdout: true, | ||
}) | ||
|
||
const outdir = join(__dirname, '..', 'out') | ||
await fs.remove(outdir).catch(() => {}) | ||
|
||
exportOutput = result.stderr + result.stdout | ||
}) | ||
afterAll(() => nextConfig.restore()) | ||
|
||
it('should warn about middleware on export', async () => { | ||
expect(exportOutput).toContain( | ||
'Statically exporting a Next.js application via `next export` disables API routes and middleware.' | ||
) | ||
}) | ||
} | ||
) | ||
}) |