-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: rewrite posthog requests to pass in full header context (#1431)
- Loading branch information
1 parent
9f6fc49
commit ff82d37
Showing
4 changed files
with
45 additions
and
43 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { NextResponse, type NextRequest } from "next/server"; | ||
import { rewritePosthog } from "./utils/rewritePosthog"; | ||
|
||
export function middleware(request: NextRequest): NextResponse { | ||
if (request.nextUrl.pathname.includes("/api/fern-docs/analytics/posthog")) { | ||
return rewritePosthog(request); | ||
} | ||
return NextResponse.next(); | ||
} | ||
|
||
export const config = { | ||
matcher: ["/api/fern-docs/analytics/posthog/:path*", "/:prefix*/api/fern-docs/analytics/posthog/:path*"], | ||
}; |
43 changes: 0 additions & 43 deletions
43
packages/ui/docs-bundle/src/pages/api/fern-docs/analytics/posthog/[...path].ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { withDefaultProtocol } from "@fern-ui/core-utils"; | ||
import { NextResponse, type NextRequest } from "next/server"; | ||
|
||
const DEFAULT_POSTHOG_INSTANCE = "https://us.i.posthog.com"; | ||
|
||
export function rewritePosthog(request: NextRequest): NextResponse { | ||
const url = request.nextUrl.clone(); | ||
|
||
const destination = new URL( | ||
withDefaultProtocol(request.headers.get("x-fern-posthog-host") ?? DEFAULT_POSTHOG_INSTANCE), | ||
); | ||
|
||
const [, intendedPathname = "/"] = request.nextUrl.pathname.split("/api/fern-docs/analytics/posthog"); | ||
|
||
const hostname = | ||
intendedPathname.startsWith("/static/") && destination.hostname.endsWith(".i.posthog.com") | ||
? destination.hostname.replace(".i.posthog.com", "-assets.i.posthog.com") // i.e. us.i.posthog.com -> us-assets.i.posthog.com | ||
: destination.hostname; | ||
|
||
const requestHeaders = new Headers(request.headers); | ||
requestHeaders.set("host", hostname); | ||
|
||
url.pathname = intendedPathname; | ||
url.protocol = "https"; | ||
url.hostname = hostname; | ||
url.port = "443"; | ||
|
||
return NextResponse.rewrite(url, { | ||
headers: requestHeaders, | ||
}); | ||
} |