UNPKG

expo-router

Version:

Expo Router is a file-based router for React Native and web applications.

85 lines 4.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getNavigationConfig = getNavigationConfig; exports.getLinkingConfig = getLinkingConfig; const native_1 = require("@react-navigation/native"); const expo_modules_core_1 = require("expo-modules-core"); const constants_1 = require("./constants"); const getReactNavigationConfig_1 = require("./getReactNavigationConfig"); const getRoutesRedirects_1 = require("./getRoutesRedirects"); const linking_1 = require("./link/linking"); function getNavigationConfig(routes, metaOnly = true) { return { screens: { [constants_1.INTERNAL_SLOT_NAME]: { path: '', ...(0, getReactNavigationConfig_1.getReactNavigationConfig)(routes, metaOnly), }, }, }; } function getLinkingConfig(routes, context, getRouteInfo, { metaOnly = true, serverUrl, redirects } = {}) { // Returning `undefined` / `null from `getInitialURL` are valid values, so we need to track if it's been called. let hasCachedInitialUrl = false; let initialUrl; const nativeLinkingKey = context .keys() .find((key) => key.match(/^\.\/\+native-intent\.[tj]sx?$/)); const nativeLinking = nativeLinkingKey ? context(nativeLinkingKey) : undefined; const config = getNavigationConfig(routes, metaOnly); return { prefixes: [], config, // A custom getInitialURL is used on native to ensure the app always starts at // the root path if it's launched from something other than a deep link. // This helps keep the native functionality working like the web functionality. // For example, if you had a root navigator where the first screen was `/settings` and the second was `/index` // then `/index` would be used on web and `/settings` would be used on native. getInitialURL() { // Expo Router calls `getInitialURL` twice, which may confuse the user if they provide a custom `getInitialURL`. // Therefor we memoize the result. if (!hasCachedInitialUrl) { if (expo_modules_core_1.Platform.OS === 'web') { initialUrl = serverUrl ?? (0, linking_1.getInitialURL)(); } else { initialUrl = serverUrl ?? (0, linking_1.getInitialURL)(); if (typeof initialUrl === 'string') { initialUrl = (0, getRoutesRedirects_1.applyRedirects)(initialUrl, redirects); if (initialUrl && typeof nativeLinking?.redirectSystemPath === 'function') { initialUrl = nativeLinking.redirectSystemPath({ path: initialUrl, initial: true }); } } else if (initialUrl) { initialUrl = initialUrl.then((url) => { url = (0, getRoutesRedirects_1.applyRedirects)(url, redirects); if (url && typeof nativeLinking?.redirectSystemPath === 'function') { return nativeLinking.redirectSystemPath({ path: url, initial: true }); } return url; }); } } hasCachedInitialUrl = true; } return initialUrl; }, subscribe: (0, linking_1.subscribe)(nativeLinking, redirects), getStateFromPath: (path, options) => { return (0, linking_1.getStateFromPath)(path, options, getRouteInfo().segments); }, getPathFromState(state, options) { return ((0, linking_1.getPathFromState)(state, { ...config, ...options, screens: config.screens ?? options?.screens ?? {}, }) ?? '/'); }, // Add all functions to ensure the types never need to fallback. // This is a convenience for usage in the package. getActionFromState: native_1.getActionFromState, }; } //# sourceMappingURL=getLinkingConfig.js.map