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-nextjs]: removing header x-middleware-rewrite for redirects #SXA-6564 #1915

Merged
merged 3 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ Our versioning strategy is as follows:

### 🧹 Chores

## 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');
sc-ruslanmatkovskyi marked this conversation as resolved.
Show resolved Hide resolved
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