question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Middleware is not applied to static paths (like before)

See original GitHub issue

Verify canary release

  • I verified that the issue exists in Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: x64
      Version: Darwin Kernel Version 21.4.0: Mon Feb 21 20:34:37 PST 2022; root:xnu-8020.101.4~2/RELEASE_X86_64
    Binaries:
      Node: 16.13.0
      npm: 8.1.0
      Yarn: 3.2.0
      pnpm: N/A
    Relevant packages:
      next: 12.1.5-canary.0
      react: 17.0.2
      react-dom: 17.0.2

What browser are you using? (if relevant)

Chrome 99

How are you deploying your application? (if relevant)

aws, next start

Describe the Bug

Opening the issue again because for some reason the previous one was deleted (???)

We are using middleware in order to protect static assets from unauthenticated users.
Until v12.1.3 it was possible to protect them using middleware.
Currently, middleware is not invoked when fetching an asset, e.g. /_next/static/chunks/something.js or /_next/static/media/image.png

Looks like its a result of #32601

Expected Behavior

I do understand that this feature was not “officially” supported, but middleware should be invoked also for static assets.
An idea might be separating page/api middleware and static assets middleware (e.g under _next folder) or a custom configuration that supports it.

To Reproduce

• Add a middleware - pages/_middleware.js • Observe that requests under _next path are not invoked

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
javivelascocommented, Jun 17, 2022

With the most recent canary we have enabled back the ability to run middleware in front of static assets which means that this behaviour is back. Glad to see there are important use cases for it 🎉

Thanks!

0reactions
github-actions[bot]commented, Jul 18, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Having a middleware function before express.static doesnt work
It has this middleware included to serve static files. So the usual code to include this middleware is: app.use(express.static(path.join ...
Read more >
Advanced Features: Middleware - Next.js
Middleware runs before cached content, so you can personalize static files and pages. Common examples of Middleware would be authentication, A/B testing, ...
Read more >
Routing - Laravel - The PHP Framework For Web Artisans
The routes/web.php file defines routes that are for your web interface. These routes are assigned the web middleware group, which provides features like...
Read more >
How to skip Next.js middleware for static and public files
To prevent middleware from running on static files, you can leverage a simple trick: static files have a . in the path, while...
Read more >
Protecting static files in ASP.NET Core using custom middleware
This means that if environment is not Production you allow access to this content. Unfortunately, StaticContent middleware does not (at least ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found