Skip to content

Commit

Permalink
#530555: [SXA][Headless] Redirects with defined language in source UR…
Browse files Browse the repository at this point in the history
…L don't work (#1023)

* #530555: [SXA][Headless] Redirects with defined language in source URL don't work

* #530555: Avoid importing NextURL

* #530555: Move locales property inside RedirectsMiddleware instead of RedirectsPlugin

* #530555: Move locales property inside RedirectsMiddlewareConfig
  • Loading branch information
AntonKechashin authored May 19, 2022
1 parent acc3458 commit 2d7899a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from 'next/server';
import { RedirectsMiddleware } from '@sitecore-jss/sitecore-jss-nextjs/edge';
import config from 'temp/config';
import { MiddlewarePlugin } from '..';
import nextConfig from '../../../../next.config';

class RedirectsPlugin implements MiddlewarePlugin {
private redirectsMiddleware: RedirectsMiddleware;
Expand All @@ -12,6 +13,7 @@ class RedirectsPlugin implements MiddlewarePlugin {
endpoint: config.graphQLEndpoint,
apiKey: config.sitecoreApiKey,
siteName: config.jssAppName,
locales: nextConfig().i18n.locales,
});
}

Expand Down
28 changes: 19 additions & 9 deletions packages/sitecore-jss-nextjs/src/edge/redirects-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ import {
/**
* extended RedirectsMiddlewareConfig config type for RedirectsMiddleware
*/
export type RedirectsMiddlewareConfig = Omit<GraphQLRedirectsServiceConfig, 'fetch'>;

export type RedirectsMiddlewareConfig = Omit<GraphQLRedirectsServiceConfig, 'fetch'> & {
locales: string[];
};
/**
* Middleware / handler fetches all redirects from Sitecore instance by grapqhl service
* compares with current url and redirects to target url
*/
export class RedirectsMiddleware {
private redirectsService: GraphQLRedirectsService;
private locales: string[];

/**
* NOTE: we provide native fetch for compatibility on Next.js Edge Runtime
Expand All @@ -28,6 +30,7 @@ export class RedirectsMiddleware {
*/
constructor(config: RedirectsMiddlewareConfig) {
this.redirectsService = new GraphQLRedirectsService({ ...config, fetch: fetch });
this.locales = config.locales;
}

/**
Expand All @@ -39,17 +42,22 @@ export class RedirectsMiddleware {
}

private handler = async (req: NextRequest): Promise<NextResponse> => {
const url = req.nextUrl.clone();
// Find the redirect from result of RedirectService

const existsRedirect = await this.getExistsRedirect(url);
const existsRedirect = await this.getExistsRedirect(req);

if (!existsRedirect) {
return NextResponse.next();
}

const url = req.nextUrl.clone();
url.search = existsRedirect.isQueryStringPreserved ? url.search : '';
url.pathname = existsRedirect.target;
const urlFirstPart = existsRedirect.target.split('/')[1];
if (this.locales.includes(urlFirstPart)) {
url.locale = urlFirstPart;
url.pathname = existsRedirect.target.replace(`/${urlFirstPart}`, '');
} else {
url.pathname = existsRedirect.target;
}
const redirectUrl = decodeURIComponent(url.href);

/** return Response redirect with http code of redirect type **/
Expand All @@ -71,11 +79,13 @@ export class RedirectsMiddleware {
* @returns Promise<RedirectInfo>
* @private
*/
private async getExistsRedirect(url: URL): Promise<RedirectInfo | undefined> {
private async getExistsRedirect(req: NextRequest): Promise<RedirectInfo | undefined> {
const redirects = await this.redirectsService.fetchRedirects();

return redirects.find((redirect: RedirectInfo) =>
regexParser(redirect.pattern).test(url.pathname)
return redirects.find(
(redirect: RedirectInfo) =>
regexParser(redirect.pattern).test(req.nextUrl.pathname) ||
regexParser(redirect.pattern).test(`/${req.nextUrl.locale}${req.nextUrl.pathname}`)
);
}
}

0 comments on commit 2d7899a

Please sign in to comment.