feat: use opennextjs to deploy on cloudflare workers

This commit is contained in:
jiang-zhexin 2025-10-16 22:22:05 +08:00
parent 777515f754
commit 6ab1a9b724
10 changed files with 4214 additions and 3 deletions

1
.gitignore vendored
View file

@ -11,6 +11,7 @@ node_modules
# next.js
/.next/
/.open-next/
/out/
/prisma/
/src/generated/

View file

@ -1,5 +1,6 @@
generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}
datasource db {

View file

@ -1,5 +1,6 @@
generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}
datasource db {

View file

@ -179,6 +179,7 @@ if (cloudMode && cloudUrl) {
/** @type {import('next').NextConfig} */
export default {
reactStrictMode: false,
serverExternalPackages: ['@prisma/client', '.prisma/client'],
env: {
basePath,
cloudMode,

3
open-next.config.ts Normal file
View file

@ -0,0 +1,3 @@
import { defineCloudflareConfig } from '@opennextjs/cloudflare';
export default defineCloudflareConfig({});

View file

@ -47,7 +47,11 @@
"postbuild": "node scripts/postbuild.js",
"test": "jest",
"cypress-open": "cypress open cypress run",
"cypress-run": "cypress run cypress run"
"cypress-run": "cypress run cypress run",
"cloudflare:build": "opennextjs-cloudflare build",
"cloudflare:preview": "opennextjs-cloudflare preview",
"cloudflare:deploy": "opennextjs-cloudflare deploy",
"cf-typegen": "wrangler types --env-interface CloudflareEnv cloudflare-env.d.ts"
},
"lint-staged": {
"**/*.{js,jsx,ts,tsx}": [
@ -72,6 +76,8 @@
"@dicebear/core": "^9.2.1",
"@fontsource/inter": "^4.5.15",
"@hello-pangea/dnd": "^17.0.0",
"@opennextjs/cloudflare": "^1.11.0",
"@prisma/adapter-pg": "^6.17.1",
"@prisma/client": "6.7.0",
"@prisma/extension-read-replicas": "^0.4.1",
"@react-spring/web": "^9.7.3",

4149
pnpm-lock.yaml generated

File diff suppressed because it is too large Load diff

2
public/_headers Normal file
View file

@ -0,0 +1,2 @@
/_next/static/*
Cache-Control: public,max-age=31536000,immutable

View file

@ -1,4 +1,6 @@
import debug from 'debug';
import { getCloudflareContext } from '@opennextjs/cloudflare';
import { PrismaPg } from '@prisma/adapter-pg';
import { PrismaClient } from '@prisma/client';
import { readReplicas } from '@prisma/extension-read-replicas';
import { formatInTimeZone } from 'date-fns-tz';
@ -409,6 +411,25 @@ function getClient(params?: {
options,
} = params || {};
if (navigator.userAgent === 'Cloudflare-Workers') {
return new Proxy({} as PrismaClient, {
get: (target, prop, receiver) => {
const adapter = new PrismaPg({
connectionString:
getCloudflareContext()?.env?.HYPERDRIVE?.connectionString ?? process.env.DATABASE_URL,
maxUses: 1,
});
const prisma = new PrismaClient({
errorFormat: 'pretty',
adapter,
...(logQuery && PRISMA_LOG_OPTIONS),
...options,
});
return Reflect.get(prisma, prop, receiver);
},
});
}
const prisma = new PrismaClient({
errorFormat: 'pretty',
...(logQuery && PRISMA_LOG_OPTIONS),

30
wrangler.toml Normal file
View file

@ -0,0 +1,30 @@
# To deploy on Cloudflare Workers, please follow these two steps
#
# 1. Build the Application: pnpm run cloudflare:build
# 2. Deploy the Application: pnpm run cloudflare:deploy
#
# You need add `DATABASE_URL` variable in build environment and runtime environment.
#
# When use Hyperdrive, you don't need to add the `DATABASE_URL` variable in the runtime environment.
main = ".open-next/worker.js"
name = "umami"
compatibility_date = "2025-09-15"
compatibility_flags = ["nodejs_compat", "global_fetch_strictly_public"]
workers_dev = false
[observability]
enabled = true
[assets]
directory = ".open-next/assets"
binding = "ASSETS"
[[services]]
binding = "WORKER_SELF_REFERENCE"
service = "umami"
# Advanced users can enable hyperdrive to optimize database connection latency.
# For usage, please refer to https://developers.cloudflare.com/hyperdrive/get-started/.
# [[hyperdrive]]
# binding = "HYPERDRIVE"
# id = ""