Skip to content

Commit 82e7652

Browse files
committed
fix(router): no html 404 unless wanted
1 parent 90d2816 commit 82e7652

File tree

7 files changed

+33
-21
lines changed

7 files changed

+33
-21
lines changed

packages/qwik-router/src/middleware/azure-swa/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@ export function createQwikRouter(opts: QwikRouterAzureOptions): AzureFunction {
128128
// In the development server, we replace the getNotFound function
129129
// For static paths, we assign a static "Not Found" message.
130130
// This ensures consistency between development and production environments for specific URLs.
131-
const notFoundHtml = isStaticPath(req.method || 'GET', url)
132-
? 'Not Found'
133-
: getNotFound(url.pathname);
131+
const notFoundHtml =
132+
!req.headers.accept?.includes('text/html') || isStaticPath(req.method || 'GET', url)
133+
? 'Not Found'
134+
: getNotFound(url.pathname);
134135
return {
135136
status: 404,
136137
headers: { 'Content-Type': 'text/html; charset=utf-8', 'X-Not-Found': url.pathname },

packages/qwik-router/src/middleware/bun/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ export function createQwikRouter(opts: QwikRouterBunOptions) {
103103
// In the development server, we replace the getNotFound function
104104
// For static paths, we assign a static "Not Found" message.
105105
// This ensures consistency between development and production environments for specific URLs.
106-
const notFoundHtml = isStaticPath(request.method || 'GET', url)
107-
? 'Not Found'
108-
: getNotFound(url.pathname);
106+
const notFoundHtml =
107+
!request.headers.get('accept')?.includes('text/html') ||
108+
isStaticPath(request.method || 'GET', url)
109+
? 'Not Found'
110+
: getNotFound(url.pathname);
109111
return new Response(notFoundHtml, {
110112
status: 404,
111113
headers: { 'Content-Type': 'text/html; charset=utf-8', 'X-Not-Found': url.pathname },

packages/qwik-router/src/middleware/cloudflare-pages/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,11 @@ export function createQwikRouter(opts: QwikRouterCloudflarePagesOptions) {
115115
// In the development server, we replace the getNotFound function
116116
// For static paths, we assign a static "Not Found" message.
117117
// This ensures consistency between development and production environments for specific URLs.
118-
const notFoundHtml = isStaticPath(request.method || 'GET', url)
119-
? 'Not Found'
120-
: getNotFound(url.pathname);
118+
const notFoundHtml =
119+
!request.headers.get('accept')?.includes('text/html') ||
120+
isStaticPath(request.method || 'GET', url)
121+
? 'Not Found'
122+
: getNotFound(url.pathname);
121123
return new Response(notFoundHtml, {
122124
status: 404,
123125
headers: { 'Content-Type': 'text/html; charset=utf-8', 'X-Not-Found': url.pathname },

packages/qwik-router/src/middleware/deno/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,11 @@ export function createQwikRouter(opts: QwikRouterDenoOptions) {
104104
// In the development server, we replace the getNotFound function
105105
// For static paths, we assign a static "Not Found" message.
106106
// This ensures consistency between development and production environments for specific URLs.
107-
const notFoundHtml = isStaticPath(request.method || 'GET', url)
108-
? 'Not Found'
109-
: getNotFound(url.pathname);
107+
const notFoundHtml =
108+
!request.headers.get('accept')?.includes('text/html') ||
109+
isStaticPath(request.method || 'GET', url)
110+
? 'Not Found'
111+
: getNotFound(url.pathname);
110112
return new Response(notFoundHtml, {
111113
status: 404,
112114
headers: { 'Content-Type': 'text/html; charset=utf-8', 'X-Not-Found': url.pathname },

packages/qwik-router/src/middleware/netlify-edge/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ export function createQwikRouter(opts: QwikRouterNetlifyOptions) {
7777
// In the development server, we replace the getNotFound function
7878
// For static paths, we assign a static "Not Found" message.
7979
// This ensures consistency between development and production environments for specific URLs.
80-
const notFoundHtml = isStaticPath(request.method || 'GET', url)
81-
? 'Not Found'
82-
: getNotFound(url.pathname);
80+
const notFoundHtml =
81+
!request.headers.get('accept')?.includes('text/html') ||
82+
isStaticPath(request.method || 'GET', url)
83+
? 'Not Found'
84+
: getNotFound(url.pathname);
8385
return new Response(notFoundHtml, {
8486
status: 404,
8587
headers: { 'Content-Type': 'text/html; charset=utf-8', 'X-Not-Found': url.pathname },

packages/qwik-router/src/middleware/node/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@ export function createQwikRouter(opts: QwikRouterNodeRequestOptions | QwikCityNo
7272
// In the development server, we replace the getNotFound function
7373
// For static paths, we assign a static "Not Found" message.
7474
// This ensures consistency between development and production environments for specific URLs.
75-
const notFoundHtml = isStaticPath(req.method || 'GET', url)
76-
? 'Not Found'
77-
: getNotFound(url.pathname);
75+
const notFoundHtml =
76+
!req.headers.accept?.includes('text/html') || isStaticPath(req.method || 'GET', url)
77+
? 'Not Found'
78+
: getNotFound(url.pathname);
7879
res.writeHead(404, {
7980
'Content-Type': 'text/html; charset=utf-8',
8081
'X-Not-Found': url.pathname,

packages/qwik-router/src/middleware/vercel-edge/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,11 @@ export function createQwikRouter(opts: QwikRouterVercelEdgeOptions) {
104104
// In the development server, we replace the getNotFound function
105105
// For static paths, we assign a static "Not Found" message.
106106
// This ensures consistency between development and production environments for specific URLs.
107-
const notFoundHtml = isStaticPath(request.method || 'GET', url)
108-
? 'Not Found'
109-
: getNotFound(url.pathname);
107+
const notFoundHtml =
108+
!request.headers.get('accept')?.includes('text/html') ||
109+
isStaticPath(request.method || 'GET', url)
110+
? 'Not Found'
111+
: getNotFound(url.pathname);
110112
return new Response(notFoundHtml, {
111113
status: 404,
112114
headers: { 'Content-Type': 'text/html; charset=utf-8', 'X-Not-Found': url.pathname },

0 commit comments

Comments
 (0)