Skip to content

Commit df136b1

Browse files
authored
chore(app-pages-router): Put proxy in front of the server functions (opennextjs#968)
* chore(app-pages-router): Put proxy in front of the server functions * add port constant * mv const * env var for port
1 parent 580fad1 commit df136b1

File tree

5 files changed

+154
-2
lines changed

5 files changed

+154
-2
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type {
2+
OpenNextConfig,
3+
OverrideOptions,
4+
} from "@opennextjs/aws/types/open-next.js";
5+
6+
const devOverride = {
7+
wrapper: "express-dev",
8+
converter: "node",
9+
incrementalCache: "fs-dev",
10+
queue: "direct",
11+
tagCache: "fs-dev-nextMode",
12+
} satisfies OverrideOptions;
13+
14+
export default {
15+
default: {
16+
override: devOverride,
17+
},
18+
functions: {
19+
api: {
20+
override: devOverride,
21+
routes: ["app/api/client/route", "app/api/host/route", "pages/api/hello"],
22+
patterns: ["/api/*"],
23+
},
24+
},
25+
imageOptimization: {
26+
override: {
27+
wrapper: "dummy",
28+
converter: "dummy",
29+
},
30+
loader: "fs-dev",
31+
},
32+
// You can override the build command here so that you don't have to rebuild next every time you make a change
33+
//buildCommand: "echo 'No build command'",
34+
} satisfies OpenNextConfig;

examples/app-pages-router/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"private": true,
55
"scripts": {
66
"openbuild": "node ../../packages/open-next/dist/index.js build --build-command \"npx turbo build\"",
7+
"openbuild:local": "node ../../packages/open-next/dist/index.js build --config-path open-next.config.local.ts",
8+
"openbuild:local:start": "tsx proxy.ts",
79
"dev": "next dev --turbopack --port 3003",
810
"build": "next build",
911
"start": "next start --port 3003",
@@ -12,18 +14,21 @@
1214
},
1315
"dependencies": {
1416
"@example/shared": "workspace:*",
15-
"next": "catalog:",
1617
"@opennextjs/aws": "workspace:*",
18+
"express-http-proxy": "2.1.1",
19+
"next": "catalog:",
1720
"react": "catalog:",
1821
"react-dom": "catalog:"
1922
},
2023
"devDependencies": {
24+
"@types/express-http-proxy": "1.6.7",
2125
"@types/node": "catalog:",
2226
"@types/react": "catalog:",
2327
"@types/react-dom": "catalog:",
2428
"autoprefixer": "catalog:",
2529
"postcss": "catalog:",
2630
"tailwindcss": "catalog:",
31+
"tsx": "4.20.5",
2732
"typescript": "catalog:"
2833
}
2934
}

examples/app-pages-router/proxy.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { spawn } from "node:child_process";
2+
3+
import express from "express";
4+
import proxy from "express-http-proxy";
5+
6+
const PORT = process.env.PORT ?? 3000;
7+
8+
// Start servers
9+
spawn("node", [".open-next/server-functions/default/index.mjs"], {
10+
env: { ...process.env, PORT: "3010" },
11+
stdio: "inherit",
12+
});
13+
14+
spawn("node", [".open-next/server-functions/api/index.mjs"], {
15+
env: { ...process.env, PORT: "3011" },
16+
stdio: "inherit",
17+
});
18+
19+
const app = express();
20+
21+
app.use(
22+
"/api/*",
23+
proxy("http://localhost:3011", {
24+
proxyReqPathResolver: (req) => req.originalUrl,
25+
proxyReqOptDecorator: (proxyReqOpts) => {
26+
proxyReqOpts.headers.host = `localhost:${PORT}`;
27+
return proxyReqOpts;
28+
},
29+
}),
30+
);
31+
32+
// Catch-all for everything else
33+
app.use(
34+
"*",
35+
proxy("http://localhost:3010", {
36+
proxyReqPathResolver: (req) => req.originalUrl,
37+
proxyReqOptDecorator: (proxyReqOpts) => {
38+
// We need to ensure the host header is set correctly else you will run into this error in `/server-actions`
39+
// Error: Invalid Server Actions request:
40+
// `x-forwarded-host` header with value `localhost:3010` does not match `origin` header with value `localhost:3000` from a forwarded Server Actions request. Aborting the action.
41+
proxyReqOpts.headers.host = `localhost:${PORT}`;
42+
delete proxyReqOpts.headers["x-forwarded-host"];
43+
return proxyReqOpts;
44+
},
45+
}),
46+
);
47+
48+
app.listen(PORT, () => {
49+
console.log(`Proxy running at http://localhost:${PORT}`);
50+
});

examples/app-pages-router/tsconfig.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,10 @@
2424
}
2525
},
2626
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
27-
"exclude": ["node_modules"]
27+
"exclude": [
28+
"node_modules",
29+
"open-next.config.ts",
30+
"open-next.config.local.ts",
31+
"proxy.ts"
32+
]
2833
}

pnpm-lock.yaml

Lines changed: 58 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)