-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathssr.tsx
56 lines (50 loc) · 1.28 KB
/
ssr.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import {
flagsClient,
evaluateFlags,
getDefinitions,
type IVariant,
} from "@unleash/nextjs";
import type { GetServerSideProps, NextPage } from "next";
import { UNLEASH_COOKIE_NAME } from "../utils";
type Data = {
isEnabled: boolean;
variant: IVariant;
percent: number;
};
const ServerSideRenderedPage: NextPage<Data> = ({
isEnabled,
variant,
percent,
}) => (
<>
Flag status: {isEnabled ? "ENABLED" : "DISABLED"}
<br />
Variant: {variant.name}
<br />
How many feature toggles are enabled: {percent}%
</>
);
export const getServerSideProps: GetServerSideProps<Data> = async (ctx) => {
const sessionId =
ctx.req.cookies[UNLEASH_COOKIE_NAME] ||
`${Math.floor(Math.random() * 1_000_000_000)}`;
ctx.res.setHeader(
"set-cookie",
`${UNLEASH_COOKIE_NAME}=${sessionId}; path=/;`
);
const definitions = await getDefinitions();
const { toggles } = evaluateFlags(definitions, {
sessionId,
});
const flags = flagsClient(toggles);
return {
props: {
isEnabled: flags.isEnabled("example-flag"),
variant: flags.getVariant("example-flag"),
percent: definitions.features.length
? Math.round((toggles.length / definitions.features.length) * 100)
: 0,
},
};
};
export default ServerSideRenderedPage;