From 130a3bd33b2af87ba2ba52137ccd073950b9504e Mon Sep 17 00:00:00 2001 From: Ruslan Matkovskyi Date: Fri, 24 Nov 2023 01:23:55 +0100 Subject: [PATCH 1/3] #603676: [sitecore-jss-nextjs] fixed redirect middleware logic for match target url when uses param of trailing slash is true in next.config --- .../sitecore-jss-nextjs/src/middleware/redirects-middleware.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sitecore-jss-nextjs/src/middleware/redirects-middleware.ts b/packages/sitecore-jss-nextjs/src/middleware/redirects-middleware.ts index de2eeaec1a..54924cb163 100644 --- a/packages/sitecore-jss-nextjs/src/middleware/redirects-middleware.ts +++ b/packages/sitecore-jss-nextjs/src/middleware/redirects-middleware.ts @@ -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) || From 2bac860f902fc7043a7bc981d73668153a728fd1 Mon Sep 17 00:00:00 2001 From: Ruslan Matkovskyi Date: Fri, 24 Nov 2023 01:29:43 +0100 Subject: [PATCH 2/3] #603676: changed CHANGELOG --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8dcf2eded5..0bd430f694 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From e2e76342c6090feaf0fff82245d3b649d4b7e713 Mon Sep 17 00:00:00 2001 From: Ruslan Matkovskyi Date: Mon, 27 Nov 2023 17:17:10 +0100 Subject: [PATCH 3/3] #603676: added unit test --- .../middleware/redirects-middleware.test.ts | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/packages/sitecore-jss-nextjs/src/middleware/redirects-middleware.test.ts b/packages/sitecore-jss-nextjs/src/middleware/redirects-middleware.test.ts index f4d7c1223f..f253547805 100644 --- a/packages/sitecore-jss-nextjs/src/middleware/redirects-middleware.test.ts +++ b/packages/sitecore-jss-nextjs/src/middleware/redirects-middleware.test.ts @@ -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(); + }); }); }); });