Skip to content

An AI-powered search engine with a generative UI

Fix: Prevent Supabase client init errors when env vars are missing

Yoshiki Miurab261e0c5643a
diff --git a/app/layout.tsx b/app/layout.tsx
index 416fa8c8..d9820e9d 100644
--- a/app/layout.tsx
+++ b/app/layout.tsx
@@ -48,10 +48,17 @@ export default async function RootLayout({
}: Readonly<{
children: React.ReactNode
}>) {
- const supabase = await createClient()
- const {
- data: { user }
- } = await supabase.auth.getUser()
+ let user = null
+ const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
+ const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
+
+ if (supabaseUrl && supabaseAnonKey) {
+ const supabase = await createClient()
+ const {
+ data: { user: supabaseUser }
+ } = await supabase.auth.getUser()
+ user = supabaseUser
+ }
return (
<html lang="en" suppressHydrationWarning>
diff --git a/lib/auth/get-current-user.ts b/lib/auth/get-current-user.ts
index 16547ae1..2663346a 100644
--- a/lib/auth/get-current-user.ts
+++ b/lib/auth/get-current-user.ts
@@ -1,6 +1,13 @@
import { createClient } from '@/lib/supabase/server'
export async function getCurrentUser() {
+ const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
+ const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
+
+ if (!supabaseUrl || !supabaseAnonKey) {
+ return null // Supabase is not configured
+ }
+
const supabase = await createClient()
const { data } = await supabase.auth.getUser()
return data.user ?? null
diff --git a/middleware.ts b/middleware.ts
index afe29b60..19ca1829 100644
--- a/middleware.ts
+++ b/middleware.ts
@@ -1,8 +1,18 @@
import { updateSession } from '@/lib/supabase/middleware'
-import { type NextRequest } from 'next/server'
+import { type NextRequest, NextResponse } from 'next/server'
export async function middleware(request: NextRequest) {
- return await updateSession(request)
+ const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
+ const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
+
+ if (supabaseUrl && supabaseAnonKey) {
+ return await updateSession(request)
+ }
+
+ // If Supabase is not configured, just pass the request through
+ return NextResponse.next({
+ request
+ })
}
export const config = {
@@ -14,6 +24,6 @@ export const config = {
* - favicon.ico (favicon file)
* Feel free to modify this pattern to include more paths.
*/
- '/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
- ],
+ '/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)'
+ ]
}