Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): fix redirects causing middleware to break #980

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.rewrite(new URL(`/${locale}${redirectUrl.pathname}`, request.url));
}
Copy link
Contributor

@BC-krasnoshapka BC-krasnoshapka Jun 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chanceaclark what's the case for this rewrite? I've just noticed that it handles prefix based locales strategy domain.com/en only. i18n supports subdomain based en.domain.com also. Should it be handled here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add more info once I move this PR from Draft -> Open, but [email protected]+ changed redirection behavior inside server actions causing things like login to break. I am not finished with this PR and this may not even be the fix for this issue, but if it is I still have other edge cases like external and absolute/relative redirects to test.


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
Loading