Avoid credentialed Gradio fetches from custom domain

#42
Files changed (1) hide show
  1. app.py +32 -1
app.py CHANGED
@@ -77,6 +77,29 @@ redirect_script = """
77
  if (window.location.pathname === '/') { window.location.replace('/home'); }
78
  </script>
79
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
  # JavaScript to fix navigation links to use relative paths (avoids domain mismatch when behind proxy)
82
  fix_nav_links_script = """
@@ -353,7 +376,15 @@ logger.info("Creating Gradio application")
353
  demo = gr.Blocks(
354
  theme=theme,
355
  css=final_css,
356
- head=posthog_script + scroll_script + redirect_script + fix_nav_links_script + tooltip_script + dark_mode_script,
 
 
 
 
 
 
 
 
357
  title="OpenHands Index",
358
  )
359
 
 
77
  if (window.location.pathname === '/') { window.location.replace('/home'); }
78
  </script>
79
  """
80
+ hf_space_fetch_credentials_script = """
81
+ <script>
82
+ (function() {
83
+ const hfSpaceApiPrefix = "https://openhands-openhands-index.hf.space/gradio_api/";
84
+ const originalFetch = window.fetch.bind(window);
85
+
86
+ window.fetch = function(input, init) {
87
+ const url = typeof input === "string" ? input : input?.url;
88
+
89
+ if (url && url.startsWith(hfSpaceApiPrefix)) {
90
+ if (input instanceof Request) {
91
+ input = new Request(input, { ...(init || {}), credentials: "omit" });
92
+ init = undefined;
93
+ } else {
94
+ init = { ...(init || {}), credentials: "omit" };
95
+ }
96
+ }
97
+
98
+ return originalFetch(input, init);
99
+ };
100
+ })();
101
+ </script>
102
+ """
103
 
104
  # JavaScript to fix navigation links to use relative paths (avoids domain mismatch when behind proxy)
105
  fix_nav_links_script = """
 
376
  demo = gr.Blocks(
377
  theme=theme,
378
  css=final_css,
379
+ head=(
380
+ hf_space_fetch_credentials_script
381
+ + posthog_script
382
+ + scroll_script
383
+ + redirect_script
384
+ + fix_nav_links_script
385
+ + tooltip_script
386
+ + dark_mode_script
387
+ ),
388
  title="OpenHands Index",
389
  )
390