Skip to content

Commit

Permalink
[sitecore-jss-nextjs]: Unit test has been added and refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruslan Matkovskyi committed Aug 28, 2024
1 parent a14cab2 commit 80f98a3
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,62 @@ describe('RedirectsMiddleware', () => {
});
});

it('should remove x-middleware-next header and redirect 301', async () => {
const siteName = 'foo';
const res = NextResponse.redirect('http://localhost:3000/found', {});
res.headers.set('x-middleware-next', '1');
res.cookies.set('sc_site', siteName);
const req = createRequest({
nextUrl: {
href: 'http://localhost:3000/not-found',
pathname: '/not-found',
locale: 'en',
search: '',
origin: 'http://localhost:3000',
clone() {
return Object.assign({}, req.nextUrl);
},
},
});

const { middleware, fetchRedirects, siteResolver } = createMiddleware({
pattern: 'not-found',
target: '/found',
redirectType: REDIRECT_TYPE_301,
isQueryStringPreserved: true,
locale: 'en',
});

const expected = NextResponse.redirect('http://localhost:3000/found', {
...res,
status: 301,
headers: {},
});

const finalRes = await middleware.getHandler()(req, res);

validateDebugLog('redirects middleware start: %o', {
hostname: 'foo.net',
language: 'en',
pathname: '/not-found',
});

validateEndMessageDebugLog('redirects middleware end in %dms: %o', {
headers: {
location: 'http://localhost:3000/found',
'set-cookie': 'sc_site=foo; Path=/',
},
redirected: false,
status: 301,
url: '',
});

expect(siteResolver.getByHost).not.called.to.equal(true);
expect(siteResolver.getByName).to.be.calledWith(siteName);
expect(fetchRedirects).to.be.calledWith(siteName);
expect(finalRes.status).to.equal(expected.status);
});

describe('should redirect to normalized path when nextjs specific "path" query string parameter is provided', () => {
it('should return 301 redirect', async () => {
const setCookies = () => {};
Expand Down
60 changes: 32 additions & 28 deletions packages/sitecore-jss-nextjs/src/middleware/redirects-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,38 +143,16 @@ export class RedirectsMiddleware extends MiddlewareBase {
url.href = prepareNewURL.href;
}

const normalizedHeaders = new Headers();

/**
* FIX for Netlify service
* Netlify doesn’t handle 301/302 redirects if Next.js sets the header x-middleware-next.
*/
if (res?.headers) {
res.headers.forEach((value, key) => {
if (key.toLowerCase() !== 'x-middleware-next') {
normalizedHeaders.set(key, value);
}
});
}

const redirectUrl = decodeURIComponent(url.href);

/** return Response redirect with http code of redirect type **/
switch (existsRedirect.redirectType) {
case REDIRECT_TYPE_301:
return NextResponse.redirect(redirectUrl, {
...res,
status: 301,
statusText: 'Moved Permanently',
headers: normalizedHeaders,
});
case REDIRECT_TYPE_302:
return NextResponse.redirect(redirectUrl, {
...res,
status: 302,
statusText: 'Found',
headers: normalizedHeaders,
});
case REDIRECT_TYPE_301: {
return this.createRedirectResponse(redirectUrl, res, 301, 'Moved Permanently');
}
case REDIRECT_TYPE_302: {
return this.createRedirectResponse(redirectUrl, res, 302, 'Found');
}
case REDIRECT_TYPE_SERVER_TRANSFER: {
return this.rewrite(redirectUrl, req, res || NextResponse.next());
}
Expand Down Expand Up @@ -281,4 +259,30 @@ export class RedirectsMiddleware extends MiddlewareBase {

return new URL(`${url.pathname}`, url.origin);
}

/**
* Helper function to create a redirect response and remove the x-middleware-next header.
* @param {string} url The URL to redirect to.
* @param {Response} res The response object.
* @param {number} status The HTTP status code of the redirect.
* @param {string} statusText The status text of the redirect.
* @returns {NextResponse<unknown>} The redirect response.
*/
private createRedirectResponse(
url: string,
res: Response | undefined,
status: number,
statusText: string
): NextResponse {
const redirect = NextResponse.redirect(url, {
...(res || {}),
status,
statusText,
headers: res?.headers,
});
if (res?.headers) {
redirect.headers.delete('x-middleware-next');
}
return redirect;
}
}

0 comments on commit 80f98a3

Please sign in to comment.