File size: 3,510 Bytes
984412a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Entry point of the "this site has moved" page (see index.html).
//
// The Reachy Mini website now lives at pollen-robotics.com. This Space no
// longer renders the old site: whatever route or hash a visitor arrives
// with, they get the single moved page from index.html, and this script
// points its button at the matching page of the new site.
//
// The former React site is gone from this repo. The Express API in server/
// is untouched: other clients (desktop app, pollen-robotics.com, ...) still
// call it on this Space's host.

const NEW_SITE_ORIGIN = 'https://pollen-robotics.com';

const HOME = { path: '/reachy-mini/', label: 'Go to the new site' };

// First path segment of the old route -> matching page on the new site.
// Anything not listed here goes to the new home page.
const PAGES = new Map([
  ['getting-started', { path: '/reachy-mini/getting-started/', label: 'Continue to Getting started' }],
  ['download', { path: '/reachy-mini/download/', label: 'Continue to Download' }],
  ['apps', { path: '/reachy-mini/apps/', label: 'Continue to Apps' }],
  // Shortcut that redirects to the store. The store refuses to be framed,
  // so it must only ever be opened as a top-level page.
  ['buy', { path: '/reachy-mini/buy/', label: 'Continue to the store' }],
]);

// "#/Download?scrollTo=release-notes", "#download", "/download/" -> "download"
function firstSegment(value) {
  const match = /^[#/]*([^/?#&]*)/.exec(value || '');
  return match ? match[1].toLowerCase() : '';
}

/**
 * Pick the page of the new site that matches where the visitor wanted to go.
 *
 * huggingface.co forwards the hash of the Space page URL to this frame
 * (huggingface.co/spaces/...#/getting-started), so the hash comes first.
 * The path covers direct links such as *.hf.space/download.
 */
function resolveDestination({ hash, pathname }) {
  const page = PAGES.get(firstSegment(hash)) || PAGES.get(firstSegment(pathname)) || HOME;
  const url = NEW_SITE_ORIGIN + page.path;
  return {
    url,
    label: page.label,
    // Readable form shown under the button: no scheme, no trailing slash.
    display: url.replace(/^https?:\/\//, '').replace(/\/$/, ''),
  };
}

function isFramed() {
  try {
    return window.self !== window.top;
  } catch {
    // Reading window.top can throw in some sandboxes: only a frame gets there.
    return true;
  }
}

function render() {
  const destination = resolveDestination(window.location);

  // On huggingface.co this page runs in a sandboxed iframe that is not
  // allowed to navigate the browser tab (no `allow-top-navigation`), so the
  // link has to open a new tab. Do not switch it to `_top`: Chrome silently
  // ignores such a link inside the Space frame.
  const link = document.getElementById('moved-link');
  if (link) link.href = destination.url;

  const label = document.getElementById('moved-label');
  if (label) label.textContent = destination.label;

  const display = document.getElementById('moved-destination');
  if (display) display.textContent = destination.display;

  return destination;
}

const destination = render();
window.addEventListener('hashchange', render);

// Opened directly (*.hf.space in its own tab): nothing stops us from
// navigating, so go straight to the new site. The page stays visible as
// the fallback. Add `?noredirect` to the URL to look at the page itself.
const stay = new URLSearchParams(window.location.search).has('noredirect');
if (!isFramed() && !stay) {
  window.location.replace(destination.url);
}