Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sitecore-jss-proxy] The rewriteRequestPath function ignores query string parameters added in a middleware #1373

Merged
merged 4 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions packages/sitecore-jss-proxy/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ describe('rewriteRequestPath', () => {
headers: {
'accept-encoding': 'gzip or whatever',
},
query: {},
} as unknown) as Request;

const actual = rewriteRequestPath(url, mockRequest, config);
Expand All @@ -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',
},
Expand Down Expand Up @@ -164,6 +183,7 @@ describe('rewriteRequestPath', () => {
headers: {
'accept-encoding': 'gzip or whatever',
},
query: {},
} as unknown) as Request;

const actual = rewriteRequestPath(url, req, qsParamsConfig);
Expand Down
5 changes: 3 additions & 2 deletions packages/sitecore-jss-proxy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,10 @@ export function rewriteRequestPath(
let finalReqPath = decodedReqPath;
illiakovalenko marked this conversation as resolved.
Show resolved Hide resolved
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 (config.qsParams) {
Expand Down
1 change: 1 addition & 0 deletions packages/sitecore-jss-proxy/src/test/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { ProxyConfig } from '../ProxyConfig';

const config: ProxyConfig = {
Expand Down