Skip to content

Commit 363f880

Browse files
committed
feat: adapt the middleware example for cloudflare
1 parent 95fd3a8 commit 363f880

File tree

9 files changed

+90
-157
lines changed

9 files changed

+90
-157
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function MiddlewarePage() {
2+
return <h1>Via middleware</h1>;
3+
}

examples/middleware/app/page.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ export default function Home() {
1313
<p>
1414
<Link href="/about2">Go to about 2 page (no redirect or rewrite)</Link>
1515
</p>
16+
<p>
17+
<Link href="/middleware">Go to middleware page (using NextResponse.next())</Link>
18+
</p>
1619
</div>
1720
);
1821
}

examples/middleware/middleware.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { NextRequest, NextResponse } from "next/server";
22

33
export function middleware(request: NextRequest) {
4+
console.log("middleware");
45
if (request.nextUrl.pathname === "/about") {
56
return NextResponse.redirect(new URL("/redirected", request.url));
67
}
@@ -11,5 +12,5 @@ export function middleware(request: NextRequest) {
1112
}
1213

1314
export const config = {
14-
matcher: ["/about/:path*", "/another/:path*"],
15+
matcher: ["/about/:path*", "/another/:path*", "/middleware/:path*"],
1516
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/** @type {import('next').NextConfig} */
2+
const nextConfig = {};
3+
4+
export default nextConfig;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { OpenNextConfig } from "@opennextjs/aws/types/open-next";
2+
3+
const config: OpenNextConfig = {
4+
default: {},
5+
6+
middleware: {
7+
external: true,
8+
override: {
9+
wrapper: "cloudflare",
10+
converter: "edge",
11+
},
12+
},
13+
};
14+
15+
export default config;

examples/middleware/package.json

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
{
2+
"name": "middleware",
23
"private": true,
34
"scripts": {
45
"dev": "next dev",
56
"build": "next build",
67
"start": "next start",
7-
"lint": "next lint"
8+
"lint": "next lint",
9+
"build:worker": "pnpm cloudflare",
10+
"dev:worker": "wrangler dev --port 8770 --inspector-port 9330",
11+
"preview:worker": "pnpm build:worker && pnpm dev:worker"
812
},
913
"dependencies": {
10-
"next": "latest",
11-
"react": "18.2.0",
12-
"react-dom": "18.2.0"
14+
"next": "catalog:",
15+
"react": "catalog:",
16+
"react-dom": "catalog:"
1317
},
1418
"devDependencies": {
19+
"@opennextjs/cloudflare": "workspace:*",
1520
"@types/node": "18.0.0",
16-
"@types/react": "18.2.8",
17-
"@types/react-dom": "18.0.5",
18-
"eslint": "8.18.0",
19-
"eslint-config-next": "12.2.0",
20-
"typescript": "4.7.4"
21+
"@types/react": "catalog:",
22+
"@types/react-dom": "catalog:",
23+
"eslint": "catalog:",
24+
"typescript": "catalog:",
25+
"wrangler": "catalog:"
2126
}
2227
}

examples/middleware/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@
2121
]
2222
},
2323
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
24-
"exclude": ["node_modules"]
24+
"exclude": ["node_modules", "open-next.config.ts"]
2525
}

examples/middleware/wrangler.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ compatibility_date = "2024-09-23"
66
compatibility_flags = ["nodejs_compat"]
77

88
# Use the new Workers + Assets to host the static frontend files
9-
assets = { directory = ".open-next/assets", binding = "ASSETS" }
9+
assets = { directory = ".open-next/assets", binding = "ASSETS" }

0 commit comments

Comments
 (0)