-
-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathconstants.ts
More file actions
50 lines (40 loc) · 2.07 KB
/
constants.ts
File metadata and controls
50 lines (40 loc) · 2.07 KB
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
import { safeJsonStringify } from './utils/htmlEscape'
import type { One } from './vite/types'
export const isWebClient =
process.env.TAMAGUI_TARGET !== 'native' && typeof window !== 'undefined'
export const isWebServer =
process.env.TAMAGUI_TARGET !== 'native' && typeof window === 'undefined'
export const isNative = process.env.TAMAGUI_TARGET === 'native'
/**
* True only in a browser main-thread context with a navigable history —
* i.e. `window` AND `window.history` are available. Excludes native,
* SSR, web workers / service workers (no `window`), and exotic sandboxed
* environments where `window` exists but history is stripped. Use this
* for guarding any `window.history` / `window.location` access so
* intercept routes, URL masking, etc. don't assume a full browser.
*/
export const hasWebHistory =
isWebClient &&
typeof window.history !== 'undefined' &&
typeof window.location !== 'undefined'
export const CACHE_KEY = `${process.env.ONE_CACHE_KEY ?? Math.round(Math.random() * 100_000_000)}`
export const LOADER_JS_POSTFIX_UNCACHED = `_vxrn_loader.js`
export const LOADER_JS_POSTFIX_REGEX_STRING = `_\\d+${LOADER_JS_POSTFIX_UNCACHED}$`
export const LOADER_JS_POSTFIX_REGEX = new RegExp(LOADER_JS_POSTFIX_REGEX_STRING)
export const LOADER_JS_POSTFIX = `_${CACHE_KEY}${LOADER_JS_POSTFIX_UNCACHED}`
export const PRELOAD_JS_POSTFIX = `_${CACHE_KEY}_preload.js`
export const CSS_PRELOAD_JS_POSTFIX = `_${CACHE_KEY}_preload_css.js`
// safari insanely aggressive caching
export const VIRTUAL_SSR_CSS_ENTRY = `virtual:ssr-css.css`
export const VIRTUAL_SSR_CSS_HREF = `/@id/__x00__${VIRTUAL_SSR_CSS_ENTRY}`
export const SERVER_CONTEXT_KEY = '__one_server_context__'
export const getSpaHeaderElements = ({
serverContext = {},
}: {
serverContext?: One.ServerContext
} = {}) => `
<script>globalThis['global'] = globalThis</script>
<script>globalThis['__vxrnIsSPA'] = true</script>
<script>globalThis["${SERVER_CONTEXT_KEY}"] = ${safeJsonStringify(serverContext)}</script>
<script>globalThis.__oneLoadedCSS = new Set(${safeJsonStringify(serverContext.css || [])})</script>
`