Skip to content

Commit

Permalink
Proxy testing: Add a proxying option in next.js
Browse files Browse the repository at this point in the history
Note: unfortunately, you can't use: return NextResponse.rewrite(...)
here, otherwise you get an error like:

    [Next] - error Error: API route returned a Response object in the Node.js runtime, this is not supported. Please use
    `runtime: "edge"` instead: https://nextjs.org/docs/api-routes/edge-api-routes

Which is precisely what we *don't* want.
  • Loading branch information
danielbeardsley committed Sep 5, 2023
1 parent 8eaf8b3 commit 7ec1c33
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
4 changes: 2 additions & 2 deletions frontend/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ const moduleExports = {
destination: `https://proxy-us-east.ifixit.com/health-check/proxy-us-east/:path*`,
},
{
source: '/:path*',
destination: `https://proxy.ifixit.com/health-check/fallback/:path*`,
source: '/proxy-nextjs-test/:path*',
destination: `/api/php-app/health-check/proxy-nextjs-test/:path*`,
},
],
};
Expand Down
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"dayjs": "1.10.5",
"framer-motion": "10.12.0",
"graphql": "15.5.3",
"http-proxy": "^1.18.1",
"ioredis": "5.2.4",
"lite-youtube-embed": "^0.2.0",
"lodash": "4.17.21",
Expand Down
18 changes: 18 additions & 0 deletions frontend/pages/api/php-app/[[...path]].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { NextApiHandler, NextApiRequest, NextApiResponse } from 'next';
import { NextResponse } from 'next/server';

Check failure on line 2 in frontend/pages/api/php-app/[[...path]].ts

View workflow job for this annotation

GitHub Actions / linter

'NextResponse' is defined but never used. Allowed unused vars must match /^_/u
import httpProxy from 'http-proxy';

const proxy = httpProxy.createProxyServer({
secure: true,
changeOrigin: true,
});

const handler: NextApiHandler = async (
req: NextApiRequest,
res: NextApiResponse
) => {
const dest = `https://proxy-us-east.ifixit.com/health-check${req.url}`;
proxy.web(req, res, { target: dest });
};

export default handler;
20 changes: 18 additions & 2 deletions pnpm-lock.yaml

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

0 comments on commit 7ec1c33

Please sign in to comment.