UNPKG

next

Version:

The React Framework

111 lines (110 loc) 5.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "createRenderSearchParamsFromClient", { enumerable: true, get: function() { return createRenderSearchParamsFromClient; } }); const _reflect = require("../web/spec-extension/adapters/reflect"); const _utils = require("./utils"); function createRenderSearchParamsFromClient(underlyingSearchParams) { if (process.env.NODE_ENV === 'development') { return makeUntrackedExoticSearchParamsWithDevWarnings(underlyingSearchParams); } else { return makeUntrackedExoticSearchParams(underlyingSearchParams); } } const CachedSearchParams = new WeakMap(); function makeUntrackedExoticSearchParamsWithDevWarnings(underlyingSearchParams) { const cachedSearchParams = CachedSearchParams.get(underlyingSearchParams); if (cachedSearchParams) { return cachedSearchParams; } const proxiedProperties = new Set(); const unproxiedProperties = []; const promise = Promise.resolve(underlyingSearchParams); Object.keys(underlyingSearchParams).forEach((prop)=>{ if (_utils.wellKnownProperties.has(prop)) { // These properties cannot be shadowed because they need to be the // true underlying value for Promises to work correctly at runtime unproxiedProperties.push(prop); } else { proxiedProperties.add(prop); promise[prop] = underlyingSearchParams[prop]; } }); const proxiedPromise = new Proxy(promise, { get (target, prop, receiver) { if (typeof prop === 'string') { if (!_utils.wellKnownProperties.has(prop) && (proxiedProperties.has(prop) || // We are accessing a property that doesn't exist on the promise nor // the underlying searchParams. Reflect.has(target, prop) === false)) { const expression = (0, _utils.describeStringPropertyAccess)('searchParams', prop); warnForSyncAccess(expression); } } return _reflect.ReflectAdapter.get(target, prop, receiver); }, set (target, prop, value, receiver) { if (typeof prop === 'string') { proxiedProperties.delete(prop); } return Reflect.set(target, prop, value, receiver); }, has (target, prop) { if (typeof prop === 'string') { if (!_utils.wellKnownProperties.has(prop) && (proxiedProperties.has(prop) || // We are accessing a property that doesn't exist on the promise nor // the underlying searchParams. Reflect.has(target, prop) === false)) { const expression = (0, _utils.describeHasCheckingStringProperty)('searchParams', prop); warnForSyncAccess(expression); } } return Reflect.has(target, prop); }, ownKeys (target) { warnForSyncSpread(); return Reflect.ownKeys(target); } }); CachedSearchParams.set(underlyingSearchParams, proxiedPromise); return proxiedPromise; } function makeUntrackedExoticSearchParams(underlyingSearchParams) { const cachedSearchParams = CachedSearchParams.get(underlyingSearchParams); if (cachedSearchParams) { return cachedSearchParams; } // We don't use makeResolvedReactPromise here because searchParams // supports copying with spread and we don't want to unnecessarily // instrument the promise with spreadable properties of ReactPromise. const promise = Promise.resolve(underlyingSearchParams); CachedSearchParams.set(underlyingSearchParams, promise); Object.keys(underlyingSearchParams).forEach((prop)=>{ if (_utils.wellKnownProperties.has(prop)) { // These properties cannot be shadowed because they need to be the // true underlying value for Promises to work correctly at runtime } else { ; promise[prop] = underlyingSearchParams[prop]; } }); return promise; } const noop = ()=>{}; const warnForSyncAccess = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS ? noop : function warnForSyncAccess(expression) { if (process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS) { return; } console.error(`A searchParam property was accessed directly with ${expression}. ` + `\`searchParams\` should be unwrapped with \`React.use()\` before accessing its properties. ` + `Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`); }; const warnForSyncSpread = process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS ? noop : function warnForSyncSpread() { if (process.env.__NEXT_DISABLE_SYNC_DYNAMIC_API_WARNINGS) { return; } console.error(`The keys of \`searchParams\` were accessed directly. ` + `\`searchParams\` should be unwrapped with \`React.use()\` before accessing its properties. ` + `Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis`); }; //# sourceMappingURL=search-params.browser.js.map