Skip to content

Commit

Permalink
Test CORS fix with app-staging.buildwithfern.com (#1977)
Browse files Browse the repository at this point in the history
  • Loading branch information
dubwub authored Jan 9, 2025
1 parent 69ada54 commit c03ce4d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
8 changes: 8 additions & 0 deletions packages/fern-docs/bundle/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ const nextConfig = {
source: "/api/fern-docs/auth/:path*",
headers: AccessControlHeaders,
},
{
source: "/api/fern-docs/search/v2/:path*",
headers: AccessControlHeaders,
},
{
source: "/:prefix*/api/fern-docs/search/v2/:path*",
headers: AccessControlHeaders,
},
{
source: "/:prefix*/api/fern-docs/auth/:path*",
headers: AccessControlHeaders,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ import { z } from "zod";
export const maxDuration = 60;
export const revalidate = 0;

// handle CORS preflight requests
export async function OPTIONS(_: NextRequest) {
const response = new NextResponse();
response.headers.set("Access-Control-Allow-Origin", "*");
response.headers.set("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
response.headers.set("Access-Control-Allow-Headers", "Content-Type");
return response;
}

export async function POST(req: NextRequest) {
const bedrock = createAmazonBedrock({
region: "us-west-2",
Expand Down Expand Up @@ -138,7 +147,11 @@ export async function POST(req: NextRequest) {
},
});

return result.toDataStreamResponse();
const response = result.toDataStreamResponse();
response.headers.set("Access-Control-Allow-Origin", "*");
response.headers.set("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
response.headers.set("Access-Control-Allow-Headers", "Content-Type");
return response;
}

async function runQueryTurbopuffer(
Expand Down
11 changes: 7 additions & 4 deletions packages/fern-docs/ui/src/search/SearchV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ export function SearchV2(): ReactElement | false {
let chatEndpoint = useApiRoute("/api/fern-docs/search/v2/chat");
let suggestEndpoint = useApiRoute("/api/fern-docs/search/v2/suggest");

if (process.env.NODE_ENV === "production") {
chatEndpoint = `https://app.ferndocs.com/api/fern-docs/search/v2/chat`;
suggestEndpoint = `https://app.ferndocs.com/api/fern-docs/search/v2/suggest`;
// TODO: this is just for testing, this should be removed once it's confirmed that CORS is working
if (process.env.NODE_ENV === "development") {
chatEndpoint = `https://app-staging.buildwithfern.com/api/fern-docs/search/v2/chat`;
suggestEndpoint = `https://app-staging.buildwithfern.com/api/fern-docs/search/v2/suggest`;
}

const router = useRouter();
Expand Down Expand Up @@ -165,7 +166,9 @@ export function SearchV2(): ReactElement | false {
askAI={askAi}
setAskAI={setAskAi}
api={chatEndpoint}
headers={{ "X-Fern-Host": domain }}
headers={{
"X-Fern-Host": domain,
}}
suggestionsApi={suggestEndpoint}
initialInput={initialInput}
setInitialInput={setInitialInput}
Expand Down

0 comments on commit c03ce4d

Please sign in to comment.