Skip to content

Commit

Permalink
fix: do not rewrite all /api routes (#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity authored Jan 30, 2024
1 parent f863314 commit c0e6545
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
5 changes: 3 additions & 2 deletions packages/ui/app/src/analytics/posthog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ function safeAccessPosthog(run: () => void): void {
}
}

export function initializePosthog(apiKey: string): void {
if (process.env.NODE_ENV === "production") {
export function initializePosthog(): void {
const apiKey = process.env.NEXT_PUBLIC_POSTHOG_API_KEY?.trim();
if (process.env.NODE_ENV === "production" && apiKey != null && apiKey.length > 0 && !IS_POSTHOG_INITIALIZED) {
posthog.init(apiKey, {
api_host: "https://app.posthog.com",
loaded: () => {
Expand Down
4 changes: 1 addition & 3 deletions packages/ui/app/src/next-app/DocsApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ export const DocsApp: React.FC<App.Props> = ({
const config = useDeepCompareMemoize(unmemoizedConfig);

useEffect(() => {
if (process.env.NEXT_PUBLIC_POSTHOG_API_KEY != null && process.env.NEXT_PUBLIC_POSTHOG_API_KEY.length > 0) {
initializePosthog(process.env.NEXT_PUBLIC_POSTHOG_API_KEY);
}
initializePosthog();
}, []);

return (
Expand Down
24 changes: 24 additions & 0 deletions packages/ui/app/src/next-app/DocsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { compact } from "lodash-es";
import { GetStaticProps, Redirect } from "next";
import Head from "next/head";
import { ReactElement } from "react";
import { capturePosthogEvent, initializePosthog } from "../analytics/posthog";
import { useColorTheme } from "../hooks/useColorTheme";
import { REGISTRY_SERVICE } from "../services/registry";
import { buildUrl } from "../util/buildUrl";
Expand Down Expand Up @@ -88,6 +89,13 @@ export const getDocsPageProps = async (
xFernHost: string | undefined,
slugArray: string[]
): Promise<DocsPageResult<DocsPage.Props>> => {
try {
initializePosthog();
} catch (e) {
// eslint-disable-next-line no-console
console.error("Failed to initialize Posthog", e);
}

if (xFernHost == null || Array.isArray(xFernHost)) {
return { type: "notFound", notFound: true };
}
Expand All @@ -97,9 +105,20 @@ export const getDocsPageProps = async (
url: buildUrl({ host: xFernHost, pathname }),
});

capturePosthogEvent("static_generate_docs_page", {
host: xFernHost,
path: pathname,
ok: docs.ok,
});

if (!docs.ok) {
// eslint-disable-next-line no-console
console.error(`Failed to fetch docs for path: /${pathname}`, docs.error);
capturePosthogEvent("static_generate_docs_page_error", {
host: xFernHost,
path: pathname,
error: docs.error,
});
return {
type: "notFound",
notFound: true,
Expand All @@ -124,6 +143,11 @@ export const getDocsPageProps = async (
if (resolvedNavigatable == null) {
// eslint-disable-next-line no-console
console.error(`Cannot resolve navigatable corresponding to "${pathname}"`);
capturePosthogEvent("static_generate_docs_page_error", {
host: xFernHost,
path: pathname,
error: `Cannot resolve navigatable corresponding to "${pathname}"`,
});
return {
type: "notFound",
notFound: true,
Expand Down
8 changes: 4 additions & 4 deletions packages/ui/public-docs-bundle/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const nextConfig = {
},
],
afterFiles: [
{
source: "/api/:path*",
destination: "/api/:path*",
},
...["proxy", "revalidate-all", "revalidate-v2", "revalidate", "serialize-mdx", "sitemap"].map((prefix) => ({
source: `/api/${prefix}`,
destination: `/api/${prefix}`,
})),
{
has: [
{
Expand Down

0 comments on commit c0e6545

Please sign in to comment.