Next.js Discord

Discord Forum

middleware and host

Unanswered
Japanese pilchard posted this in #help-forum
Open in Discord
Japanese pilchardOP
Hi there,

Is it possible to create a matcher to run the middleware only if it does not match a specific hostname. We manage custom domain and we have specific redirection to do on some routes. We would like to run the middleware only for these custom domain, not our main domain.

I did not see anything that talk about this, is this possible?

5 Replies

Japanese pilchardOP
@joulev thank you! That would be a great addition, for e-commerce that usually support custom domain, that’s nice to be able to trigger the middleware based on the host. I currently do that in the middleware itself but if it was matched on the host, that would improve the performances 🙂
@Japanese pilchard <@484037068239142956> thank you! That would be a great addition, for e-commerce that usually support custom domain, that’s nice to be able to trigger the middleware based on the host. I currently do that in the middleware itself but if it was matched on the host, that would improve the performances 🙂
i use a cloudflare worker to direct the custom domains to the pathname first, so the nextjs app doesn't need to care about the hostname. in that system, filtering by the matcher is possible. but depending on the tech you use, that may not be possible – and there isn't much you can do about it
Japanese pilchardOP
Unfortunately I cannot really move the logic to cloudflare but the worker would have been a good solution otherwise.
yeah as long as you perform the multi tenancy matching inside the middleware itself, it is impossible. the middleware matcher only filters based on pathname