PageSourceSearch

https://pressthink.org/j/rosen-archive/frontend/utils/pathResolver.js?v=3.8.36

js pressthink.org collected 2026-10-02 04:26:16 UTC 2,851 bytes, 68 lines download raw bytes

1// Three-way environment + path resolution for the archive frontend.
2// Hosts map to one of: 'local' (localhost / IPv4 or IPv6 loopback), 'github-pages'
3// (any *.github.io), or 'production' (PressThink and anything else).
4// Imported by App.js, archiveService.js, and Header.js. sw.js duplicates
5// the same logic inline because it's a classic service worker and can't
6// ES-import — keep the inline detection in sw.js in sync with this file.
7
8const BASE_PATH_MAP = {
9  local: '/frontend',
10  'github-pages': '/rosen-frontend',
11  production: '/j/rosen-archive',
12};
13
14const DATA_PATH_MAP = {
15  local: '/data',
16  'github-pages': '/rosen-frontend/data',
17  production: '/j/rosen-archive/data',
18};
19
20// Site root each environment serves from. Standalone surfaces (the dissertation
21// tools, the data tools) live under this root, e.g. `${root}/dissertation/faq/`.
22// Links to them must go through resolveSitePath() so they resolve in local
23// preview and GitHub Pages, not just production.
24const ROOT_PATH_MAP = {
25  local: '',
26  'github-pages': '/rosen-frontend',
27  production: '/j/rosen-archive',
28};
29
30// URL.hostname serializes an IPv6 literal with brackets in browsers, while
31// direct callers and some runtimes may supply the bare address. Accept both so
32// the preview server's documented IPv6 bind behaves like every other local
33// surface instead of accidentally requesting the production path prefix.
34const LOCAL_HOSTS = new Set(['localhost', '127.0.0.1', '::1', '[::1]']);
35
36const detectHost = () => {
37  // self.location works in both window and service-worker scopes since
38  // window extends self. Falls back to '' under node (test runtime).
39  if (typeof self !== 'undefined' && self.location && self.location.hostname) {
40    return self.location.hostname;
41  }
42  return '';
43};
44
45export const getEnvironment = (host = detectHost()) => {
46  if (LOCAL_HOSTS.has(host)) return 'local';
47  if (host.endsWith('.github.io')) return 'github-pages';
48  return 'production';
49};
50
51export const getBasePath = (host = detectHost()) => BASE_PATH_MAP[getEnvironment(host)];
52
53export const getDataPath = (host = detectHost()) => DATA_PATH_MAP[getEnvironment(host)];
54
55export const getSiteRoot = (host = detectHost()) => ROOT_PATH_MAP[getEnvironment(host)];
56
57// Resolve a site-root-relative path (e.g. 'dissertation/faq/') to an absolute
58// path for the current environment. Leading slashes on `rel` are ignored.
59export const resolveSitePath = (rel, host = detectHost()) =>
60  getSiteRoot(host) + '/' + String(rel).replace(/^\/+/, '');
61
62export const ENVIRONMENT = getEnvironment();
63export const BASE_PATH = getBasePath();
64export const DATA_PATH = getDataPath();
65export const SITE_ROOT = getSiteRoot();
66export const IS_LOCAL = ENVIRONMENT === 'local';
67export const IS_GITHUB_PAGES = ENVIRONMENT === 'github-pages';
68export const IS_PRODUCTION = ENVIRONMENT === 'production';

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.