Skip to content

Fix Supabase client initialization during Next.js build prerendering - #2

Draft
Julez14 with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-build-issues-react-ssr
Draft

Fix Supabase client initialization during Next.js build prerendering#2
Julez14 with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-build-issues-react-ssr

Conversation

Copilot AI commented Dec 13, 2025

Copy link
Copy Markdown

Build fails during prerendering with @supabase/ssr: Your project's URL and API key are required to create a Supabase client! because Supabase clients initialize at module load time before environment variables are available.

Changes

  • lib/supabase/client.ts: Lazy initialization via Proxy pattern - client created on first access rather than at module import
  • lib/supabase/server.ts: Explicit env var validation with clear error messages instead of non-null assertions
  • lib/supabase/middleware.ts: Pass-through when Supabase not configured instead of throwing during build

Implementation

Before:

const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL!;
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!;

export const supabase = createBrowserClient(supabaseUrl, supabaseAnonKey);

After:

let client: SupabaseClient | null = null;

function getSupabaseClient() {
  if (client) return client;
  
  const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
  const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
  
  if (!supabaseUrl || !supabaseAnonKey) {
    throw new Error("Missing Supabase environment variables...");
  }
  
  client = createBrowserClient(supabaseUrl, supabaseAnonKey);
  return client;
}

export const supabase = new Proxy({} as SupabaseClient, {
  get(_target, prop, receiver) {
    const client = getSupabaseClient();
    const value = client[prop as keyof SupabaseClient];
    return typeof value === "function" ? value.bind(client) : value;
  },
});

Environment variables now only required at runtime when Supabase methods are actually invoked, not during static generation.

Original prompt

Fix the build issues associated with the latest Pull Request. The PR is titled "Fix React Server Components CVE vulnerabilities"

Build Logs:
Error occurred prerendering page "/_not-found". Read more: https://nextjs.org/docs/messages/prerender-error
Error: @supabase/ssr: Your project's URL and API key are required to create a Supabase client!
Check your Supabase project's API settings to find these values
https://supabase.com/dashboard/project/_/settings/api
at tZ (/vercel/path0/.next/server/chunks/473.js:21:89310)
at 8600 (/vercel/path0/.next/server/app/_not-found/page.js:7:4010)
at t (/vercel/path0/.next/server/webpack-runtime.js:1:128)
at 4623 (/vercel/path0/.next/server/app/_not-found/page.js:7:919)
at Object.t [as require] (/vercel/path0/.next/server/webpack-runtime.js:1:128)
at require (/vercel/path0/node_modules/.pnpm/next@15.2.8_react-dom@19.0.0_react@19.0.0__react@19.0.0/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:134:10079)
at c (/vercel/path0/node_modules/.pnpm/next@15.2.8_react-dom@19.0.0_react@19.0.0__react@19.0.0/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:92:680)
at F (/vercel/path0/node_modules/.pnpm/next@15.2.8_react-dom@19.0.0_react@19.0.0__react@19.0.0/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:92:10643)
at P (/vercel/path0/node_modules/.pnpm/next@15.2.8_react-dom@19.0.0_react@19.0.0__react@19.0.0/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:92:8692)
at rI (/vercel/path0/node_modules/.pnpm/next@15.2.8_react-dom@19.0.0_react@19.0.0__react@19.0.0/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:76:33106)
Export encountered an error on /_not-found/page: /_not-found, exiting the build.
⨯ Next.js build worker exited with code: 1 and signal: null
 ELIFECYCLE  Command failed with exit code 1.
Error: Command "pnpm run build" exited with 1


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@vercel

vercel Bot commented Dec 13, 2025

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
v0-image-analysis Error Error Dec 13, 2025 6:00pm
Copilot AI and others added 3 commits December 13, 2025 17:57
…build

Co-authored-by: Julez14 <31865267+Julez14@users.noreply.github.com>
Co-authored-by: Julez14 <31865267+Julez14@users.noreply.github.com>
Co-authored-by: Julez14 <31865267+Julez14@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix build issues associated with React Server Components Dec 13, 2025
Copilot AI requested a review from Julez14 December 13, 2025 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants