Skip to content

Commit bb21ea3

Browse files
iamjr15claude
andcommitted
fix: preview proxy now handles CSS/JS with absolute paths
Added Referer header parsing to extract sandbox ID for static assets that use absolute paths like /_next/static/... without the sandbox prefix. This fixes broken CSS/styles in preview iframes. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 44b9b7a commit bb21ea3

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

preview-worker/src/index.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ function parseSandboxInfo(pathname: string): { portSandbox: string; remainingPat
3030
};
3131
}
3232

33+
/**
34+
* Extract sandbox info from Referer header
35+
* Used for static assets like CSS/JS that use absolute paths
36+
*/
37+
function parseSandboxFromReferer(referer: string | null): string | null {
38+
if (!referer) return null;
39+
try {
40+
const url = new URL(referer);
41+
const match = url.pathname.match(/^\/(\d+-[a-f0-9-]+)/i);
42+
return match ? match[1] : null;
43+
} catch {
44+
return null;
45+
}
46+
}
47+
3348
export default {
3449
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
3550
const url = new URL(request.url);
@@ -45,16 +60,30 @@ export default {
4560
}
4661

4762
// Parse the sandbox info from the path
48-
const info = parseSandboxInfo(url.pathname);
63+
let info = parseSandboxInfo(url.pathname);
64+
65+
// If path doesn't have sandbox prefix, try to get it from Referer header
66+
// This handles CSS/JS/images that use absolute paths like /_next/static/...
4967
if (!info) {
50-
return new Response(JSON.stringify({
51-
error: 'Invalid URL format',
52-
message: 'Expected format: /{port}-{sandboxId}/path',
53-
example: '/3000-abc123-def456-789/index.html'
54-
}), {
55-
status: 400,
56-
headers: { 'Content-Type': 'application/json' }
57-
});
68+
const referer = request.headers.get('referer');
69+
const sandboxFromReferer = parseSandboxFromReferer(referer);
70+
71+
if (sandboxFromReferer) {
72+
// Use the sandbox from referer and the full path as remaining path
73+
info = {
74+
portSandbox: sandboxFromReferer,
75+
remainingPath: url.pathname
76+
};
77+
} else {
78+
return new Response(JSON.stringify({
79+
error: 'Invalid URL format',
80+
message: 'Expected format: /{port}-{sandboxId}/path',
81+
example: '/3000-abc123-def456-789/index.html'
82+
}), {
83+
status: 400,
84+
headers: { 'Content-Type': 'application/json' }
85+
});
86+
}
5887
}
5988

6089
// Build the target URL

0 commit comments

Comments
 (0)