Skip to content

Commit

Permalink
fix(core): fix redirects causing middleware to break
Browse files Browse the repository at this point in the history
  • Loading branch information
chanceaclark committed Jun 6, 2024
1 parent bf4739d commit 941a536
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 105 deletions.
1 change: 1 addition & 0 deletions core/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const Credentials = z.object({
});

const config = {
trustHost: true,
session: {
strategy: 'jwt',
},
Expand Down
19 changes: 8 additions & 11 deletions core/middlewares/with-routes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { cookies } from 'next/headers';
import { NextFetchEvent, NextRequest, NextResponse } from 'next/server';
import createMiddleware from 'next-intl/middleware';
import { z } from 'zod';
Expand Down Expand Up @@ -155,8 +154,6 @@ const getRouteInfo = async (request: NextRequest, event: NextFetchEvent) => {

export const withRoutes: MiddlewareFactory = () => {
return async (request, event) => {
locale = cookies().get('NEXT_LOCALE')?.value || '';

const intlMiddleware = createMiddleware({
locales,
localePrefix,
Expand All @@ -165,14 +162,16 @@ export const withRoutes: MiddlewareFactory = () => {
});
const response = intlMiddleware(request);

// Early redirect to detected locale if needed
locale = response.cookies.get('NEXT_LOCALE')?.value || defaultLocale;

if (response.redirected) {
return response;
}

if (!locale) {
// Try to get locale detected by next-intl
locale = response.cookies.get('NEXT_LOCALE')?.value || '';
if (request.headers.has('x-action-redirect')) {
const redirectUrl = new URL(request.headers.get('x-action-redirect') ?? '', request.url);

return NextResponse.redirect(new URL(`/${locale}${redirectUrl.pathname}`, request.url));
}

const { route, status } = await getRouteInfo(request, event);
Expand All @@ -184,14 +183,12 @@ export const withRoutes: MiddlewareFactory = () => {

// Follow redirects if found on the route
// Use 301 status code as it is more universally supported by crawlers
const redirectConfig = { status: 301 };

if (route?.redirect) {
switch (route.redirect.to.__typename) {
case 'ManualRedirect': {
// For manual redirects, redirect to the full URL to handle cases
// where the destination URL might be external to the site
return NextResponse.redirect(route.redirect.toUrl, redirectConfig);
return NextResponse.redirect(route.redirect.toUrl, { status: 301 });
}

default: {
Expand All @@ -201,7 +198,7 @@ export const withRoutes: MiddlewareFactory = () => {
const redirectPathname = new URL(route.redirect.toUrl).pathname;
const redirectUrl = new URL(redirectPathname, request.url);

return NextResponse.redirect(redirectUrl, redirectConfig);
return NextResponse.redirect(redirectUrl, { status: 301 });
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
"lodash.debounce": "^4.0.8",
"lru-cache": "^10.2.2",
"lucide-react": "^0.383.0",
"next": "14.1.4",
"next-auth": "5.0.0-beta.17",
"next-intl": "^3.13.0",
"next": "14.2.3",
"next-auth": "5.0.0-beta.19",
"next-intl": "^3.14.1",
"react": "^18.3.1",
"react-day-picker": "^8.10.0",
"react-dom": "^18.3.1",
Expand Down
Loading

0 comments on commit 941a536

Please sign in to comment.