Simplify D1 database access and add fallback to LocalStorage for Edge Runtime compatibility

This commit is contained in:
katelya
2025-09-05 16:21:41 +08:00
parent 62072a5558
commit 3ce1bd1ce4
4 changed files with 48 additions and 67 deletions
+10 -30
View File
@@ -1,42 +1,22 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { NextRequest, NextResponse } from 'next/server';
import { NextResponse } from 'next/server';
export const runtime = 'edge';
export async function GET(request: NextRequest) {
export async function GET() {
try {
const debugInfo = {
return NextResponse.json({
status: 'ok',
timestamp: new Date().toISOString(),
environment: {
NODE_ENV: process.env.NODE_ENV,
NEXT_PUBLIC_STORAGE_TYPE: process.env.NEXT_PUBLIC_STORAGE_TYPE,
USERNAME: process.env.USERNAME ? '***' : undefined,
PASSWORD: process.env.PASSWORD ? '***' : undefined,
},
globalThis: {
hasDB: typeof globalThis !== 'undefined' && !!(globalThis as any).DB,
hasProcess: typeof globalThis !== 'undefined' && !!(globalThis as any).process,
hasCloudflare: typeof globalThis !== 'undefined' && !!(globalThis as any).cloudflare,
},
processEnv: {
hasDB: !!(process.env as any).DB,
keys: Object.keys(process.env).filter(key =>
key.startsWith('DB') ||
key.startsWith('NEXT_') ||
key.startsWith('CF_') ||
key.startsWith('CLOUDFLARE_')
),
},
runtime: 'edge',
userAgent: request.headers.get('user-agent')?.slice(0, 100),
};
return NextResponse.json(debugInfo);
storageType: process.env.NEXT_PUBLIC_STORAGE_TYPE || 'localstorage',
hasDB: !!(globalThis as any).DB,
nodeEnv: process.env.NODE_ENV
});
} catch (error) {
return NextResponse.json({
error: 'Debug info collection failed',
message: error instanceof Error ? error.message : 'Unknown error',
timestamp: new Date().toISOString()
error: 'Debug failed',
message: error instanceof Error ? error.message : 'Unknown error'
}, { status: 500 });
}
}