diff --git a/packages/sitecore-jss-proxy/src/index.test.ts b/packages/sitecore-jss-proxy/src/index.test.ts index e92a233080..0b8d180e8a 100644 --- a/packages/sitecore-jss-proxy/src/index.test.ts +++ b/packages/sitecore-jss-proxy/src/index.test.ts @@ -63,6 +63,7 @@ describe('rewriteRequestPath', () => { headers: { 'accept-encoding': 'gzip or whatever', }, + query: {}, } as unknown) as Request; const actual = rewriteRequestPath(url, mockRequest, config); @@ -75,19 +76,37 @@ describe('rewriteRequestPath', () => { headers: { 'accept-encoding': 'gzip or whatever', }, + query: {}, } as unknown) as Request; const actual = rewriteRequestPath(url, mockRequest, config); expect(actual).to.equal(expected); }); + + it('should return route prefixed with layout service route and with request query appended', () => { + const url = '/about'; + const expected = '/sitecore/layoutsvc/render/jss?item=%2Fabout&sc_apikey={GUID}&foo=bar'; + + const req = ({ + query: { foo: 'bar' }, + headers: { + 'accept-encoding': 'gzip or whatever', + }, + } as unknown) as Request; + + const actual = rewriteRequestPath(url, req, config); + + expect(actual).to.equal(expected); + }); + describe('when url contains a querystring', () => { it('should return route prefixed with layout service route and with querystring appended', () => { const url = '/about?sc_camp=123456%2078'; const expected = - '/sitecore/layoutsvc/render/jss?item=%2Fabout&sc_apikey={GUID}&sc_camp=123456%2078'; + '/sitecore/layoutsvc/render/jss?item=%2Fabout&sc_apikey={GUID}&sc_camp=123456%2078&foo=bar'; const req = ({ - query: { sc_camp: '123456 78' }, + query: { sc_camp: '123456 78', foo: 'bar' }, headers: { 'accept-encoding': 'gzip or whatever', }, @@ -164,6 +183,7 @@ describe('rewriteRequestPath', () => { headers: { 'accept-encoding': 'gzip or whatever', }, + query: {}, } as unknown) as Request; const actual = rewriteRequestPath(url, req, qsParamsConfig); diff --git a/packages/sitecore-jss-proxy/src/index.ts b/packages/sitecore-jss-proxy/src/index.ts index 56eea86c63..af02f22dc4 100644 --- a/packages/sitecore-jss-proxy/src/index.ts +++ b/packages/sitecore-jss-proxy/src/index.ts @@ -384,9 +384,16 @@ export function rewriteRequestPath( let finalReqPath = decodedReqPath; const qsIndex = finalReqPath.indexOf('?'); let qs = ''; - if (qsIndex > -1) { + if (qsIndex > -1 || Object.keys(req.query).length) { qs = buildQueryString((req as ProxyIncomingMessage).query); - finalReqPath = finalReqPath.slice(0, qsIndex); + // Splice qs part when url contains that + if (qsIndex > -1) finalReqPath = finalReqPath.slice(0, qsIndex); + } + + // if the request URL contains a path/route that should not be re-written, then just pass it along as-is + if (isUrlIgnored(`${finalReqPath}?${qs}`, config)) { + // we do not return the decoded URL because we're using it verbatim - should be encoded. + return reqPath; } if (config.qsParams) { diff --git a/packages/sitecore-jss-proxy/src/test/config.test.ts b/packages/sitecore-jss-proxy/src/test/config.test.ts index 09c4856554..0e936f34c0 100644 --- a/packages/sitecore-jss-proxy/src/test/config.test.ts +++ b/packages/sitecore-jss-proxy/src/test/config.test.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-unused-vars */ import { ProxyConfig } from '../ProxyConfig'; const config: ProxyConfig = {