Skip to content

Commit 383b842

Browse files
committed
fix: skip BC route resolution for 404 path
1 parent 8096cc5 commit 383b842

1 file changed

Lines changed: 30 additions & 4 deletions

File tree

core/middlewares/with-routes.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,15 @@ const getRouteInfo = async (request: NextRequest, event: NextFetchEvent) => {
248248
// For route resolution parity, we need to also include query params, otherwise certain redirects will not work.
249249
const pathname = clearLocaleFromPath(request.nextUrl.pathname + request.nextUrl.search, locale);
250250

251+
// Skip BigCommerce route resolution for /404 paths to avoid GraphQL 400 errors
252+
// This is a defensive check in case the early return in withRoutes doesn't catch it
253+
if (pathname === '/404' || pathname === '/404/' || pathname.startsWith('/404?')) {
254+
return {
255+
route: undefined,
256+
status: undefined,
257+
};
258+
}
259+
251260
let [routeCache, statusCache] = await kv.mget<RouteCache | StorefrontStatusCache>(
252261
kvKey(pathname, channelId),
253262
kvKey(STORE_STATUS_KEY, channelId),
@@ -290,6 +299,27 @@ export const withRoutes: MiddlewareFactory = () => {
290299
return async (request, event) => {
291300
const locale = request.headers.get('x-bc-locale') ?? '';
292301

302+
const { pathname } = new URL(request.url);
303+
const cleanPathName = clearLocaleFromPath(pathname, locale);
304+
305+
// Skip BigCommerce route resolution for /404 path to avoid GraphQL 400 errors on Vercel
306+
// This path should be handled by Next.js's not-found page via the catch-all route
307+
// Check both the clean path and the original pathname to catch all variations
308+
if (
309+
cleanPathName === '/404' ||
310+
cleanPathName === '/404/' ||
311+
pathname === '/404' ||
312+
pathname === '/404/' ||
313+
pathname.endsWith('/404') ||
314+
pathname.endsWith('/404/')
315+
) {
316+
const url404 = new URL(`/${locale}/404`, request.url);
317+
318+
url404.search = request.nextUrl.search;
319+
320+
return NextResponse.rewrite(url404);
321+
}
322+
293323
const { route, status } = await getRouteInfo(request, event);
294324

295325
if (status === 'MAINTENANCE') {
@@ -403,10 +433,6 @@ export const withRoutes: MiddlewareFactory = () => {
403433
}
404434

405435
default: {
406-
const { pathname } = new URL(request.url);
407-
408-
const cleanPathName = clearLocaleFromPath(pathname, locale);
409-
410436
url = `/${locale}${cleanPathName}`;
411437
}
412438
}

0 commit comments

Comments
 (0)