Open cerimorse opened 1 year ago
Hi @cerimorse
I was able to resolve this by checking for the request header "x-forwarded-host". When using a custom domain this header was accessible through the Nextjs middleware, when a default URL is used (ending with .azurestaticwebapp.net) the header is not there. So I was able to add a redirect to the custom domain.
Same issue but cannot access the X-Forwarded-Host
header from next.config.js
.
/** @type {import('next').NextConfig} */
const config = {
output: 'standalone',
async redirects() {
return [
{
source: '/',
destination: 'https://contoso.com/products',
has: [{ type: 'host', value: 'contoso.app' }],
permanent: false,
},
];
},
};
export default config;
Removing the host condition in has
works but that's not the intended behaviour.
@thomasgauvin seen you writing posts recently so I thought I could tag you on this one. Since this has been pending for a while is there any particular guideline on how such situations should be handled?
Yep, would be great to use has
with type: 'host'
.
The Problem
There doesn't seem to be a way to access the original host when using Next.js middleware as part of the recently released hybrid deployment. We're currently using the middleware layer to rewrite the URL pathname based on the subdomain. To do this we need access to the original host name.
To Reproduce
Add a middleware layer to any Next.js hybrid deployment and observe the
host
header. A basic middleware implementation, taken from a Vercel hostname rewrites example, might look like this:When running this locally, the
host
header correctly returns the expected host name. When deploying to Azure Static Web Apps using the Next.js hybrid deployment, thehost
header returns a value similar to<randomly-generated-string>.azurewebsites.net
. I assume this is thehost
value of the build time generated managed function app that the middleware is running on.Expected Result
I think there are two possible expected results:
host
header should reflect the domain name used to access the static web appX-Forwarded-Host
headerPossible Solution
I can see that there is already the ability to access the original url via the
x-ms-original-url
header that was added in this pull request. If this is already possible, hopefully it shouldn't be too difficult to add theX-Forwarded-Host
header in a similar manner (unless I've overlooked something!).