-
What is the documentation issue?I think we can clear up some of the flags documentation to list what is required for what and where. Is there any context that might help us understand?I find the whole flags documentation a bit confusing. The first section outlines how to set up the Flags SDK in NextJS. It says the following:
I tried to follow that guide, as well as the Manage Flags from the Toolbar guide, but my overrides don't seem to apply automatically. I'm probably just misunderstanding the docs somewhere, but I am not sure. Seems like I've set things up properly. // src/flag.ts
import { unstable_flag as flag } from '@vercel/flags/next'
import { getFlags } from './app/getFlags'
export const myFlag = flag({
key: 'my-flag',
decide: () => false,
description: 'My Flag',
origin: 'https://example.com/#new-feature',
} // src/app/layout.tsx
export default async function Layout({
children, // will be a page or nested layout
}: {
children: React.ReactNode
}) {
const enableMyFlag = await myFlag()
console.log('myFlag', enableMyFlag) import { NextResponse, type NextRequest } from 'next/server'
import { verifyAccess, type ApiData } from '@vercel/flags'
export async function GET(request: NextRequest) {
const access = await verifyAccess(request.headers.get('Authorization'))
if (!access) return NextResponse.json(null, { status: 401 })
return NextResponse.json<ApiData>({
definitions: {
myFlag: {
description: 'My Flag',
origin: 'https://example.com/#new-feature',
options: [
{ value: false, label: 'Off' },
{ value: true, label: 'On' },
],
},
},
})
} When I console log my flag in my layout, regardless of my toolbar overrides it shows False. I am not sure where I am going wrong. TIA Does the docs page already exist? Please link to it. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Is it proper or expected of me to add my import { unstable_flag as flag } from '@vercel/flags/next'
import { get } from '@vercel/edge-config'
import { getFlags } from './app/getFlags'
export const myFlag = flag({
key: 'enable-myflag',
async decide() {
const overrides = await getFlags()
if (overrides.myFlag !== undefined) {
return overrides.myFlag
}
const value = await get('enable-myflag')
return value ?? false
},
}) |
Beta Was this translation helpful? Give feedback.
-
@Tigatok Thank you for the feedback! I will share this with the team. I moved this out of Next.js issues since this is also technically related to Vercel documentation, not Next.js documentation itself (nextjs.org/docs). That seems right, with Edge Config. From our docs →
The guide is sort of set up to first have a Let me know if that help clarifies! |
Beta Was this translation helpful? Give feedback.
-
Hi @Tigatok, thanks for trying out the Flags SDK. 🙇 The issue here is likely a mismatch between the key of your flags in the // flag.ts
export const myFlag = flag({
key: 'my-flag', // <-- Key is provided as "my-flag"
decide: () => false,
description: 'My Flag',
origin: 'https://example.com/#new-feature',
}
// .well-known/vercel/flags/route.ts
return NextResponse.json<ApiData>({
definitions: {
myFlag: { // <-- Key is provided as "myFlag"
description: 'My Flag',
origin: 'https://example.com/#new-feature',
options: [
{ value: false, label: 'Off' },
{ value: true, label: 'On' },
],
},
},
}) To help prevent this, you can use unstable_getProviderData to convert your flags to definitions. It works like this: // app/flags.ts
export const myFlag = flag({key: 'my-flag'})
// .well-known/vercel/flags/route.ts
import { verifyAccess, type ProviderData } from '@vercel/flags';
import { unstable_getProviderData as getProviderData } from '@vercel/flags/next';
import { NextResponse } from 'next/server';
import * as flags from '#/app/flags'; // update this to point to where you export your flags
export const GET = async (request: Request): Promise<Response> => {
const access = await verifyAccess(request.headers.get('Authorization'));
if (!access) return NextResponse.json(null, { status: 401 });
return NextResponse.json<ProviderData>(getProviderData(flags));
}; I expect you will see overrides working in the toolbar with this change, and you won't have to worry about getting the key right in the future. If you define all your flags in one place, you won't have to update the route.ts in the future either. |
Beta Was this translation helpful? Give feedback.
Hi @Tigatok, thanks for trying out the Flags SDK. 🙇
The issue here is likely a mismatch between the key of your flags in the
.well-known
route and your flag definition: