Skip to content
Open
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
3 changes: 1 addition & 2 deletions packages/nextjs/src/proxy/locale-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ export class LocaleProxy extends ProxyBase {
proxiesContext?: ProxiesContext
): Promise<NextResponse> => {
try {
const { pathname } = req.nextUrl;

const pathname = this.getPathname(req, res);
const localeFromPath = this.getLocaleFromPath(pathname);
const locale = localeFromPath || this.getLanguage(req, res);

Expand Down
4 changes: 2 additions & 2 deletions packages/nextjs/src/proxy/multisite-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { NextResponse, NextRequest } from 'next/server';
import { getSiteRewrite, SITE_KEY } from '@sitecore-content-sdk/content/site';
import { ProxyBase, ProxyBaseConfig, REWRITE_HEADER_NAME } from './proxy';
import { ProxyBase, ProxyBaseConfig } from './proxy';
import { SitecoreConfig } from '../config';
import { PREVIEW_KEY } from '@sitecore-content-sdk/content/editing';

Expand Down Expand Up @@ -67,7 +67,7 @@ export class MultisiteProxy extends ProxyBase {
): Promise<NextResponse> => {
try {
// Path can be rewritten by previously executed proxy
const pathname = res?.headers.get(REWRITE_HEADER_NAME) || req.nextUrl.pathname;
const pathname = this.getPathname(req, res);
const language = this.getLanguage(req, res);
const hostname = this.getHostHeader(req) || this.defaultHostname;
const startTimestamp = Date.now();
Expand Down
49 changes: 44 additions & 5 deletions packages/nextjs/src/proxy/proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,47 @@ describe('ProxyBase', () => {
});
});

describe('getPathname', () => {
it('should return pathname from nextUrl', () => {
const proxy = new SampleProxy({ sites: [] });
const req = createReq({
nextUrl: {
pathname: '/test/path',
},
});

expect(proxy['getPathname'](req)).to.equal('/test/path');
});

it('should return rewrite header when present', () => {
const proxy = new SampleProxy({ sites: [] });
const req = createReq();
const res = createRes({
headers: {
[REWRITE_HEADER_NAME]: '/rewritten/path',
},
});

expect(proxy['getPathname'](req, res)).to.equal('/rewritten/path');
});

it('should prefer rewrite header over nextUrl pathname', () => {
const proxy = new SampleProxy({ sites: [] });
const req = createReq({
nextUrl: {
pathname: '/test/path',
},
});
const res = createRes({
headers: {
[REWRITE_HEADER_NAME]: '/rewritten/path',
},
});

expect(proxy['getPathname'](req, res)).to.equal('/rewritten/path');
});
});

describe('getLanguage', () => {
it('should return defined language', () => {
const proxy = new SampleProxy({ sites: [] });
Expand Down Expand Up @@ -845,11 +886,9 @@ describe('defineProxy', () => {
},
} as unknown as NextRequest;

const result = await defineProxy(
redirectsProxy,
personalizeProxy,
languageRedirectProxy
).exec(req);
const result = await defineProxy(redirectsProxy, personalizeProxy, languageRedirectProxy).exec(
req
);

expect(redirectsProxy.handle).to.have.been.calledOnce;
expect(personalizeProxy.handle).to.not.have.been.called;
Expand Down
12 changes: 12 additions & 0 deletions packages/nextjs/src/proxy/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,18 @@ export abstract class ProxyBase extends ProxyHandler {
);
}

/**
* Get requested pathname.
* - If response is passed and REWRITE_HEADER_NAME header is set, use it as the pathname
* - If response isn't passed or REWRITE_HEADER_NAME header is not set, use the pathname from the request URL
* @param {NextRequest} req request
* @param {NextResponse} [res] response (optional)
* @returns {string} requested pathname
*/
protected getPathname(req: NextRequest, res?: NextResponse): string {
return res?.headers.get(REWRITE_HEADER_NAME) ?? req.nextUrl.pathname;
}

/**
* Get site information. If site name is stored in cookie, use it, otherwise resolve by hostname
* - If site can't be resolved by site name cookie use default site info based on provided parameters
Expand Down