Commit ·
aaa79ed
1
Parent(s): 047712c
Deep-link into the app through the Hugging Face Space page
Browse filesCo-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- frontend/src/App.tsx +2 -0
- frontend/src/lib/hfSpace.ts +47 -0
- frontend/src/main.tsx +3 -0
frontend/src/App.tsx
CHANGED
|
@@ -9,6 +9,7 @@ import { SequencePage } from './pages/SequencePage';
|
|
| 9 |
import { ChainPage } from './pages/ChainPage';
|
| 10 |
import { StatisticsPage } from './pages/StatisticsPage';
|
| 11 |
import { DocsPage } from './pages/DocsPage';
|
|
|
|
| 12 |
|
| 13 |
function ScrollToTop() {
|
| 14 |
const { pathname } = useLocation();
|
|
@@ -43,6 +44,7 @@ export default function App() {
|
|
| 43 |
<div className="app">
|
| 44 |
<DatasetJsonLd />
|
| 45 |
<ScrollToTop />
|
|
|
|
| 46 |
<Header />
|
| 47 |
<main id="main">
|
| 48 |
<Routes>
|
|
|
|
| 9 |
import { ChainPage } from './pages/ChainPage';
|
| 10 |
import { StatisticsPage } from './pages/StatisticsPage';
|
| 11 |
import { DocsPage } from './pages/DocsPage';
|
| 12 |
+
import { HfSpaceUrlSync } from './lib/hfSpace';
|
| 13 |
|
| 14 |
function ScrollToTop() {
|
| 15 |
const { pathname } = useLocation();
|
|
|
|
| 44 |
<div className="app">
|
| 45 |
<DatasetJsonLd />
|
| 46 |
<ScrollToTop />
|
| 47 |
+
<HfSpaceUrlSync />
|
| 48 |
<Header />
|
| 49 |
<main id="main">
|
| 50 |
<Routes>
|
frontend/src/lib/hfSpace.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Deep links through the Hugging Face Space page.
|
| 3 |
+
*
|
| 4 |
+
* huggingface.co/spaces/omaib/MuSProt shows this app in an iframe that always
|
| 5 |
+
* loads at "/", so its own paths (/chain/7ua3/I) never reach the address bar
|
| 6 |
+
* and cannot be shared. HF does pass the Space page's query string to the app
|
| 7 |
+
* on first load, and lets the app rewrite that query string with postMessage
|
| 8 |
+
* (https://huggingface.co/docs/hub/spaces-handle-url-parameters). So the page
|
| 9 |
+
* travels as ?p=…: huggingface.co/spaces/omaib/MuSProt?p=/chain/7ua3/I
|
| 10 |
+
*/
|
| 11 |
+
import { useEffect } from 'react';
|
| 12 |
+
import { useLocation } from 'react-router-dom';
|
| 13 |
+
|
| 14 |
+
const HF_ORIGIN = 'https://huggingface.co';
|
| 15 |
+
const PARAM = 'p';
|
| 16 |
+
|
| 17 |
+
/** Before the router starts: open the page named by ?p= instead of the home page. */
|
| 18 |
+
export function openDeepLink(): void {
|
| 19 |
+
const target = new URLSearchParams(window.location.search).get(PARAM);
|
| 20 |
+
if (window.location.pathname !== '/' || !target || !target.startsWith('/') || target.startsWith('//')) return;
|
| 21 |
+
window.history.replaceState(null, '', target + window.location.hash);
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
function embedded(): boolean {
|
| 25 |
+
try {
|
| 26 |
+
return window.parent !== window;
|
| 27 |
+
} catch {
|
| 28 |
+
return true;
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
/** Mirror the current page into the Space page's address bar while browsing inside it. */
|
| 33 |
+
export function HfSpaceUrlSync() {
|
| 34 |
+
const { pathname, search, hash } = useLocation();
|
| 35 |
+
useEffect(() => {
|
| 36 |
+
if (!embedded()) return;
|
| 37 |
+
const params = new URLSearchParams(search);
|
| 38 |
+
params.delete('__theme'); // added by HF to the iframe URL
|
| 39 |
+
const rest = params.toString();
|
| 40 |
+
const path = pathname + (rest ? `?${rest}` : '');
|
| 41 |
+
// keep slashes readable: ?p=/chain/7ua3/I
|
| 42 |
+
const queryString = path === '/' ? '' : `${PARAM}=${encodeURIComponent(path).replace(/%2F/g, '/')}`;
|
| 43 |
+
// delivered only when the parent really is huggingface.co; dropped otherwise
|
| 44 |
+
window.parent.postMessage({ queryString, hash }, HF_ORIGIN);
|
| 45 |
+
}, [pathname, search, hash]);
|
| 46 |
+
return null;
|
| 47 |
+
}
|
frontend/src/main.tsx
CHANGED
|
@@ -6,6 +6,9 @@ import './styles/tokens.css';
|
|
| 6 |
import './styles/base.css';
|
| 7 |
import './styles/ui.css';
|
| 8 |
import './styles/pages.css';
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
createRoot(document.getElementById('root')!).render(
|
| 11 |
<HelmetProvider>
|
|
|
|
| 6 |
import './styles/base.css';
|
| 7 |
import './styles/ui.css';
|
| 8 |
import './styles/pages.css';
|
| 9 |
+
import { openDeepLink } from './lib/hfSpace';
|
| 10 |
+
|
| 11 |
+
openDeepLink();
|
| 12 |
|
| 13 |
createRoot(document.getElementById('root')!).render(
|
| 14 |
<HelmetProvider>
|