Skip to content

Commit

Permalink
EES-5464 replace next config rewrite with middleware solution
Browse files Browse the repository at this point in the history
  • Loading branch information
bennettstuart committed Sep 20, 2024
1 parent 23712c8 commit f7ce64e
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 146 deletions.
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions src/explore-education-statistics-frontend/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ const nextConfig = {
},
];
},
async rewrites() {
return [
{
source: '/data-catalogue/data-set/:dataSetFileId/csv',
destination: `${process.env.CONTENT_API_BASE_URL}/data-set-files/:dataSetFileId/download`,
},
];
},
webpack(config, options) {
const { dev, isServer } = options;

Expand Down
1 change: 1 addition & 0 deletions src/explore-education-statistics-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"memoizee": "^0.4.14",
"next": "^14.2.2",
"next-sitemap": "^4.2.3",
"node-match-path": "^0.6.3",
"nookies": "^2.5.2",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
8 changes: 4 additions & 4 deletions src/explore-education-statistics-frontend/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import redirectPages from '@frontend/middleware/pages/redirectPages';
import type { NextRequest } from 'next/server';
import chain from './middleware/chain';
import rewriteDataCatalogueDownload from './middleware/pages/rewriteDataCatalogue';

export default async function middleware(request: NextRequest) {
return redirectPages(request);
}
export default chain([rewriteDataCatalogueDownload, redirectPages]);

// Only run the middleware on the specified paths below.
// Ideally we'd just exclude build files and run it on all routes,
Expand All @@ -16,6 +15,7 @@ export const config = {
'/find-statistics/:path*/:path*',
'/data-tables',
'/data-catalogue',
'/data-catalogue/data-set/:dataSetFileId/csv',
'/methodology/:path*',
'/subscriptions',
],
Expand Down
16 changes: 16 additions & 0 deletions src/explore-education-statistics-frontend/src/middleware/chain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { NextMiddleware, NextResponse } from 'next/server';

export type MiddlewareFactory = (middleware: NextMiddleware) => NextMiddleware;

export default function chain(
functions: MiddlewareFactory[],
index = 0,
): NextMiddleware {
const current = functions[index];
if (current) {
const next = chain(functions, index + 1);
return current(next);
}

return () => NextResponse.next();
}
Loading

0 comments on commit f7ce64e

Please sign in to comment.