Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat: Use Vercel Toolbar for overriding feature flags (#29697)

Ivan Vasilovb77a1b48be61
diff --git a/apps/studio/components/ui/Flag/FlagProvider.tsx b/apps/studio/components/ui/Flag/FlagProvider.tsx
index 57f11d9fd69..c846e102a88 100644
--- a/apps/studio/components/ui/Flag/FlagProvider.tsx
+++ b/apps/studio/components/ui/Flag/FlagProvider.tsx
@@ -1,10 +1,23 @@
import type { User } from '@supabase/supabase-js'
+import { FlagValues } from '@vercel/flags/react'
import { PropsWithChildren, useEffect, useState } from 'react'
-import { IS_PLATFORM } from 'lib/constants'
-import FlagContext from './FlagContext'
import { useUser } from 'lib/auth'
import { getFlags } from 'lib/configcat'
+import { IS_PLATFORM } from 'lib/constants'
+import FlagContext from './FlagContext'
+
+var getCookies = function () {
+ var pairs = document.cookie.split(';')
+ var cookies: Record<string, string> = {}
+ for (var i = 0; i < pairs.length; i++) {
+ var [t_key, value] = pairs[i].split('=')
+ const key = t_key.trim()
+
+ cookies[key] = unescape(value)
+ }
+ return cookies
+}
const FlagProvider = ({ children }: PropsWithChildren<{}>) => {
const user = useUser()
@@ -15,9 +28,14 @@ const FlagProvider = ({ children }: PropsWithChildren<{}>) => {
const processFlags = async (user?: User) => {
const flagStore: any = {}
const flagValues = await getFlags(user)
-
- flagValues.forEach((item: any) => {
- flagStore[item.settingKey] = item.settingValue
+ let overridesCookieValue: Record<string, boolean> = {}
+ try {
+ const cookies = getCookies()
+ overridesCookieValue = JSON.parse(cookies['vercel-flag-overrides'])
+ } catch {}
+
+ flagValues.forEach((item) => {
+ flagStore[item.settingKey] = overridesCookieValue[item.settingKey] ?? item.settingValue
})
setStore(flagStore)
}
@@ -30,7 +48,12 @@ const FlagProvider = ({ children }: PropsWithChildren<{}>) => {
if (IS_PLATFORM) processFlags(user ?? undefined)
}, [user])
- return <Provider value={store}>{children}</Provider>
+ return (
+ <Provider value={store}>
+ <FlagValues values={store} />
+ {children}
+ </Provider>
+ )
}
export default FlagProvider
diff --git a/apps/studio/lib/configcat.ts b/apps/studio/lib/configcat.ts
index 8fb166f3872..c566a75449a 100644
--- a/apps/studio/lib/configcat.ts
+++ b/apps/studio/lib/configcat.ts
@@ -18,5 +18,8 @@ function getClient() {
}
export async function getFlags(user?: User) {
- return getClient().getAllValuesAsync(new configcat.User(user?.email ?? ''))
+ if (user) {
+ return getClient().getAllValuesAsync(new configcat.User(user?.email ?? ''))
+ }
+ return []
}
diff --git a/apps/studio/next.config.js b/apps/studio/next.config.js
index 42780a847f4..99e596339d8 100644
--- a/apps/studio/next.config.js
+++ b/apps/studio/next.config.js
@@ -102,6 +102,16 @@ const nextConfig = {
experimental: {
webpackBuildWorker: true,
},
+ async rewrites() {
+ return [
+ {
+ source: `/.well-known/vercel/flags`,
+ // TODO: Replace this with supabase.com/.well-known/vercel/flags once this PR is merged.
+ destination: `${process.env.VERCEL_URL ? `https://${process.env.VERCEL_URL}` : process.env.NEXT_PUBLIC_SITE_URL}${process.env.NEXT_PUBLIC_BASE_PATH}/.well-known/vercel/flags`,
+ basePath: false,
+ },
+ ]
+ },
async redirects() {
return [
...(process.env.NEXT_PUBLIC_IS_PLATFORM === 'true'
@@ -419,6 +429,15 @@ const nextConfig = {
},
],
},
+ {
+ source: '/.well-known/vercel/flags',
+ headers: [
+ {
+ key: 'content-type',
+ value: 'application/json',
+ },
+ ],
+ },
{
source: '/img/:slug*',
headers: [{ key: 'cache-control', value: 'max-age=2592000' }],
diff --git a/apps/studio/package.json b/apps/studio/package.json
index 97f9cac2c07..55ad9b69b3d 100644
--- a/apps/studio/package.json
+++ b/apps/studio/package.json
@@ -45,6 +45,7 @@
"@tanstack/react-query": "4.35.7",
"@tanstack/react-query-devtools": "4.35.7",
"@uidotdev/usehooks": "^2.4.1",
+ "@vercel/flags": "^2.6.0",
"@vitejs/plugin-react": "^4.2.1",
"@zip.js/zip.js": "^2.7.29",
"ai": "^2.2.31",
diff --git a/apps/studio/public/.well-known/vercel/flags b/apps/studio/public/.well-known/vercel/flags
new file mode 100644
index 00000000000..a9f85c63631
--- /dev/null
+++ b/apps/studio/public/.well-known/vercel/flags
@@ -0,0 +1 @@
+{ "overrideEncryptionMode": "plaintext" }
\ No newline at end of file
diff --git a/apps/www/next.config.mjs b/apps/www/next.config.mjs
index 29ca81298a4..89419622ee3 100644
--- a/apps/www/next.config.mjs
+++ b/apps/www/next.config.mjs
@@ -70,6 +70,15 @@ const nextConfig = {
},
],
},
+ {
+ source: '/.well-known/vercel/flags',
+ headers: [
+ {
+ key: 'content-type',
+ value: 'application/json',
+ },
+ ],
+ },
]
},
async rewrites() {
diff --git a/apps/www/public/.well-known/vercel/flags b/apps/www/public/.well-known/vercel/flags
new file mode 100644
index 00000000000..82558ec1782
--- /dev/null
+++ b/apps/www/public/.well-known/vercel/flags
@@ -0,0 +1 @@
+{ "overrideEncryptionMode": "plaintext" }
diff --git a/package-lock.json b/package-lock.json
index d9bdda56fe8..50a1a2e239e 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1831,6 +1831,7 @@
"@tanstack/react-query": "4.35.7",
"@tanstack/react-query-devtools": "4.35.7",
"@uidotdev/usehooks": "^2.4.1",
+ "@vercel/flags": "^2.6.0",
"@vitejs/plugin-react": "^4.2.1",
"@zip.js/zip.js": "^2.7.29",
"ai": "^2.2.31",
@@ -16314,6 +16315,34 @@
"version": "1.2.0",
"license": "ISC"
},
+ "node_modules/@vercel/flags": {
+ "version": "2.6.0",
+ "resolved": "https://registry.npmjs.org/@vercel/flags/-/flags-2.6.0.tgz",
+ "integrity": "sha512-GvLX0CK/OsIqq672OBvcCeu0k3tb3QdE0lRn1P5zulMQJIPTLlUDEH0y9TeAyC7TMWlpTTT5Bealqu0rmncyXQ==",
+ "dependencies": {
+ "jose": "5.2.1"
+ },
+ "peerDependencies": {
+ "@sveltejs/kit": "*",
+ "next": "*",
+ "react": "*",
+ "react-dom": "*"
+ },
+ "peerDependenciesMeta": {
+ "@sveltejs/kit": {
+ "optional": true
+ },
+ "next": {
+ "optional": true
+ },
+ "react": {
+ "optional": true
+ },
+ "react-dom": {
+ "optional": true
+ }
+ }
+ },
"node_modules/@vercel/og": {
"version": "0.6.2",
"resolved": "https://registry.npmjs.org/@vercel/og/-/og-0.6.2.tgz",
@@ -26665,6 +26694,14 @@
"jiti": "bin/jiti.js"
}
},
+ "node_modules/jose": {
+ "version": "5.2.1",
+ "resolved": "https://registry.npmjs.org/jose/-/jose-5.2.1.tgz",
+ "integrity": "sha512-qiaQhtQRw6YrOaOj0v59h3R6hUY9NvxBmmnMfKemkqYmBB0tEc97NbLP7ix44VP5p9/0YHG8Vyhzuo5YBNwviA==",
+ "funding": {
+ "url": "https://github.com/sponsors/panva"
+ }
+ },
"node_modules/jotai": {
"version": "2.8.1",
"resolved": "https://registry.npmjs.org/jotai/-/jotai-2.8.1.tgz",