-
Notifications
You must be signed in to change notification settings - Fork 26.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(middleware): issues warnings when using node.js global APIs in m…
…iddleware
- Loading branch information
Showing
6 changed files
with
372 additions
and
27 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
5 changes: 5 additions & 0 deletions
5
test/integration/middleware/overrides-node.js-api/pages/_middleware.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,5 @@ | ||
export default function middleware() { | ||
process.cwd = () => 'fixed-value' | ||
console.log(process.cwd(), process.env) | ||
return new Response() | ||
} |
47 changes: 47 additions & 0 deletions
47
test/integration/middleware/overrides-node.js-api/test/index.test.ts
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,47 @@ | ||
/* eslint-env jest */ | ||
|
||
import { | ||
fetchViaHTTP, | ||
findPort, | ||
killApp, | ||
launchApp, | ||
waitFor, | ||
} from 'next-test-utils' | ||
import { join } from 'path' | ||
|
||
const context = { appDir: join(__dirname, '../'), appPort: NaN, app: null } | ||
|
||
jest.setTimeout(1000 * 60 * 2) | ||
|
||
describe('Middleware overriding a Node.js API', () => { | ||
describe('dev mode', () => { | ||
let output = '' | ||
|
||
beforeAll(async () => { | ||
output = '' | ||
context.appPort = await findPort() | ||
context.app = await launchApp(context.appDir, context.appPort, { | ||
onStdout(msg) { | ||
output += msg | ||
}, | ||
onStderr(msg) { | ||
output += msg | ||
}, | ||
}) | ||
}) | ||
|
||
afterAll(() => killApp(context.app)) | ||
|
||
it('shows a warning but allows overriding', async () => { | ||
const res = await fetchViaHTTP(context.appPort, '/') | ||
await waitFor(500) | ||
expect(res.status).toBe(200) | ||
expect(output) | ||
.toContain(`NodejsRuntimeApiInMiddlewareWarning: You're using a Node.js API (process.cwd) which is not supported in the Edge Runtime that Middleware uses. | ||
Learn more: https://nextjs.org/docs/api-reference/edge-runtime`) | ||
expect(output).toContain('fixed-value') | ||
expect(output).not.toContain('TypeError') | ||
expect(output).not.toContain(`You're using a Node.js API (process.env)`) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.