Skip to content

Commit 7c975ba

Browse files
Gus Hcursoragent
authored andcommitted
Route Pages preview hosts to workers.dev agent API.
Allow *.pages.dev origins in the agent Worker CORS so design chat works when the custom domain is blocked. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 72aea5a commit 7c975ba

3 files changed

Lines changed: 30 additions & 5 deletions

File tree

src/app/globals.css

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,10 +1065,10 @@
10651065
.element-chat-popup-thread {
10661066
display: flex;
10671067
flex-direction: column;
1068-
gap: 4px;
1068+
gap: 6px;
10691069
max-height: 160px;
10701070
overflow-y: auto;
1071-
padding: 2px 14px 0;
1071+
padding: 4px 14px 14px;
10721072
}
10731073

10741074
.element-chat-popup-line {
@@ -1084,10 +1084,12 @@
10841084

10851085
.element-chat-popup-line--assistant {
10861086
color: rgba(17, 17, 17, 0.55);
1087+
padding-bottom: 2px;
10871088
}
10881089

10891090
.element-chat-popup-line--status {
10901091
color: rgba(17, 17, 17, 0.35);
1092+
padding-bottom: 2px;
10911093
}
10921094

10931095
.element-chat-popup-line--error {

src/lib/agent-client.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,27 @@ import type { ElementContext } from "./element-context";
44
const SESSION_ID_KEY = "popped.dev:agent-session-id";
55
const AGENT_ID_KEY = "popped.dev:agent-id";
66

7-
const API_BASE =
8-
(typeof process !== "undefined" && process.env.NEXT_PUBLIC_AGENT_API_URL) || "";
7+
/** Pages hosts have no /api — call the Worker directly (works when popped.dev is blocked). */
8+
const PAGES_AGENT_API = "https://popped-dev-agent-api.parse-nip.workers.dev";
9+
10+
function isPagesDevHost(hostname: string): boolean {
11+
return hostname === "popped-dev.pages.dev" || hostname.endsWith("--popped-dev.pages.dev");
12+
}
13+
14+
function getApiBase(): string {
15+
const fromEnv =
16+
typeof process !== "undefined" ? process.env.NEXT_PUBLIC_AGENT_API_URL?.trim() : "";
17+
if (fromEnv) return fromEnv;
18+
19+
if (typeof window !== "undefined" && isPagesDevHost(window.location.hostname)) {
20+
return PAGES_AGENT_API;
21+
}
22+
23+
return "";
24+
}
925

1026
function apiUrl(path: string): string {
11-
return `${API_BASE}${path}`;
27+
return `${getApiBase()}${path}`;
1228
}
1329

1430
export function getContributorName(): string {

workers/agent-api/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ app.use(
3636
) {
3737
return origin;
3838
}
39+
// Cloudflare Pages default + branch preview URLs (no Worker on *.pages.dev).
40+
if (
41+
origin === "https://popped-dev.pages.dev" ||
42+
/^https:\/\/[^/]+--popped-dev\.pages\.dev$/.test(origin)
43+
) {
44+
return origin;
45+
}
3946
return c.env.CORS_ORIGIN || "https://popped.dev";
4047
},
4148
allowMethods: ["GET", "POST", "OPTIONS"],

0 commit comments

Comments
 (0)