Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,4 @@ target/

test/
.vercel
.env*.local
31 changes: 26 additions & 5 deletions app/api/session/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NextResponse } from "next/server";
import Browserbase from "@browserbasehq/sdk";
import { getAll } from "@vercel/edge-config";
import { NextResponse } from "next/server";

type BrowserbaseRegion =
| "us-west-2"
Expand Down Expand Up @@ -112,21 +113,41 @@ function getRegionFromTimezoneAbbr(timezoneAbbr?: string): BrowserbaseRegion {
}
}

interface EdgeConfig {
advancedStealth: boolean | undefined;
proxies: boolean | undefined;
}

async function createSession(timezone?: string) {
const bb = new Browserbase({
apiKey: process.env.BROWSERBASE_API_KEY!,
});

const config = await getAll<EdgeConfig>();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const config = await getAll<EdgeConfig>();
let config: EdgeConfig;
try {
config = await getAll<EdgeConfig>();
} catch (error) {
console.warn('Edge Config unavailable, using defaults:', error);
config = { advancedStealth: undefined, proxies: undefined };
}

The getAll<EdgeConfig>() call may throw an error when the Edge Config is not set up or accessible, but this error is not caught and will cause the entire session creation to fail.

View Details

Analysis

Unhandled Edge Config error breaks session creation API

What fails: createSession() in app/api/session/route.ts calls getAll<EdgeConfig>() without error handling, causing 500 errors when Edge Config is not configured

How to reproduce:

# Without EDGE_CONFIG environment variable set:
curl -X POST http://localhost:3000/api/session -H "Content-Type: application/json" -d '{"timezone":"PST"}'

Result: Returns 500 Internal Server Error with message "@vercel/edge-config: No connection string provided"

Expected: Should return 200 OK with session data, using default values (advancedStealth: false, proxies: true) when Edge Config is unavailable per Vercel Edge Config SDK docs


const { advancedStealth: advancedStealthConfig, proxies: proxiesConfig } =
config;

const advancedStealth: boolean = advancedStealthConfig ?? false;
const proxies: boolean = proxiesConfig ?? true;

// Build browserSettings conditionally
const browserSettings: Browserbase.Sessions.SessionCreateParams.BrowserSettings =
{
viewport: {
width: 2560,
height: 1440,
},
//@ts-expect-error - not present in the types, but valid
os: "windows",
blockAds: true,
advancedStealth: true,
advancedStealth,
// Only set os if advancedStealth is true
...(advancedStealth
? {
os: "mac",
}
: {
os: "linux",
}),
};

// Use timezone abbreviation to determine base region
Expand All @@ -140,7 +161,7 @@ async function createSession(timezone?: string) {

const session = await bb.sessions.create({
projectId: process.env.BROWSERBASE_PROJECT_ID!,
proxies: true,
proxies,
browserSettings,
keepAlive: true,
region: finalRegion,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@browserbasehq/sdk": "^2.6.0",
"@browserbasehq/stagehand": "file:./sdk/stagehand-ts",
"@vercel/analytics": "^1.4.1",
"@vercel/edge-config": "^1.4.0",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"framer-motion": "^11.0.3",
Expand Down
23 changes: 23 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.