addons/isl/src/urlParams.tsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import type {InitialParamKeys} from './platform';
b69ab319
b69ab3110import {logger} from './logger';
b69ab3111
b69ab3112const INITIAL_PARAMS_LOCAL_STORAGE_KEY = 'ISLInitialParams';
b69ab3113
b69ab3114declare global {
b69ab3115 interface Window {
b69ab3116 relativeDateNowOverride?: number;
b69ab3117 }
b69ab3118}
b69ab3119
b69ab3120/**
b69ab3121 * Extract parameters from URL, then remove from URL to be cleaner (and hide sensitive tokens)
b69ab3122 */
b69ab3123export function computeInitialParams(isBrowserPlatform: boolean): Map<InitialParamKeys, string> {
b69ab3124 let initialParams: Map<InitialParamKeys, string> | undefined;
b69ab3125 if (typeof window === 'undefined') {
b69ab3126 return new Map();
b69ab3127 }
b69ab3128 if (window.location.search) {
b69ab3129 initialParams = new Map([...new URLSearchParams(window.location.search).entries()]);
b69ab3130 logger.log('Loaded initial params from URL: ', initialParams);
b69ab3131 if (isBrowserPlatform) {
b69ab3132 // Save params to local storage so reloading the page keeps the same URL parameters
b69ab3133 // Note: this assumes if search parameters are provided, ALL relevant search parameters are provided at the same time.
b69ab3134 // This way initial parameters stored in local storage is always consistent.
b69ab3135 try {
b69ab3136 localStorage.setItem(
b69ab3137 INITIAL_PARAMS_LOCAL_STORAGE_KEY,
b69ab3138 JSON.stringify([...initialParams.entries()].filter(([k]) => k !== 'sessionId')),
b69ab3139 );
b69ab3140 } catch (error) {
b69ab3141 logger.log('Failed to save initial params to local storage', error);
b69ab3142 }
b69ab3143 window.history.replaceState({}, document.title, window.location.pathname);
b69ab3144 logger.log('Saved initial params to local storage');
b69ab3145 }
b69ab3146 }
b69ab3147 // if parameters not passed in the URL, load previously seen values from localStorage.
b69ab3148 if (!initialParams) {
b69ab3149 try {
b69ab3150 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
b69ab3151 initialParams = new Map(JSON.parse(localStorage.getItem(INITIAL_PARAMS_LOCAL_STORAGE_KEY)!));
b69ab3152 logger.log('Loaded initial params from local storage: ', initialParams);
b69ab3153 } catch (error) {
b69ab3154 logger.log('Failed to load initial params from local storage', error);
b69ab3155 }
b69ab3156 }
b69ab3157
b69ab3158 // relative date's "now" override is stored separate in window for easier access
b69ab3159 const nowOverride = initialParams?.get('now');
b69ab3160 if (nowOverride) {
b69ab3161 try {
b69ab3162 window.relativeDateNowOverride = parseInt(nowOverride);
b69ab3163 } catch (error) {
b69ab3164 logger.error('relative date "now" override in the wrong format', error);
b69ab3165 }
b69ab3166 }
b69ab3167
b69ab3168 return initialParams ?? new Map();
b69ab3169}