Azure / Azure/static-web-apps

Azure Static Web Apps Domain Routing Bug

Open
#1,642 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
346
Forks
67
PR merge metrics
No merged PRs in 30d

Description

### Azure Static Web Apps Domain Routing Bug Description

**Issue Summary**
Azure Static Web Apps exhibits a critical bug with multi-domain support where the first domain accessed after deployment "sticks" in header values for all subsequent requests, regardless of which custom domain is actually being used to access the application.

**Detailed Description**
When deploying a Next.js application to Azure Static Web Apps with multiple custom domains configured, the infrastructure incorrectly caches the first domain accessed after deployment. All standard domain detection headers (`x-forwarded-host`, `x-ms-original-host`, `x-ms-original-url`, etc.) then show this first domain for all subsequent requests, preventing correct domain-based routing in middleware.

For example:

- After deployment, if the first request comes to `site-a.example.com`
- All subsequent requests to `site-b.example.com` will have headers showing `site-a.example.com`
- This occurs even when directly accessing the site via a different domain in a new session/browser

**Steps to Reproduce**
- Deploy a Next.js application to Azure Static Web Apps
- Configure multiple custom domains (e.g., `site-a.example.com` and `site-b.example.com`)
- Implement Next.js middleware that attempts to detect the domain using request headers
- Access one domain after deployment (e.g., `site-a.example.com`)
- Access a different domain (e.g., `site-b.example.com`)
- Observe headers in middleware show the first accessed domain regardless of which domain is used

**Impact**
This bug makes it impossible to reliably implement domain-based routing using middleware in Azure Static Web Apps when multiple custom domains are configured. Developers are forced to implement client-side domain detection and routing, which causes flicker, SEO issues, and a poor user experience as the page must first load before redirecting to the correct domain-specific content.

**Diagnostic Information**
The following headers all consistently return the first accessed domain after deployment:

- `x-forwarded-host`
- `x-ms-original-host`
- `x-ms-original-url`
- `host`

**Current Workarounds**
The only reliable workaround is implementing client-side detection with JavaScript after the page loads, which has significant drawbacks:

- Initial page load shows incorrect content
- Flash of wrong content before redirect
- SEO issues due to incorrect initial content
- Poor user experience

**Environment**
- Next.js 15.x
- Azure Static Web Apps
- Multiple custom domains configured
- Using Next.js middleware for routing

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.