Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

Add feature flags (#405)

Sean Boult4441429a82fe
diff --git a/apps/web/global.d.ts b/apps/web/global.d.ts
index 256daf24..cf607717 100644
--- a/apps/web/global.d.ts
+++ b/apps/web/global.d.ts
@@ -11,5 +11,6 @@ declare namespace NodeJS {
GITHUB_SECRET: string;
PORT: number;
RESEND_API_KEY: string;
+ EDGE_CONFIG: string;
}
}
diff --git a/apps/web/package.json b/apps/web/package.json
index 74de6afb..4b07c850 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -33,6 +33,7 @@
"@types/lodash": "^4.14.195",
"@uiw/react-md-editor": "^3.23.3",
"@uploadthing/react": "^5.2.0",
+ "@vercel/edge-config": "^0.2.1",
"clsx": "^2.0.0",
"cmdk": "^0.2.0",
"date-fns": "^2.30.0",
diff --git a/apps/web/src/app/api/auth/creds/route.ts b/apps/web/src/app/api/auth/creds/route.ts
deleted file mode 100644
index ab695c4e..00000000
--- a/apps/web/src/app/api/auth/creds/route.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-export async function GET() {
- return new Response('Auth required', {
- status: 401,
- headers: { 'WWW-authenticate': 'Basic realm="Secure Area"' },
- });
-}
diff --git a/apps/web/src/app/api/flags/route.ts b/apps/web/src/app/api/flags/route.ts
new file mode 100644
index 00000000..b6fcee59
--- /dev/null
+++ b/apps/web/src/app/api/flags/route.ts
@@ -0,0 +1,7 @@
+import { NextResponse } from 'next/server';
+import { getAllFlags } from '~/utils/feature-flags';
+
+export async function GET() {
+ const flags = await getAllFlags();
+ return NextResponse.json(flags);
+}
diff --git a/apps/web/src/app/page.tsx b/apps/web/src/app/page.tsx
index 86238975..ae323de6 100644
--- a/apps/web/src/app/page.tsx
+++ b/apps/web/src/app/page.tsx
@@ -1,5 +1,4 @@
import type { Metadata } from 'next';
-import { Provider as BalancerProvider } from 'react-wrap-balancer';
import Community from '~/components/landing/community/community';
import Features from '~/components/landing/features';
import Hero from '~/components/landing/hero';
@@ -15,13 +14,11 @@ export const metadata: Metadata = {
export default async function Index() {
return (
<>
- <BalancerProvider>
- <Hero />
- <Features />
- <Community />
- <Waitlist />
- <Footsies />
- </BalancerProvider>
+ <Hero />
+ <Features />
+ <Community />
+ <Waitlist />
+ <Footsies />
</>
);
}
diff --git a/apps/web/src/app/providers.tsx b/apps/web/src/app/providers.tsx
index 80d79dc5..44189790 100644
--- a/apps/web/src/app/providers.tsx
+++ b/apps/web/src/app/providers.tsx
@@ -1,24 +1,40 @@
'use client';
+import { Provider as BalancerProvider } from 'react-wrap-balancer';
import { SessionProvider } from '@repo/auth/react';
import { ThemeProvider } from 'next-themes';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { TooltipProvider } from '@repo/ui';
+import React, { useEffect, useState } from 'react';
interface Props {
children: React.ReactNode;
}
const queryClient = new QueryClient();
+export const FeatureFlagContext = React.createContext<Record<string, boolean>>({});
export function Providers({ children }: Props) {
+ const [featureFlags, setFeatureFlags] = useState<Record<string, boolean>>({});
+ useEffect(() => {
+ fetch('/api/flags')
+ .then((res) => res.json())
+ .then((data) => {
+ setFeatureFlags(data);
+ });
+ }, []);
+
return (
- <QueryClientProvider client={queryClient}>
- <SessionProvider>
- <ThemeProvider attribute="class">
- <TooltipProvider>{children}</TooltipProvider>
- </ThemeProvider>
- </SessionProvider>
- </QueryClientProvider>
+ <FeatureFlagContext.Provider value={featureFlags}>
+ <BalancerProvider>
+ <QueryClientProvider client={queryClient}>
+ <SessionProvider>
+ <ThemeProvider attribute="class">
+ <TooltipProvider>{children}</TooltipProvider>
+ </ThemeProvider>
+ </SessionProvider>
+ </QueryClientProvider>
+ </BalancerProvider>
+ </FeatureFlagContext.Provider>
);
}
diff --git a/apps/web/src/components/ui/navigation.tsx b/apps/web/src/components/ui/navigation.tsx
index 6514f93a..b5eb2ec5 100644
--- a/apps/web/src/components/ui/navigation.tsx
+++ b/apps/web/src/components/ui/navigation.tsx
@@ -15,11 +15,14 @@ import { Loader2, LogIn, Moon, Plus, Settings, Settings2, Sun, User } from '@rep
import { useTheme } from 'next-themes';
import Link from 'next/link';
import { usePathname } from 'next/navigation';
-import { useEffect, useState } from 'react';
+import { useContext, useEffect, useState } from 'react';
import { getBaseUrl } from '~/utils/getBaseUrl';
+import { FeatureFlagContext } from '~/app/providers';
export function Navigation() {
const pathname = usePathname();
+ const featureFlags = useContext(FeatureFlagContext);
+
return (
<header className="z-10 w-full">
<nav
@@ -65,14 +68,16 @@ export function Navigation() {
</span>
</a>
- <Link href="/explore">
- <span className={navigationMenuTriggerStyle()}>Explore</span>
- </Link>
+ {featureFlags?.exploreButton ? (
+ <Link href="/explore">
+ <span className={navigationMenuTriggerStyle()}>Explore</span>
+ </Link>
+ ) : null}
</div>
<div className="flex">
<div className="flex items-center justify-end gap-2">
<ThemeButton />
- <LoginButton />
+ {featureFlags?.loginButton ? <LoginButton /> : null}
</div>
</div>
</div>
diff --git a/apps/web/src/middleware.ts b/apps/web/src/middleware.ts
index dd66f9ac..71baaa59 100644
--- a/apps/web/src/middleware.ts
+++ b/apps/web/src/middleware.ts
@@ -1,23 +1,11 @@
import { type NextRequest, NextResponse } from 'next/server';
export function middleware(req: NextRequest) {
- if (process.env.NODE_ENV === 'production') {
- const basicAuth = req.headers.get('authorization');
- const url = req.nextUrl;
+ const path = req.nextUrl.pathname;
- if (basicAuth) {
- const authValue = basicAuth.split(' ')[1];
- const [user, pwd] = atob(authValue ?? '').split(':');
-
- if (user === process.env.USERNAME && process.env.PASSWORD === pwd) {
- return NextResponse.next();
- }
- }
-
- // Make the user get some damn creds
- url.pathname = '/api/auth/creds';
-
- return NextResponse.rewrite(url);
+ // if path is /explore or /challenge/* and redirect to /waitlist
+ if (path === '/explore' || path.startsWith('/challenge/')) {
+ return NextResponse.redirect(new URL('/waitlist', req.url));
}
return NextResponse.next();
diff --git a/apps/web/src/utils/feature-flags.ts b/apps/web/src/utils/feature-flags.ts
new file mode 100644
index 00000000..c0ddf693
--- /dev/null
+++ b/apps/web/src/utils/feature-flags.ts
@@ -0,0 +1,22 @@
+import { createClient } from '@vercel/edge-config';
+
+const edgeConfig = createClient(process.env.EDGE_CONFIG);
+/**
+ * This will try to eval a feature flag on the prod app
+ *
+ * @example
+ * ```
+ * const isFeatureEnabled = await evaluateFlag('my-feature-flag');
+ * ```
+ */
+export async function evaluateFlag(key: string): Promise<boolean> {
+ const featureFlag = await edgeConfig.get(key);
+
+ const rawValue = featureFlag?.valueOf();
+ return rawValue == true;
+}
+
+export async function getAllFlags(): Promise<any> {
+ const allFeatureFlag = await edgeConfig.getAll();
+ return allFeatureFlag;
+}
diff --git a/apps/web/src/utils/featureFlags.ts b/apps/web/src/utils/featureFlags.ts
deleted file mode 100644
index 753a8c37..00000000
--- a/apps/web/src/utils/featureFlags.ts
+++ /dev/null
@@ -1,4 +0,0 @@
-// For now we use this since it is a simp build time thingy
-export function isProd() {
- return process.env.NODE_ENV === 'production';
-}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 5c5c52b8..7471caeb 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -252,6 +252,9 @@ importers:
'@uploadthing/react':
specifier: ^5.2.0
+ '@vercel/edge-config':
+ specifier: ^0.2.1
+ version: 0.2.1
clsx:
specifier: ^2.0.0
version: 2.0.0
@@ -3918,6 +3921,17 @@ packages:
zod: 3.21.4
dev: false
+ /@vercel/[email protected]:
+ resolution: {integrity: sha512-NRIBwfcS0bUoUbRWlNGetqjvLSwgYH/BqKqDN7vK1g32p7dN96k0712COgaz6VFizAm9b0g6IG6hR6+hc0KCPg==}
+ dev: false
+
+ /@vercel/[email protected]:
+ resolution: {integrity: sha512-847kYqJEbga4PGgNrctQ9XsD+2Kw/S+UjzZnIFpebQ9VbdtB0MX4anq33WetcYZYPfhZd2L0uXVnY/BcjI5dOw==}
+ engines: {node: '>=14.6'}
+ dependencies:
+ '@vercel/edge-config-fs': 0.1.0
+ dev: false
+
resolution: {integrity: sha512-/uj/sVvxE4SFYUmc4VnMHGkVx/m+jkWzsqAawAghK8BFm5KXP8WAJ475osnO0xhqhptlZLWJDXcxGDht/sVm+g==}
engines: {node: '>=16'}
diff --git a/turbo.json b/turbo.json
index e553c4f7..d9667160 100644
--- a/turbo.json
+++ b/turbo.json
@@ -13,6 +13,7 @@
"GITHUB_ADMIN_SECRET",
"UPLOADTHING_SECRET",
"UPLOADTHING_APP_ID",
+ "EDGE_CONFIG",
"NEXTAUTH_SECRET",
"NEXTAUTH_URL",
"RESEND_API_KEY",