next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Middleware does not work in Next.js 13 when the /pages directory is removed

Open jamescmartinez opened this issue 3 years ago • 1 comments

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

  1. npm run dev to start the app
  2. open http://localhost:3000/page2 to open /page2 in your browser, noting that /page2 does not redirect to / as configured in middleware.ts
  3. stop the dev server
  4. mkdir pages to create an empty pages directory
  5. npm run dev to start the app
  6. open http://localhost:3000/page2 to open /page2 in your browser, noting that /page2 now redirects to /

jamescmartinez avatar Oct 27 '22 18:10 jamescmartinez

I ran into this as well. Had to add a pages/.keep for deployment.

jensen avatar Oct 29 '22 15:10 jensen

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.

github-actions[bot] avatar Dec 22 '22 00:12 github-actions[bot]