Skip to content

Commit

Permalink
[sitecore-jss-nextjs] Fix Chromes mode for NativeFetcher (#2018)
Browse files Browse the repository at this point in the history
  • Loading branch information
art-alexeyenko authored Jan 24, 2025
1 parent 90e6a50 commit e262abe
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ Our versioning strategy is as follows:

## Unreleased

### 🐛 Bug Fixes

* `[sitecore-jss-nextj]` Fix Chromes editing mode when rendering host URL is internally redirected in XMCloud


## 22.4.0

### 🐛 Bug Fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,29 @@ describe('EditingRenderMiddleware', () => {
expect(fetcher.get).to.have.been.calledWithMatch('https://vercel.com');
});

it('resolveServerUrl should return https address when authorization header present', async () => {
const html = '<html><body>Something amazing</body></html>';
const fetcher = mockFetcher(html);
const dataService = mockDataService();
const query = {} as Query;
query[QUERY_PARAM_EDITING_SECRET] = secret;
const req = mockRequest(EE_BODY, query, undefined, {
authorization: '123',
host: 'testhostheader.com',
});
const res = mockResponse();

const middleware = new EditingRenderMiddleware({
dataFetcher: fetcher,
editingDataService: dataService,
});
const handler = middleware.getHandler();

await handler(req, res);

expect(fetcher.get).to.have.been.calledWithMatch('https://testhostheader.com');
});

it('should use custom resolveServerUrl', async () => {
const html = '<html><body>Something amazing</body></html>';
const fetcher = mockFetcher(html);
Expand Down Expand Up @@ -731,7 +754,7 @@ describe('EditingRenderMiddleware', () => {

expect(fetcher.get).to.have.been.calledOnce;
expect(fetcher.get).to.have.been.calledWith(
match('http://localhost:3000/test/path?timestamp'),
match('https://localhost:3000/test/path?timestamp'),
{
headers: {
authorization: mockAuthValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ export class ChromesHandler extends RenderMiddlewareBase {
* @param {NextApiRequest} req
*/
private defaultResolveServerUrl = (req: NextApiRequest) => {
return `${process.env.VERCEL ? 'https' : 'http'}://${req.headers.host}`;
// use https for requests with auth but also support unsecured http rendering hosts
return `${req.headers.authorization || process.env.VERCEL ? 'https' : 'http'}://${
req.headers.host
}`;
};

private extractEditingData(req: NextApiRequest): EditingData {
Expand Down

0 comments on commit e262abe

Please sign in to comment.