Skip to content

Commit

Permalink
feat: switch to middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
plutoless committed Sep 4, 2024
1 parent 674daab commit dde0ff1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ WORKDIR /app

# COPY .env.example .env
COPY . .

RUN npm i && \
RUN npm config set registry https://registry.npmmirror.com
RUN npm i --verbose && \
npm run build


Expand Down
14 changes: 0 additions & 14 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,6 @@ const nextConfig = {
// output: 'test',
output: "standalone",
reactStrictMode: false,
async rewrites() {
const { TEN_DEV_SERVER_URL } = process.env;

// Check if environment variables are available
if (!TEN_DEV_SERVER_URL) {
throw "Environment variables TEN_DEV_SERVER_URL are not available";
}
return [
{
source: '/api/dev-server/v1/:path*',
destination: `${TEN_DEV_SERVER_URL}/api/dev-server/v1/:path*`,
},
];
},
}

export default nextConfig
2 changes: 1 addition & 1 deletion src/common/constant.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const PREFIX = "/api/dev-server/v1"
const PREFIX = "/apix/dev-server/v1"

export const USE_MOCK = process.env.NEXT_PUBLIC_USE_MOCK === "true"
export const DEFAULT_APP = "localhost"
Expand Down
22 changes: 22 additions & 0 deletions src/middleware.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// middleware.js
import { NextRequest, NextResponse } from 'next/server';


const { TEN_DEV_SERVER_URL } = process.env;

// Check if environment variables are available
if (!TEN_DEV_SERVER_URL) {
throw "Environment variables TEN_DEV_SERVER_URL are not available";
}

export function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;

if (pathname.startsWith('/apix/dev-server/v1')) {
const url = req.nextUrl.clone();
url.href = `${TEN_DEV_SERVER_URL}${pathname.replace('/apix/dev-server/v1', '/api/dev-server/v1')}`;

// console.log(`Rewriting request to ${url.href}`);
return NextResponse.rewrite(url);
}
}

0 comments on commit dde0ff1

Please sign in to comment.