Skip to content

Commit 51ad111

Browse files
creazyfrogclaude
andcommitted
Switch Drive fetch to JSONP to bypass CORS restrictions
Apps Script redirects cause CORS proxies to timeout. JSONP via dynamic script tag injection bypasses CORS entirely and works reliably with Google Apps Script web app endpoints. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 14692ab commit 51ad111

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

src/hooks/useGoogleDriveFolder.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,31 @@ interface DriveFile {
3434
createdTime?: string
3535
}
3636

37+
function fetchViaJSONP(url: string): Promise<{ files: DriveFile[] }> {
38+
return new Promise((resolve, reject) => {
39+
const cbName = `_driveCallback_${Date.now()}`
40+
const script = document.createElement('script')
41+
;(window as Record<string, unknown>)[cbName] = (data: { files: DriveFile[] }) => {
42+
delete (window as Record<string, unknown>)[cbName]
43+
document.head.removeChild(script)
44+
resolve(data)
45+
}
46+
script.src = `${url}&callback=${cbName}`
47+
script.onerror = () => {
48+
delete (window as Record<string, unknown>)[cbName]
49+
document.head.removeChild(script)
50+
reject(new Error('JSONP failed'))
51+
}
52+
document.head.appendChild(script)
53+
})
54+
}
55+
3756
let cache: Project[] | null = null
3857

3958
export async function fetchDriveVideos(): Promise<Project[]> {
4059
if (cache !== null) return cache
4160

42-
// Route through CORS proxy since Apps Script doesn't send CORS headers
43-
const proxyUrl = `https://api.allorigins.win/get?url=${encodeURIComponent(APPS_SCRIPT_URL)}`
44-
const res = await fetch(proxyUrl)
45-
if (!res.ok) return []
46-
47-
const { contents } = await res.json()
48-
if (!contents) return []
49-
50-
const { files } = JSON.parse(contents)
61+
const { files } = await fetchViaJSONP(APPS_SCRIPT_URL)
5162
if (!Array.isArray(files)) return []
5263

5364
const projects: Project[] = files.flatMap((file: DriveFile) => {

0 commit comments

Comments
 (0)