Skip to content

Commit

Permalink
feat: New <Feature> component to gate docs content by feature flag (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rpc333 authored Jan 8, 2025
1 parent d573935 commit 07631d9
Show file tree
Hide file tree
Showing 69 changed files with 637 additions and 468 deletions.
10 changes: 5 additions & 5 deletions packages/fern-docs/bundle/src/app/[[...slug]]/llms-full.txt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getDocsDomainEdge, getHostEdge } from "@/server/xfernhost/edge";
import { FernNavigation } from "@fern-api/fdr-sdk";
import { CONTINUE, SKIP } from "@fern-api/fdr-sdk/traversers";
import { isNonNullish } from "@fern-api/ui-core-utils";
import { getFeatureFlags } from "@fern-docs/edge-config";
import { getEdgeFlags } from "@fern-docs/edge-config";
import { addLeadingSlash, COOKIE_FERN_TOKEN } from "@fern-docs/utils";
import { uniqBy } from "es-toolkit/array";
import { cookies } from "next/headers";
Expand All @@ -20,9 +20,9 @@ export async function handleLLMSFullTxt(
const domain = getDocsDomainEdge(req);
const host = getHostEdge(req);
const fern_token = cookies().get(COOKIE_FERN_TOKEN)?.value;
const featureFlags = await getFeatureFlags(domain);
const loader = DocsLoader.for(domain, host, fern_token).withFeatureFlags(
featureFlags
const edgeFlags = await getEdgeFlags(domain);
const loader = DocsLoader.for(domain, host, fern_token).withEdgeFlags(
edgeFlags
);

const root = getSectionRoot(await loader.root(), path);
Expand Down Expand Up @@ -53,7 +53,7 @@ export async function handleLLMSFullTxt(
nodes,
(a) => FernNavigation.getPageId(a) ?? a.canonicalSlug ?? a.slug
).map(async (node) => {
const markdown = await getMarkdownForPath(node, loader, featureFlags);
const markdown = await getMarkdownForPath(node, loader, edgeFlags);
if (markdown == null) {
return undefined;
}
Expand Down
10 changes: 5 additions & 5 deletions packages/fern-docs/bundle/src/app/[[...slug]]/llms.txt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getDocsDomainEdge, getHostEdge } from "@/server/xfernhost/edge";
import * as FernNavigation from "@fern-api/fdr-sdk/navigation";
import { CONTINUE, SKIP } from "@fern-api/fdr-sdk/traversers";
import { isNonNullish, withDefaultProtocol } from "@fern-api/ui-core-utils";
import { getFeatureFlags } from "@fern-docs/edge-config";
import { getEdgeFlags } from "@fern-docs/edge-config";
import { COOKIE_FERN_TOKEN, addLeadingSlash } from "@fern-docs/utils";
import { cookies } from "next/headers";
import { NextRequest, NextResponse } from "next/server";
Expand Down Expand Up @@ -40,9 +40,9 @@ export async function handleLLMSTxt(
const domain = getDocsDomainEdge(req);
const host = getHostEdge(req);
const fern_token = cookies().get(COOKIE_FERN_TOKEN)?.value;
const featureFlags = await getFeatureFlags(domain);
const loader = DocsLoader.for(domain, host, fern_token).withFeatureFlags(
featureFlags
const edgeFlags = await getEdgeFlags(domain);
const loader = DocsLoader.for(domain, host, fern_token).withEdgeFlags(
edgeFlags
);

const root = getSectionRoot(await loader.root(), path);
Expand Down Expand Up @@ -71,7 +71,7 @@ export async function handleLLMSTxt(
const landingPage = getLandingPage(root);
const markdown =
landingPage != null
? await getMarkdownForPath(landingPage, loader, featureFlags)
? await getMarkdownForPath(landingPage, loader, edgeFlags)
: undefined;

// traverse the tree in a depth-first manner to collect all the nodes that have markdown content
Expand Down
10 changes: 5 additions & 5 deletions packages/fern-docs/bundle/src/app/[[...slug]]/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
getPageNodeForPath,
} from "@/server/getMarkdownForPath";
import { getDocsDomainEdge, getHostEdge } from "@/server/xfernhost/edge";
import { getFeatureFlags } from "@fern-docs/edge-config";
import { getEdgeFlags } from "@fern-docs/edge-config";
import { addLeadingSlash, COOKIE_FERN_TOKEN } from "@fern-docs/utils";
import { cookies } from "next/headers";
import { notFound } from "next/navigation";
Expand All @@ -23,9 +23,9 @@ export async function handleMarkdown(
const domain = getDocsDomainEdge(req);
const host = getHostEdge(req);
const fern_token = cookies().get(COOKIE_FERN_TOKEN)?.value;
const featureFlags = await getFeatureFlags(domain);
const loader = DocsLoader.for(domain, host, fern_token).withFeatureFlags(
featureFlags
const edgeFlags = await getEdgeFlags(domain);
const loader = DocsLoader.for(domain, host, fern_token).withEdgeFlags(
edgeFlags
);

const node = getPageNodeForPath(await loader.root(), path);
Expand All @@ -39,7 +39,7 @@ export async function handleMarkdown(
return new NextResponse(null, { status: 403 });
}

const markdown = await getMarkdownForPath(node, loader, featureFlags);
const markdown = await getMarkdownForPath(node, loader, edgeFlags);
if (markdown == null) {
notFound();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getAuthStateEdge } from "@/server/auth/getAuthStateEdge";
import * as ApiDefinition from "@fern-api/fdr-sdk/api-definition";
import { ApiDefinitionLoader } from "@fern-docs/cache";
import { getFeatureFlags } from "@fern-docs/edge-config";
import { getEdgeFlags } from "@fern-docs/edge-config";
import { getMdxBundler } from "@fern-docs/ui/bundlers";
import { NextRequest, NextResponse } from "next/server";

Expand All @@ -20,15 +20,15 @@ export async function GET(
);
}

const flags = await getFeatureFlags(authState.domain);
const flags = await getEdgeFlags(authState.domain);
const engine = flags.useMdxBundler ? "mdx-bundler" : "next-mdx-remote";
const serializeMdx = await getMdxBundler(engine);

const apiDefinition = await ApiDefinitionLoader.create(
authState.domain,
ApiDefinition.ApiDefinitionId(api)
)
.withFlags(flags)
.withEdgeFlags(flags)
.withMdxBundler(serializeMdx, engine)
.withPrune({
type: "endpoint",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getAuthStateEdge } from "@/server/auth/getAuthStateEdge";
import * as ApiDefinition from "@fern-api/fdr-sdk/api-definition";
import { ApiDefinitionLoader } from "@fern-docs/cache";
import { getFeatureFlags } from "@fern-docs/edge-config";
import { getEdgeFlags } from "@fern-docs/edge-config";
import { getMdxBundler } from "@fern-docs/ui/bundlers";
import { NextRequest, NextResponse } from "next/server";

Expand All @@ -20,15 +20,15 @@ export async function GET(
);
}

const flags = await getFeatureFlags(authState.domain);
const flags = await getEdgeFlags(authState.domain);
const engine = flags.useMdxBundler ? "mdx-bundler" : "next-mdx-remote";
const serializeMdx = await getMdxBundler(engine);

const apiDefinition = await ApiDefinitionLoader.create(
authState.domain,
ApiDefinition.ApiDefinitionId(api)
)
.withFlags(flags)
.withEdgeFlags(flags)
.withMdxBundler(serializeMdx, engine)
.withPrune({
type: "webhook",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getAuthStateEdge } from "@/server/auth/getAuthStateEdge";
import * as ApiDefinition from "@fern-api/fdr-sdk/api-definition";
import { ApiDefinitionLoader } from "@fern-docs/cache";
import { getFeatureFlags } from "@fern-docs/edge-config";
import { getEdgeFlags } from "@fern-docs/edge-config";
import { getMdxBundler } from "@fern-docs/ui/bundlers";
import { NextRequest, NextResponse } from "next/server";

Expand All @@ -20,15 +20,15 @@ export async function GET(
);
}

const flags = await getFeatureFlags(authState.domain);
const flags = await getEdgeFlags(authState.domain);
const engine = flags.useMdxBundler ? "mdx-bundler" : "next-mdx-remote";
const serializeMdx = await getMdxBundler(engine);

const apiDefinition = await ApiDefinitionLoader.create(
authState.domain,
ApiDefinition.ApiDefinitionId(api)
)
.withFlags(flags)
.withEdgeFlags(flags)
.withMdxBundler(serializeMdx, engine)
.withPrune({
type: "webSocket",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import { NextRequest, NextResponse, userAgent } from "next/server";

export const runtime = "edge";

interface LaunchDarklyInfo {
clientSideId: string;
interface LaunchDarklyContext {
kind: "multi";
user:
| { anonymous: true }
Expand All @@ -29,7 +28,7 @@ interface LaunchDarklyInfo {

export async function GET(
req: NextRequest
): Promise<NextResponse<LaunchDarklyInfo | undefined>> {
): Promise<NextResponse<LaunchDarklyContext | undefined>> {
const domain = getDocsDomainEdge(req);

const config = await safeGetLaunchDarklySettings(domain);
Expand All @@ -40,7 +39,6 @@ export async function GET(
}

return NextResponse.json({
clientSideId,
kind: "multi" as const,
user: await getUserContext(req),
device: await getDeviceContext(req),
Expand Down Expand Up @@ -72,7 +70,7 @@ async function hashString(

async function getUserContext(
req: NextRequest
): Promise<LaunchDarklyInfo["user"]> {
): Promise<LaunchDarklyContext["user"]> {
const jar = cookies();

const fernToken = jar.get(COOKIE_FERN_TOKEN)?.value;
Expand Down Expand Up @@ -102,7 +100,7 @@ async function getUserContext(

async function getDeviceContext(
req: NextRequest
): Promise<LaunchDarklyInfo["device"]> {
): Promise<LaunchDarklyContext["device"]> {
const agent = userAgent(req);

const hash = (await hashString(agent.ua)) ?? crypto.randomUUID();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getDocsDomainEdge } from "@/server/xfernhost/edge";
import { createAnthropic } from "@ai-sdk/anthropic";
// import { createAmazonBedrock } from "@ai-sdk/amazon-bedrock";
import { createOpenAI } from "@ai-sdk/openai";
import { getAuthEdgeConfig, getFeatureFlags } from "@fern-docs/edge-config";
import { getAuthEdgeConfig, getEdgeFlags } from "@fern-docs/edge-config";
import { createDefaultSystemPrompt } from "@fern-docs/search-server";
import {
queryTurbopuffer,
Expand Down Expand Up @@ -48,12 +48,12 @@ export async function POST(req: NextRequest) {
}

const start = Date.now();
const [authEdgeConfig, featureFlags] = await Promise.all([
const [authEdgeConfig, edgeFlags] = await Promise.all([
getAuthEdgeConfig(domain),
getFeatureFlags(domain),
getEdgeFlags(domain),
]);

if (!featureFlags.isAskAiEnabled) {
if (!edgeFlags.isAskAiEnabled) {
throw new Error(`Ask AI is not enabled for ${domain}`);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "@/server/env-variables";
import { Gate, withBasicTokenAnonymous } from "@/server/withRbac";
import { getDocsDomainEdge } from "@/server/xfernhost/edge";
import { getAuthEdgeConfig, getFeatureFlags } from "@fern-docs/edge-config";
import { getAuthEdgeConfig, getEdgeFlags } from "@fern-docs/edge-config";
import {
SEARCH_INDEX,
algoliaIndexSettingsTask,
Expand Down Expand Up @@ -40,9 +40,9 @@ export async function GET(req: NextRequest): Promise<NextResponse> {
}

const start = Date.now();
const [authEdgeConfig, featureFlags] = await Promise.all([
const [authEdgeConfig, edgeFlags] = await Promise.all([
getAuthEdgeConfig(domain),
getFeatureFlags(domain),
getEdgeFlags(domain),
]);

await algoliaIndexSettingsTask({
Expand Down Expand Up @@ -70,7 +70,7 @@ export async function GET(req: NextRequest): Promise<NextResponse> {
) === Gate.DENY
);
},
...featureFlags,
...edgeFlags,
});

const end = Date.now();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createOpenAI } from "@ai-sdk/openai";
import { getAuthEdgeConfig, getFeatureFlags } from "@fern-docs/edge-config";
import { getAuthEdgeConfig, getEdgeFlags } from "@fern-docs/edge-config";
import { turbopufferUpsertTask } from "@fern-docs/search-server/turbopuffer";
import { addLeadingSlash, withoutStaging } from "@fern-docs/utils";
import { embedMany } from "ai";
Expand Down Expand Up @@ -48,12 +48,12 @@ export async function GET(req: NextRequest): Promise<NextResponse> {
}

const start = Date.now();
const [authEdgeConfig, featureFlags] = await Promise.all([
const [authEdgeConfig, edgeFlags] = await Promise.all([
getAuthEdgeConfig(domain),
getFeatureFlags(domain),
getEdgeFlags(domain),
]);

if (!featureFlags.isAskAiEnabled) {
if (!edgeFlags.isAskAiEnabled) {
throw new Error(`AI Chat is not enabled for ${domain}`);
}

Expand All @@ -64,7 +64,7 @@ export async function GET(req: NextRequest): Promise<NextResponse> {
environment: fdrEnvironment(),
fernToken: fernToken(),
domain: withoutStaging(domain),
...featureFlags,
...edgeFlags,
},
vectorizer: async (chunks) => {
const embeddings = await embedMany({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { algoliaAppId, anthropicApiKey } from "@/server/env-variables";
import { getDocsDomainEdge } from "@/server/xfernhost/edge";
import { createAnthropic } from "@ai-sdk/anthropic";
import { searchClient } from "@algolia/client-search";
import { getFeatureFlags } from "@fern-docs/edge-config";
import { getEdgeFlags } from "@fern-docs/edge-config";
import { SuggestionsSchema } from "@fern-docs/search-server";
import {
SEARCH_INDEX,
Expand Down Expand Up @@ -32,10 +32,10 @@ export async function POST(req: NextRequest): Promise<Response> {

const start = Date.now();
const domain = getDocsDomainEdge(req);
const featureFlags = await getFeatureFlags(domain);
const edgeFlags = await getEdgeFlags(domain);
const cookieJar = cookies();

if (!featureFlags.isAskAiEnabled) {
if (!edgeFlags.isAskAiEnabled) {
throw new Error(`Ask AI is not enabled for ${domain}`);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { Revalidator } from "@/server/revalidator";
import { getDocsDomainNode, getHostNode } from "@/server/xfernhost/node";
import { NodeCollector } from "@fern-api/fdr-sdk/navigation";
import { getFeatureFlags } from "@fern-docs/edge-config";
import { getEdgeFlags } from "@fern-docs/edge-config";
import { withoutStaging } from "@fern-docs/utils";
import type { FernDocs } from "@fern-fern/fern-docs-sdk";
import { NextApiHandler, NextApiRequest, NextApiResponse } from "next";
Expand Down Expand Up @@ -50,8 +50,8 @@ const handler: NextApiHandler = async (
// never provide a token here because revalidation should only be done on public routes (for now)
const loader = DocsLoader.for(domain, host);

const flags = await getFeatureFlags(domain);
const root = await loader.withFeatureFlags(flags).root();
const flags = await getEdgeFlags(domain);
const root = await loader.withEdgeFlags(flags).root();

if (!root) {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { Revalidator } from "@/server/revalidator";
import { getDocsDomainNode, getHostNode } from "@/server/xfernhost/node";
import { NodeCollector } from "@fern-api/fdr-sdk/navigation";
import { getFeatureFlags } from "@fern-docs/edge-config";
import { getEdgeFlags } from "@fern-docs/edge-config";
import { withoutStaging } from "@fern-docs/utils";
import type { FernDocs } from "@fern-fern/fern-docs-sdk";
import { NextApiHandler, NextApiRequest, NextApiResponse } from "next";
Expand Down Expand Up @@ -53,8 +53,8 @@ const handler: NextApiHandler = async (

// never provide a token here because revalidation should only be done on public routes (for now)
const loader = DocsLoader.for(domain, host);
const flags = await getFeatureFlags(domain);
const root = await loader.withFeatureFlags(flags).root();
const flags = await getEdgeFlags(domain);
const root = await loader.withEdgeFlags(flags).root();

if (!root) {
/**
Expand Down
Loading

0 comments on commit 07631d9

Please sign in to comment.