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: don't rewrite Next.js server actions' requests #1707

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
115 changes: 61 additions & 54 deletions core/middlewares/with-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,58 @@ const getRouteInfo = async (request: NextRequest, event: NextFetchEvent) => {
}
};

export const withRoutes: MiddlewareFactory = () => {
// See https://github.com/vercel/next.js/blob/main/packages/next/src/server/lib/server-action-request-meta.ts
const isServerAction = ({ method, headers }: NextRequest) =>
method === 'POST' &&
(headers.get('next-action') != null ||
headers.get('content-type') === 'application/x-www-form-urlencoded' ||
headers.get('content-type')?.startsWith('multipart/form-data'));

const getRewriteUrl = (
locale: string,
node: z.infer<typeof NodeSchema> | null | undefined,
request: NextRequest,
postfix: string,
) => {
Copy link
Author

@agurtovoy agurtovoy Dec 5, 2024

Choose a reason for hiding this comment

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

Factored out a standalone piece of logic from withRoutes below to keep the withRoutes complexity under the limit. No semantic changes. I kept RawHtmlPage handling in withRoutes as a special case that is separate from the rewrite logic.

diff

switch (node?.__typename) {
case 'Brand':
return `/${locale}/brand/${node.entityId}${postfix}`;

case 'Category':
return `/${locale}/category/${node.entityId}${postfix}`;

case 'Product':
return `/${locale}/product/${node.entityId}${postfix}`;

case 'NormalPage':
return `/${locale}/webpages/normal/${node.id}`;

case 'ContactPage':
return `/${locale}/webpages/contact/${node.id}`;

default: {
const { pathname } = new URL(request.url);

const cleanPathName = clearLocaleFromPath(pathname, locale);

if (cleanPathName === '/' && postfix) {
return `/${locale}${postfix}`;
}

return `/${locale}${cleanPathName}`;
}
}
};

export const withRoutes: MiddlewareFactory = (next) => {
return async (request, event) => {
// Behind the scenes, Next.js server actions are translated into `POST` requests
// that get handled by the framework; don't interfere with these requests
// https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#behavior
if (isServerAction(request)) {
return next(request, event);
}
Copy link
Author

Choose a reason for hiding this comment

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

The actual fix.


const locale = request.headers.get('x-bc-locale') ?? '';

const { route, status } = await getRouteInfo(request, event);
Expand Down Expand Up @@ -294,64 +344,21 @@ export const withRoutes: MiddlewareFactory = () => {
}
}

const customerAccessToken = await getSessionCustomerAccessToken();
let postfix = '';

if (!request.nextUrl.search && !customerAccessToken && request.method === 'GET') {
postfix = '/static';
}
Copy link
Author

Choose a reason for hiding this comment

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

Moved below.


const node = route?.node;
let url: string;

switch (node?.__typename) {
case 'Brand': {
url = `/${locale}/brand/${node.entityId}${postfix}`;
break;
}

case 'Category': {
url = `/${locale}/category/${node.entityId}${postfix}`;
break;
}
if (node?.__typename === 'RawHtmlPage') {
const { htmlBody } = await getRawWebPageContent(node.id);

case 'Product': {
url = `/${locale}/product/${node.entityId}${postfix}`;
break;
}

case 'NormalPage': {
url = `/${locale}/webpages/normal/${node.id}`;
break;
}

case 'ContactPage': {
url = `/${locale}/webpages/contact/${node.id}`;
break;
}

case 'RawHtmlPage': {
const { htmlBody } = await getRawWebPageContent(node.id);

return new NextResponse(htmlBody, {
headers: { 'content-type': 'text/html' },
});
}

default: {
const { pathname } = new URL(request.url);

const cleanPathName = clearLocaleFromPath(pathname, locale);

if (cleanPathName === '/' && postfix) {
url = `/${locale}${postfix}`;
break;
}

url = `/${locale}${cleanPathName}`;
}
return new NextResponse(htmlBody, {
headers: { 'content-type': 'text/html' },
});
}

const customerAccessToken = await getSessionCustomerAccessToken();
const postfix =
!request.nextUrl.search && !customerAccessToken && request.method === 'GET' ? '/static' : '';

const url = getRewriteUrl(locale, node, request, postfix);
const rewriteUrl = new URL(url, request.url);

rewriteUrl.search = request.nextUrl.search;
Expand Down
Loading