next.js
next.js copied to clipboard
Middleware does not work in Next.js 13 when the /pages directory is removed
Verify canary release
- [X] I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 21.6.0: Mon Aug 22 20:20:05 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T8101
Binaries:
Node: 18.10.0
npm: 8.19.2
Yarn: N/A
pnpm: N/A
Relevant packages:
next: 13.0.1-canary.0
eslint-config-next: 13.0.0
react: 18.2.0
react-dom: 18.2.0
What browser are you using? (if relevant)
106.0.5249.119
How are you deploying your application? (if relevant)
n/a
Describe the Bug
Removing the pages directory prevents Next.js middleware from running.
Expected Behavior
The middleware should work with the new app directory when the pages directory does not exist.
Link to reproduction
https://github.com/jamescmartinez/nextjs13-middlware-appdir-bug
To Reproduce
-
npm run devto start the app -
open http://localhost:3000/page2to open/page2in your browser, noting that/page2does not redirect to/as configured inmiddleware.ts - stop the dev server
-
mkdir pagesto create an empty pages directory -
npm run devto start the app -
open http://localhost:3000/page2to open/page2in your browser, noting that/page2now redirects to/
I ran into this as well. Had to add a pages/.keep for deployment.
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.