Skip to content

Commit

Permalink
[sitecore-jss-nextjs]: removing header x-middleware-rewrite for redir…
Browse files Browse the repository at this point in the history
…ects #SXA-6564 (#1915)
  • Loading branch information
sc-ruslanmatkovskyi authored and illiakovalenko committed Sep 5, 2024
1 parent 51ab57f commit 3455ca1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Our versioning strategy is as follows:
- Minor: may include breaking changes in framework packages (e.g. framework upgrades, new features, improvements)
- Major: may include breaking changes in core packages (e.g. major architectural changes, major features)

## 22.1.3

### 🐛 Bug Fixes

* `[sitecore-jss-nextjs]` Addressed an issue where the x-middleware-rewrite header caused redirects to fail during execution on Netlify. ([#1915](https://github.com/Sitecore/jss/pull/1915))

## 22.1.2

### 🐛 Bug Fixes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
/* eslint-disable no-unused-expressions */
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable dot-notation */
import chai, { use } from 'chai';
import chaiString from 'chai-string';
import sinonChai from 'sinon-chai';
import sinon, { spy } from 'sinon';
import { NextRequest, NextResponse } from 'next/server';
import { debug } from '@sitecore-jss/sitecore-jss';
import { debug, GraphQLRequestClient } from '@sitecore-jss/sitecore-jss';
import {
REDIRECT_TYPE_301,
REDIRECT_TYPE_302,
REDIRECT_TYPE_SERVER_TRANSFER,
SiteResolver,
} from '@sitecore-jss/sitecore-jss/site';
import chai, { use } from 'chai';
import chaiString from 'chai-string';
import { NextRequest, NextResponse } from 'next/server';
import sinon, { spy } from 'sinon';
import sinonChai from 'sinon-chai';
import { RedirectsMiddleware } from './redirects-middleware';
import { GraphQLRequestClient } from '@sitecore-jss/sitecore-jss';

use(sinonChai);
const expect = chai.use(chaiString).expect;
Expand Down Expand Up @@ -1430,10 +1429,11 @@ describe('RedirectsMiddleware', () => {
});
});

it('should remove x-middleware-next header and redirect 301', async () => {
it('should remove x-middleware-next/x-middleware-rewrite headers and redirect 301', async () => {
const siteName = 'foo';
const res = NextResponse.redirect('http://localhost:3000/found', {});
res.headers.set('x-middleware-next', '1');
res.headers.set('x-middleware-rewrite', '1');
res.cookies.set('sc_site', siteName);
const req = createRequest({
nextUrl: {
Expand Down Expand Up @@ -1480,6 +1480,10 @@ describe('RedirectsMiddleware', () => {
url: '',
});

// Check that the headers were not removed
expect(finalRes.headers.has('x-middleware-next')).to.equal(false);
expect(finalRes.headers.has('x-middleware-rewrite')).to.equal(false);

expect(siteResolver.getByHost).not.called.to.equal(true);
expect(siteResolver.getByName).to.be.calledWith(siteName);
expect(fetchRedirects).to.be.calledWith(siteName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ export class RedirectsMiddleware extends MiddlewareBase {
});
if (res?.headers) {
redirect.headers.delete('x-middleware-next');
redirect.headers.delete('x-middleware-rewrite');
}
return redirect;
}
Expand Down

0 comments on commit 3455ca1

Please sign in to comment.