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-nexjts] Redirects don't work when file extensions present in a route #1566

Merged
merged 3 commits into from
Jul 20, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Our versioning strategy is as follows:

### 🐛 Bug Fixes

* `[tempaltes/nextjs]` `[templates/nextjs-sxa]` `[sitecore-jss-nexjts]` Redirects don't work when file extensions present in a route ([#1566](https://github.com/Sitecore/jss/pull/1566))
* `[templates/vue]` "lint" command is failing due to bug introduced by eslint-plugin-prettier ([#1563](https://github.com/Sitecore/jss/pull/1563))
* `[templates/nextjs-sxa]` Fix font awesome - added CDN instead of using node_modules(problem with CORS) ([#1536](https://github.com/Sitecore/jss/pull/1536) ([#1545](https://github.com/Sitecore/jss/pull/1545))
* `[templates/nextjs-sxa]` Fix menu component of third-level menu. ([#1540](https://github.com/Sitecore/jss/pull/1540)) ([#1546](https://github.com/Sitecore/jss/pull/1546))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class RedirectsPlugin implements MiddlewarePlugin {
// These should match those in your next.config.js (i18n.locales).
locales: ['en'],
// This function determines if a route should be excluded from RedirectsMiddleware.
// Certain paths are ignored by default (e.g. files and Next.js API routes), but you may wish to exclude more.
// Certain paths are ignored by default (e.g. Next.js API routes), but you may wish to exclude more.
// This is an important performance consideration since Next.js Edge middleware runs on every request.
excludeRoute: () => false,
// This function determines if the middleware should be turned off.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const config = {
* 3. /sitecore/api (Sitecore API routes)
* 4. /- (Sitecore media)
* 5. /healthz (Health check)
* 6. all root files inside /public (e.g. /favicon.ico)
* 6. all root files inside /public
*/
matcher: ['/', '/((?!api/|_next/|healthz|sitecore/api/|-/|[\\w-]+\\.\\w+).*)'],
matcher: ['/', '/((?!api/|_next/|healthz|sitecore/api/|-/|favicon.ico|sc_logo.svg).*)'],
};
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ describe('MiddlewareBase', () => {
it('default', () => {
const middleware = new SampleMiddleware({ siteResolver: new MockSiteResolver([]) });

expect(middleware['excludeRoute']('/src/image.png')).to.equal(true);
expect(middleware['excludeRoute']('/api/layout/render')).to.equal(true);
expect(middleware['excludeRoute']('/sitecore/render')).to.equal(true);
expect(middleware['excludeRoute']('/_next/webpack')).to.equal(true);
Expand Down
1 change: 0 additions & 1 deletion packages/sitecore-jss-nextjs/src/middleware/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export abstract class MiddlewareBase {

protected excludeRoute(pathname: string) {
return (
pathname.includes('.') || // Ignore files
pathname.startsWith('/api/') || // Ignore Next.js API calls
pathname.startsWith('/sitecore/') || // Ignore Sitecore API calls
pathname.startsWith('/_next') || // Ignore next service calls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export class MultisiteMiddleware extends MiddlewareBase {
};
}

protected excludeRoute(pathname: string): boolean | undefined {
// ignore files
return pathname.includes('.') || super.excludeRoute(pathname);
}

private handler = async (req: NextRequest, res?: NextResponse): Promise<NextResponse> => {
const pathname = req.nextUrl.pathname;
const language = this.getLanguage(req);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ export class PersonalizeMiddleware extends MiddlewareBase {
};
}

protected excludeRoute(pathname: string): boolean | undefined {
// ignore files
return pathname.includes('.') || super.excludeRoute(pathname);
}

private handler = async (req: NextRequest, res?: NextResponse): Promise<NextResponse> => {
const pathname = req.nextUrl.pathname;
const language = this.getLanguage(req);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ describe('RedirectsMiddleware', () => {
it('default', async () => {
const { middleware } = createMiddleware();

await test('/src/image.png', middleware);
await test('/api/layout/render', middleware);
await test('/sitecore/render', middleware);
await test('/_next/webpack', middleware);
Expand All @@ -272,7 +271,6 @@ describe('RedirectsMiddleware', () => {
excludeRoute,
});

await test('/src/image.png', middleware);
await test('/api/layout/render', middleware);
await test('/sitecore/render', middleware);
await test('/_next/webpack', middleware);
Expand Down