Skip to content

Commit

Permalink
code split
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed Oct 7, 2024
1 parent 424cdfb commit 54bef9f
Show file tree
Hide file tree
Showing 47 changed files with 351 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const Breadcrumb = memo(
return (
<Head>
<script
key="jsonld-breadcrumb"
type="application/ld+json"
id="jsonld-breadcrumb"
dangerouslySetInnerHTML={{
Expand Down
3 changes: 1 addition & 2 deletions packages/commons/search-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
},
"dependencies": {
"@fern-api/fdr-sdk": "workspace:*",
"@fern-ui/core-utils": "workspace:*",
"@vercel/edge-config": "^1.1.0"
"@fern-ui/core-utils": "workspace:*"
},
"devDependencies": {
"@fern-platform/configs": "workspace:*",
Expand Down
29 changes: 7 additions & 22 deletions packages/commons/search-utils/src/SearchConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ import type {
InkeepSearchSettings,
InkeepWidgetBaseSettings,
} from "@inkeep/widgets";
import { getAll } from "@vercel/edge-config";
import type { DeepReadonly } from "ts-essentials";

const FEATURE_FLAGS = ["inkeep-enabled" as const];

export type InkeepSharedSettings = DeepReadonly<{
replaceSearch: boolean;
baseSettings: InkeepWidgetBaseSettings;
Expand All @@ -20,10 +17,6 @@ export type InkeepSharedSettings = DeepReadonly<{
modalSettings?: Partial<InkeepModalSettings>;
}>;

interface EdgeConfigResponse {
"inkeep-enabled"?: Record<string, InkeepSharedSettings>;
}

export declare namespace SearchConfig {
interface Unversioned {
type: "unversioned";
Expand Down Expand Up @@ -128,8 +121,8 @@ async function getAlgoliaSearchConfig(

export async function getSearchConfig(
client: FdrClient,
domain: string,
searchInfo: Algolia.SearchInfo,
inkeep: InkeepSharedSettings | undefined,
): Promise<SearchConfig> {
const algolia = await getAlgoliaSearchConfig(client, searchInfo);

Expand All @@ -138,20 +131,12 @@ export async function getSearchConfig(
return { isAvailable: false };
}

try {
const config = await getAll<EdgeConfigResponse>(FEATURE_FLAGS);
const maybeInkeep = config["inkeep-enabled"]?.[domain];

if (maybeInkeep?.baseSettings.integrationId != null) {
return {
isAvailable: true,
algolia,
inkeep: maybeInkeep,
};
}
} catch (e) {
// eslint-disable-next-line no-console
console.error("Error fetching edge config", e);
if (inkeep?.baseSettings.integrationId != null) {
return {
isAvailable: true,
algolia,
inkeep,
};
}

return { isAvailable: true, algolia };
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"exports": {
"./bundlers": "./src/mdx/bundlers/index.ts",
"./bundlers/next-mdx-remote": "./src/mdx/bundlers/next-mdx-remote.ts",
"./auth": "./src/auth/index.ts",
".": "./src/index.ts"
},
"sideEffects": [
Expand Down Expand Up @@ -121,6 +120,7 @@
"zod": "^3.23.8"
},
"devDependencies": {
"@fern-ui/fern-docs-auth": "workspace:*",
"@chromatic-com/storybook": "^1.3.5",
"@fern-platform/configs": "workspace:*",
"@mdx-js/esbuild": "^3.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/app/src/atoms/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { FernUser } from "@fern-ui/fern-docs-auth";
import { useAtomValue } from "jotai";
import { selectAtom } from "jotai/utils";
import { isEqual } from "lodash-es";
import type { FernUser } from "../auth";
import { DOCS_ATOM } from "./docs";

export const FERN_USER_ATOM = selectAtom(DOCS_ATOM, (docs) => docs.user, isEqual);
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/app/src/atoms/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { DocsV1Read, DocsV2Read, FdrAPI } from "@fern-api/fdr-sdk/client/ty
import type * as FernDocs from "@fern-api/fdr-sdk/docs";
import type * as FernNavigation from "@fern-api/fdr-sdk/navigation";
import { ColorsConfig, SidebarTab, VersionSwitcherInfo } from "@fern-ui/fdr-utils";
import type { FernUser } from "@fern-ui/fern-docs-auth";
import { NextSeoProps } from "@fern-ui/next-seo";
import { CustomerAnalytics } from "../analytics/types";
import { FernUser } from "../auth";
import { DocsContent } from "../resolver/DocsContent";
import { FernTheme } from "../themes/ThemedDocs";

Expand Down
22 changes: 22 additions & 0 deletions packages/ui/app/src/components/LinkPreload.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Head from "next/head";
import { ReactElement } from "react";
import { useIsLocalPreview } from "../contexts/local-preview";
import { useApiRoute } from "../hooks/useApiRoute";

export function LinkPreload({ href }: { href: string }): ReactElement {
return (
<Head>
<link key={href} rel="preload" href={href} as="fetch" crossOrigin="anonymous" />
</Head>
);
}

type FernDocsApiRoute = `/api/fern-docs/${string}`;
export function LinkPreloadApiRoute({ href }: { href: FernDocsApiRoute }): ReactElement | null {
const isLocalPreview = useIsLocalPreview();
const key = useApiRoute(href);
if (isLocalPreview) {
return null;
}
return <LinkPreload href={key} />;
}
3 changes: 3 additions & 0 deletions packages/ui/app/src/docs/DocsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ReactElement } from "react";
import { HydrateAtoms, useMessageHandler, useSetJustNavigated, type DocsProps } from "../atoms";
import { BgImageGradient } from "../components/BgImageGradient";
import { JavascriptProvider } from "../components/JavascriptProvider";
import { LinkPreloadApiRoute } from "../components/LinkPreload";
import { useBeforePopState } from "../hooks/useBeforePopState";
import { useConsoleMessage } from "../hooks/useConsoleMessage";
import { useRouteChangeComplete, useRouteChangeStart } from "../hooks/useRouteChanged";
Expand Down Expand Up @@ -45,6 +46,8 @@ export function DocsPage(pageProps: DocsProps): ReactElement | null {

return (
<>
<LinkPreloadApiRoute href="/api/fern-docs/search" />
<LinkPreloadApiRoute href="/api/fern-docs/auth/api-key-injection" />
<NextSeo />
<InitializeTheme />
<SearchDialog />
Expand Down
48 changes: 33 additions & 15 deletions packages/ui/app/src/mdx/components/launchdarkly/LaunchDarkly.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,53 @@
import { captureException } from "@sentry/nextjs";
import { atom, useAtomValue } from "jotai";
import { loadable } from "jotai/utils";
import { loadable, useHydrateAtoms } from "jotai/utils";
import * as LDClient from "launchdarkly-js-client-sdk";
import { PropsWithChildren, ReactNode, useCallback, useEffect, useState } from "react";
import { selectApiRoute } from "../../../hooks/useApiRoute";

async function fetchClientSideId(route: string): Promise<string | undefined> {
const json = await fetch(route).then((res) => res.json());
return json?.["client-side-id"];
import useSWR from "swr";
import { useApiRoute } from "../../../hooks/useApiRoute";

interface LaunchDarklyInfo {
clientSideId: string;
kind: "multi";
user:
| { anonymous: true }
| {
key: string;
email?: string;
name?: string;
};
device: {
key: string;
[key: string]: unknown;
};
}

const LAUNCH_DARKLY_INFO_ATOM = atom<LaunchDarklyInfo | undefined>(undefined);

// this is a singleton atom that initializes the LaunchDarkly client-side SDK
const ldClientAtom = atom<Promise<LDClient.LDClient | undefined>>(async (get) => {
const LD_CLIENT_ATOM = atom<Promise<LDClient.LDClient | undefined>>(async (get) => {
if (typeof window === "undefined") {
return undefined;
}
const route = selectApiRoute(get, "/api/fern-docs/integrations/launchdarkly");
const clientSideId = await fetchClientSideId(route);
if (!clientSideId) {

const info = get(LAUNCH_DARKLY_INFO_ATOM);
if (!info) {
return undefined;
}

const client = LDClient.initialize(clientSideId, {
kind: "user",
anonymous: true,
});
const { clientSideId, ...context } = info;

const client = LDClient.initialize(clientSideId, context);

await client.waitForInitialization();

return client;
});

const useLaunchDarklyFlag = (flag: string): boolean => {
const loadableClient = useAtomValue(loadable(ldClientAtom));
const loadableClient = useAtomValue(loadable(LD_CLIENT_ATOM));
const client = loadableClient.state === "hasData" ? loadableClient.data : undefined;

if (loadableClient.state === "hasError") {
captureException(loadableClient.error);
}
Expand Down Expand Up @@ -72,6 +86,10 @@ export interface LaunchDarklyProps {
}

export function LaunchDarkly({ flag, children }: PropsWithChildren<LaunchDarklyProps>): ReactNode {
const route = useApiRoute("/api/fern-docs/integrations/launchdarkly");
const { data } = useSWR(route, (key): Promise<LaunchDarklyInfo> => fetch(key).then((res) => res.json()));
useHydrateAtoms([[LAUNCH_DARKLY_INFO_ATOM, data]], { dangerouslyForceHydrate: true });

const ldClient = useLaunchDarklyFlag(flag);

if (!ldClient) {
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/app/src/services/useApiKeyInjectionConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { APIKeyInjectionConfig } from "@fern-ui/fern-docs-auth";
import useSWR from "swr";
import { APIKeyInjectionConfig } from "../auth";
import { useApiRoute } from "../hooks/useApiRoute";

const DEFAULT = { enabled: false as const };
Expand Down
3 changes: 3 additions & 0 deletions packages/ui/app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
{
"path": "../chatbot"
},
{
"path": "../fern-docs-auth"
},
{
"path": "../components"
}
Expand Down
3 changes: 2 additions & 1 deletion packages/ui/docs-bundle/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ const nextConfig = {
"@fern-ui/components",
"@fern-ui/core-utils",
"@fern-ui/fdr-utils",
"@fern-ui/fern-docs-utils",
"@fern-ui/fern-docs-auth",
"@fern-ui/fern-docs-edge",
"@fern-ui/fern-docs-utils",
"@fern-ui/loadable",
"@fern-ui/next-seo",
"@fern-ui/react-commons",
Expand Down
3 changes: 2 additions & 1 deletion packages/ui/docs-bundle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
"@fern-ui/components": "workspace:*",
"@fern-ui/core-utils": "workspace:*",
"@fern-ui/fdr-utils": "workspace:*",
"@fern-ui/fern-docs-utils": "workspace:*",
"@fern-ui/fern-docs-auth": "workspace:*",
"@fern-ui/fern-docs-edge": "workspace:*",
"@fern-ui/fern-docs-utils": "workspace:*",
"@fern-ui/search-utils": "workspace:*",
"@fern-ui/ui": "workspace:*",
"@sentry/nextjs": "^8.30.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/docs-bundle/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { extractBuildId, extractNextDataPathname } from "@/server/extractNextDat
import { getPageRoute, getPageRouteMatch, getPageRoutePath } from "@/server/pageRoutes";
import { rewritePosthog } from "@/server/rewritePosthog";
import { getXFernHostEdge } from "@/server/xfernhost/edge";
import type { FernUser } from "@fern-ui/fern-docs-auth";
import { getAuthEdgeConfig } from "@fern-ui/fern-docs-edge";
import { COOKIE_FERN_TOKEN, HEADER_X_FERN_HOST } from "@fern-ui/fern-docs-utils";
import type { FernUser } from "@fern-ui/ui/auth";
import { removeTrailingSlash } from "next/dist/shared/lib/router/utils/remove-trailing-slash";
import { NextRequest, NextResponse, type NextMiddleware } from "next/server";
import urlJoin from "url-join";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { OAuth2Client } from "@/server/auth/OAuth2Client";
import { getAPIKeyInjectionConfig } from "@/server/auth/getApiKeyInjectionConfig";
import { withSecureCookie } from "@/server/auth/withSecure";
import { getXFernHostEdge } from "@/server/xfernhost/edge";
import { APIKeyInjectionConfig, OryAccessTokenSchema } from "@fern-ui/fern-docs-auth";
import { getAuthEdgeConfig } from "@fern-ui/fern-docs-edge";
import { COOKIE_FERN_TOKEN } from "@fern-ui/fern-docs-utils";
import { APIKeyInjectionConfig, OryAccessTokenSchema } from "@fern-ui/ui/auth";
import { NextRequest, NextResponse } from "next/server";
import { WebflowClient } from "webflow-api";
import type { OauthScope } from "webflow-api/api/types/OAuthScope";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { signFernJWT } from "@/server/auth/FernJWT";
import { withSecureCookie } from "@/server/auth/withSecure";
import { getWorkOS, getWorkOSClientId } from "@/server/workos";
import { getXFernHostEdge } from "@/server/xfernhost/edge";
import { FernUser } from "@fern-ui/fern-docs-auth";
import { getAuthEdgeConfig } from "@fern-ui/fern-docs-edge";
import { COOKIE_FERN_TOKEN } from "@fern-ui/fern-docs-utils";
import { FernUser } from "@fern-ui/ui/auth";
import { NextRequest, NextResponse } from "next/server";

export const runtime = "edge";
Expand Down
Loading

0 comments on commit 54bef9f

Please sign in to comment.