Merge pull request #33 from abhi1693/ishan/fix-activity-clerkprovider
Fix /activity prerender crash when ClerkProvider is skipped
This commit is contained in:
@@ -17,11 +17,14 @@ import {
|
|||||||
|
|
||||||
import type { ComponentProps } from "react";
|
import type { ComponentProps } from "react";
|
||||||
|
|
||||||
|
import { isLikelyValidClerkPublishableKey } from "@/auth/clerkKey";
|
||||||
|
|
||||||
export function isClerkEnabled(): boolean {
|
export function isClerkEnabled(): boolean {
|
||||||
// Invariant: Clerk is disabled ONLY when the publishable key is absent.
|
// IMPORTANT: keep this in sync with AuthProvider; otherwise components like
|
||||||
// If a key is present, we assume Clerk is intended to be enabled and we let
|
// <SignedOut/> may render without a <ClerkProvider/> and crash during prerender.
|
||||||
// Clerk fail fast if the key is invalid/misconfigured.
|
return isLikelyValidClerkPublishableKey(
|
||||||
return Boolean(process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY);
|
process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function SignedIn(props: { children: ReactNode }) {
|
export function SignedIn(props: { children: ReactNode }) {
|
||||||
|
|||||||
22
frontend/src/auth/clerkKey.ts
Normal file
22
frontend/src/auth/clerkKey.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
// Shared Clerk publishable-key gating logic.
|
||||||
|
//
|
||||||
|
// IMPORTANT: keep this file dependency-free (no `"use client"`, no React, no Clerk imports)
|
||||||
|
// so it can be used from both client and server/edge entrypoints.
|
||||||
|
|
||||||
|
export function isLikelyValidClerkPublishableKey(key: string | undefined): key is string {
|
||||||
|
if (!key) return false;
|
||||||
|
|
||||||
|
// Clerk publishable keys look like: pk_test_... or pk_live_...
|
||||||
|
// In CI we want builds to stay secretless; if the key isn't present/valid,
|
||||||
|
// we skip Clerk entirely so `next build` can prerender.
|
||||||
|
//
|
||||||
|
// Note: this is a conservative heuristic (not an authoritative validation).
|
||||||
|
const m = /^pk_(test|live)_([A-Za-z0-9]+)$/.exec(key);
|
||||||
|
if (!m) return false;
|
||||||
|
|
||||||
|
const body = m[2];
|
||||||
|
if (body.length < 16) return false;
|
||||||
|
if (/^0+$/.test(body)) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
@@ -3,23 +3,7 @@
|
|||||||
import { ClerkProvider } from "@clerk/nextjs";
|
import { ClerkProvider } from "@clerk/nextjs";
|
||||||
import type { ReactNode } from "react";
|
import type { ReactNode } from "react";
|
||||||
|
|
||||||
function isLikelyValidClerkPublishableKey(
|
import { isLikelyValidClerkPublishableKey } from "@/auth/clerkKey";
|
||||||
key: string | undefined,
|
|
||||||
): key is string {
|
|
||||||
if (!key) return false;
|
|
||||||
// Clerk publishable keys look like: pk_test_... or pk_live_...
|
|
||||||
// In CI we want builds to stay secretless; if the key isn't present/valid,
|
|
||||||
// we skip Clerk entirely so `next build` can prerender.
|
|
||||||
//
|
|
||||||
// Note: Clerk appears to validate key *contents*, not just shape. We therefore
|
|
||||||
// use a conservative heuristic to avoid treating obvious placeholders as valid.
|
|
||||||
const m = /^pk_(test|live)_([A-Za-z0-9]+)$/.exec(key);
|
|
||||||
if (!m) return false;
|
|
||||||
const body = m[2];
|
|
||||||
if (body.length < 16) return false;
|
|
||||||
if (/^0+$/.test(body)) return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function AuthProvider({ children }: { children: ReactNode }) {
|
export function AuthProvider({ children }: { children: ReactNode }) {
|
||||||
const publishableKey = process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY;
|
const publishableKey = process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY;
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { clerkMiddleware } from "@clerk/nextjs/server";
|
import { clerkMiddleware } from "@clerk/nextjs/server";
|
||||||
|
|
||||||
|
import { isLikelyValidClerkPublishableKey } from "@/auth/clerkKey";
|
||||||
|
|
||||||
const isClerkEnabled = () =>
|
const isClerkEnabled = () =>
|
||||||
Boolean(process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY);
|
isLikelyValidClerkPublishableKey(process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY);
|
||||||
|
|
||||||
export default isClerkEnabled() ? clerkMiddleware() : () => NextResponse.next();
|
export default isClerkEnabled() ? clerkMiddleware() : () => NextResponse.next();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user