UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

57 lines (56 loc) 2.13 kB
import { isWebClient } from "../constants.mjs"; import { evictOldest } from "../utils/evictOldest.mjs"; import { getLinkingConfig as createLinkingConfig } from "./getLinkingConfig.mjs"; let linkingConfig; let cachedBaseLinkingConfig; let cachedRouteNodeForLinking = null; const ssrStateCache = /* @__PURE__ */new Map(); function getLinking() { return linkingConfig; } function setLinking(_) { linkingConfig = _; } function resetLinking() { linkingConfig = void 0; } function ensureBaseLinkingConfig(routeNode) { if (routeNode && (routeNode !== cachedRouteNodeForLinking || !cachedBaseLinkingConfig)) { cachedBaseLinkingConfig = createLinkingConfig(routeNode); cachedRouteNodeForLinking = routeNode; } } function getSSRInitialState(routeNode, initialLocation) { if (!routeNode) return void 0; ensureBaseLinkingConfig(routeNode); if (!cachedBaseLinkingConfig) return void 0; const path = initialLocation.pathname + (initialLocation.search || ""); if (ssrStateCache.has(path)) return ssrStateCache.get(path); const state = cachedBaseLinkingConfig.getStateFromPath?.(path, cachedBaseLinkingConfig.config); evictOldest(ssrStateCache, 5e3, 1e3); ssrStateCache.set(path, state); return state; } function setupLinking(routeNode, initialLocation) { let initialState; if (routeNode) { ensureBaseLinkingConfig(routeNode); linkingConfig = { ...cachedBaseLinkingConfig }; if (initialLocation) { linkingConfig.getInitialURL = () => initialLocation.toString(); let path = initialLocation.pathname + (initialLocation.search || ""); if (isWebClient) { const historyState = window.history.state; if (historyState?.__tempLocation?.pathname && !historyState.__tempKey) { path = historyState.__tempLocation.pathname + (historyState.__tempLocation.search || ""); } } initialState = linkingConfig.getStateFromPath?.(path, linkingConfig.config); } } return initialState; } export { ensureBaseLinkingConfig, getLinking, getSSRInitialState, resetLinking, setLinking, setupLinking }; //# sourceMappingURL=linkingConfig.mjs.map