-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
136 additions
and
45 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
examples/docs/app/getting-started/overview/[code]/flags.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { flag } from '@vercel/flags/next'; | ||
|
||
export const randomFlag = flag<boolean>({ | ||
key: 'random-flag', | ||
description: 'A flag that is on for 50% of visitors', | ||
decide() { | ||
return Math.random() > 0.5; | ||
}, | ||
}); | ||
|
||
export const overviewFlags = [randomFlag]; |
13 changes: 13 additions & 0 deletions
13
examples/docs/app/getting-started/overview/[code]/middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { precompute } from '@vercel/flags/next'; | ||
import { type NextRequest, NextResponse } from 'next/server'; | ||
import { overviewFlags } from './flags'; | ||
|
||
export async function overviewMiddleware(request: NextRequest) { | ||
// precompute the flags | ||
const code = await precompute(overviewFlags); | ||
|
||
// rewrite the page with the code | ||
return NextResponse.rewrite( | ||
new URL(`/getting-started/overview/${code}`, request.url), | ||
); | ||
} |
72 changes: 72 additions & 0 deletions
72
examples/docs/app/getting-started/overview/[code]/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Content } from '@/components/content'; | ||
import { DemoFlag } from '@/components/demo-flag'; | ||
import { randomFlag, overviewFlags } from './flags'; | ||
import { ReloadButton } from './reload-button'; | ||
import { CodeBlock } from '@/components/code-block'; | ||
import Link from 'next/link'; | ||
|
||
export const dynamic = 'error'; | ||
|
||
export default async function Page({ | ||
params, | ||
}: { | ||
params: Promise<{ code: string }>; | ||
}) { | ||
const awaitedParams = await params; | ||
const overview = await randomFlag(awaitedParams.code, overviewFlags); | ||
return ( | ||
<Content crumbs={['getting-started', 'overview']}> | ||
<h3>Overview</h3> | ||
<p>The Flags SDK makes it easy to use feature flags in Next.js.</p> | ||
<ul> | ||
<li>Works with any flag provider, with built-in adapters for many</li> | ||
<li>App Router, Pages Router and Edge Middleware compatible</li> | ||
<li>Supports dynamic and static pages</li> | ||
<li>Implements best practices to avoid common pitfalls</li> | ||
</ul> | ||
<h4>Usage example</h4> | ||
<p> | ||
Declare a new feature flag using the <code>flag</code> function. | ||
</p> | ||
<CodeBlock> | ||
{` | ||
import { flag } from '@vercel/flags/next'; | ||
export const randomFlag = flag({ | ||
key: "random-flag", | ||
decide() { | ||
// this flag will be on for 50% of visitors | ||
return Math.random() > 0.5; | ||
} | ||
});`} | ||
</CodeBlock> | ||
<p>Use the flag by calling it like any other async function.</p> | ||
<CodeBlock>{`await randomFlag()`}</CodeBlock> | ||
<p>This demo flag is on for 50% of visitors.</p> | ||
<DemoFlag name={randomFlag.key} value={overview} /> | ||
<ReloadButton /> | ||
<p> | ||
See the <Link href="/getting-started/quickstart">Quickstart</Link> for | ||
full setup instructions. | ||
</p> | ||
<h4>Introduction</h4> | ||
<p> | ||
This package provides a simple way to use feature flags in your Next.js | ||
applications. It can be used no matter if your application is hosted on | ||
Vercel or not. It works with App Router, Pages Router and Edge | ||
Middleware. It also works with any feature flag provider. | ||
</p> | ||
<p> | ||
This package encodes the best practices when using feature flags in | ||
Next.js. We also understand that you sometimes need to deviate from the | ||
golden path, and have examples for those cases as well. | ||
</p> | ||
<p> | ||
While this package is called <code>@vercel/flags</code> it does not | ||
require using Vercel. It is also not limited to feature flags, and can | ||
be used for experimentation, A/B testing and any other dynamic flagging | ||
of code. | ||
</p> | ||
</Content> | ||
); | ||
} |
20 changes: 20 additions & 0 deletions
20
examples/docs/app/getting-started/overview/[code]/reload-button.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use client'; | ||
|
||
import { Button } from '@/components/ui/button'; | ||
import { useRouter } from 'next/navigation'; | ||
|
||
export function ReloadButton() { | ||
const router = useRouter(); | ||
|
||
return ( | ||
<Button | ||
type="button" | ||
onClick={() => { | ||
router.refresh(); | ||
}} | ||
variant="outline" | ||
> | ||
Reload page | ||
</Button> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters