-
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: next.config.js should use x-forwarded-host to resolve rewrites (#…
…911)
- Loading branch information
1 parent
30243dc
commit 0778362
Showing
10 changed files
with
130 additions
and
97 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
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
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
13 changes: 8 additions & 5 deletions
13
packages/ui/docs-bundle/src/pages/api/fern-docs/robots.txt.ts
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 |
---|---|---|
@@ -1,15 +1,18 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { getXFernHostEdge } from "../../../utils/xFernHost"; | ||
|
||
export const runtime = "edge"; | ||
|
||
export default async function GET(req: NextRequest): Promise<NextResponse> { | ||
const xFernHost = process.env.NEXT_PUBLIC_DOCS_DOMAIN ?? req.headers.get("x-fern-host") ?? req.nextUrl.host; | ||
const xFernHost = getXFernHostEdge(req); | ||
|
||
const hostname = new URL(`https://${xFernHost}`).hostname; // strip basepath | ||
|
||
if (hostname.includes(".docs.dev.buildwithfern.com") || hostname.includes(".docs.buildwithfern.com")) { | ||
if ( | ||
xFernHost.includes(".docs.buildwithfern.com") || | ||
xFernHost.includes(".docs.dev.buildwithfern.com") || | ||
xFernHost.includes(".docs.staging.buildwithfern.com") | ||
) { | ||
return new NextResponse("User-Agent: *\nDisallow: /", { status: 200 }); | ||
} | ||
|
||
return new NextResponse(`User-Agent: *\nSitemap: https://${hostname}/sitemap.xml`, { status: 200 }); | ||
return new NextResponse(`User-Agent: *\nSitemap: https://${xFernHost}/sitemap.xml`, { status: 200 }); | ||
} |
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
15 changes: 2 additions & 13 deletions
15
packages/ui/docs-bundle/src/pages/api/fern-docs/sitemap.xml.ts
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,91 @@ | ||
import type { NextApiRequest } from "next"; | ||
import type { NextRequest } from "next/server"; | ||
|
||
/** | ||
* Notes: | ||
* | ||
* x-fern-host is always appended to the request header by cloudfront for all *.docs.buildwithfern.com requests. | ||
* if the request is a rewrite from a custom domain, then x-forwarded-host is appended to the request header. | ||
* prefer x-forwarded-host over x-fern-host. | ||
* | ||
* NEXT_PUBLIC_DOCS_DOMAIN is used for local development only. | ||
* _fern_docs_preview is used for previewing the docs. | ||
*/ | ||
|
||
export function getXFernHostEdge(req: NextRequest, useSearchParams = false): string { | ||
const hosts = [ | ||
useSearchParams ? req.nextUrl.searchParams.get("host") : undefined, | ||
process.env.NEXT_PUBLIC_DOCS_DOMAIN, | ||
req.cookies.get("_fern_docs_preview")?.value, | ||
req.headers.get("x-forwarded-host"), | ||
req.headers.get("x-fern-host"), | ||
req.nextUrl.host, | ||
]; | ||
|
||
for (let host of hosts) { | ||
host = cleanHost(host); | ||
if (host != null) { | ||
return host; | ||
} | ||
} | ||
|
||
throw new Error("Could not determine xFernHost from request."); | ||
} | ||
|
||
export function getXFernHostNode(req: NextApiRequest, useSearchParams = false): string { | ||
const hosts = [ | ||
useSearchParams ? req.query["host"] : undefined, | ||
process.env.NEXT_PUBLIC_DOCS_DOMAIN, | ||
req.cookies["_fern_docs_preview"], | ||
req.headers["x-forwarded-host"], | ||
req.headers["x-fern-host"], | ||
req.headers.host, | ||
]; | ||
|
||
for (let host of hosts) { | ||
if (Array.isArray(host)) { | ||
continue; | ||
} | ||
|
||
host = cleanHost(host); | ||
if (host != null) { | ||
return host; | ||
} | ||
} | ||
|
||
throw new Error("Could not determine xFernHost from request."); | ||
} | ||
|
||
export function cleanHost(host: string | null | undefined): string | undefined { | ||
if (typeof host !== "string") { | ||
return undefined; | ||
} | ||
|
||
host = host.trim(); | ||
|
||
// host should not be localhost | ||
if (host.includes("localhost")) { | ||
return undefined; | ||
} | ||
|
||
// host should not be an ip address | ||
if (host.match(/\d+\.\d+\.\d+\.\d+/)) { | ||
return undefined; | ||
} | ||
|
||
// strip `http://` or `https://` from the host, if present | ||
if (host.includes("://")) { | ||
host = host.split("://")[1]; | ||
} | ||
|
||
// strip trailing slash from the host, if present | ||
if (host.endsWith("/")) { | ||
host = host.slice(0, -1); | ||
} | ||
|
||
if (host === "") { | ||
return undefined; | ||
} | ||
|
||
return host; | ||
} |