Skip to content

Commit

Permalink
#603676: [sitecore-jss-nextjs] fixed redirect middleware logic (#1676)
Browse files Browse the repository at this point in the history
* #603676: [sitecore-jss-nextjs] fixed redirect middleware logic for match target url when uses param of trailing slash is true in next.config

* #603676: changed CHANGELOG

* #603676: added unit test

---------

Co-authored-by: Ruslan Matkovskyi <[email protected]>
  • Loading branch information
sc-ruslanmatkovskyi and Ruslan Matkovskyi authored Nov 27, 2023
1 parent 54c5c61 commit be6bf8b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ Our versioning strategy is as follows:

### 🐛 Bug Fixes

* `[templates/node-headless-ssr-proxy]` [node-headless-ssr-proxy] Add sc_site qs parameter to Layout Service requests by default ([#1660](https://github.com/Sitecore/jss/pull/1660))
* `[templates/node-headless-ssr-proxy]` `[node-headless-ssr-proxy]` Add sc_site qs parameter to Layout Service requests by default ([#1660](https://github.com/Sitecore/jss/pull/1660))
* `[sitecore-jss-nextjs]` Fix redirect middleware to match pattern when uses param trailingSlash in next.config.js ([#1676](https://github.com/Sitecore/jss/pull/1676))

## 21.6.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,65 @@ describe('RedirectsMiddleware', () => {

nextRedirectStub.restore();
});

it('should redirect, when next.config uses params trailingSlash is true', async () => {
const setCookies = () => {};
const res = createResponse({
url: 'http://localhost:3000/found/',
status: 301,
setCookies,
});
const nextRedirectStub = sinon.stub(NextResponse, 'redirect').callsFake((url, init) => {
const status = typeof init === 'number' ? init : init?.status || 307;
return ({
url,
status,
cookies: { set: setCookies },
headers: res.headers,
} as unknown) as NextResponse;
});
const req = createRequest({
nextUrl: {
pathname: '/not-found/',
href: 'http://localhost:3000/not-found/',
locale: 'en',
clone() {
return Object.assign({}, req.nextUrl);
},
},
});

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

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

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

validateEndMessageDebugLog('redirects middleware end in %dms: %o', {
headers: {},
redirected: undefined,
status: 301,
url: 'http://localhost:3000/found/',
});

expect(siteResolver.getByHost).to.be.calledWith(hostname);
// eslint-disable-next-line no-unused-expressions
expect(fetchRedirects.called).to.be.true;
expect(finalRes).to.deep.equal(res);
expect(finalRes.status).to.equal(res.status);

nextRedirectStub.restore();
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export class RedirectsMiddleware extends MiddlewareBase {
.replace(/^\/|\/$/g, '')
.replace(/^\^\/|\/\$$/g, '')
.replace(/^\^|\$$/g, '')
.replace(/\$\/gi$/g, '')}$/gi`;
.replace(/\$\/gi$/g, '')}[\/]?$/gi`;

return (
(regexParser(redirect.pattern).test(tragetURL) ||
Expand Down

0 comments on commit be6bf8b

Please sign in to comment.