Commit 7ba11aa3 authored by Janka Uryga's avatar Janka Uryga

wip stuff idk

parent a1642c54
...@@ -264,6 +264,9 @@ export function createExhaustiveParamsProxy<TParams extends Params>( ...@@ -264,6 +264,9 @@ export function createExhaustiveParamsProxy<TParams extends Params>(
} }
return new Proxy(underlyingParams, { return new Proxy(underlyingParams, {
get(target, prop, receiver) { get(target, prop, receiver) {
if (prop === EXHAUSTIVE_PROXY_MARKER) {
return true
}
if ( if (
typeof prop === 'string' && typeof prop === 'string' &&
!wellKnownProperties.has(prop) && !wellKnownProperties.has(prop) &&
...@@ -287,6 +290,16 @@ export function createExhaustiveParamsProxy<TParams extends Params>( ...@@ -287,6 +290,16 @@ export function createExhaustiveParamsProxy<TParams extends Params>(
}) })
} }
const EXHAUSTIVE_PROXY_MARKER: unique symbol = Symbol.for(
'next.instant-validation.params-proxy'
)
export function isExhaustiveParamsProxy(params: Params): boolean {
return (
(params as Record<string | symbol, any>)[EXHAUSTIVE_PROXY_MARKER] === true
)
}
function isEmptyParams(params: Params) { function isEmptyParams(params: Params) {
for (const _param in params) { for (const _param in params) {
return false return false
......
import type { Params } from '../../request/params'
import { workUnitAsyncStorage } from '../work-unit-async-storage.external'
const PARAM_PLACEHOLDER = '-'
export function adaptParamsForImageMetadata(params: Params) {
const workUnitStore = workUnitAsyncStorage.getStore()
if (workUnitStore) {
switch (workUnitStore.type) {
case 'request': {
if (workUnitStore.validationSamples) {
const { isExhaustiveParamsProxy } =
require('./instant-samples') as typeof import('./instant-samples')
if (isExhaustiveParamsProxy(params)) {
return createPlaceholderParamsForInstantValidation(
params,
PARAM_PLACEHOLDER
)
}
}
// fallthrough
}
case 'cache':
case 'private-cache':
case 'unstable-cache':
case 'prerender-legacy':
case 'prerender-ppr':
case 'prerender-client':
case 'prerender':
case 'prerender-runtime':
case 'validation-client':
case 'generate-static-params': {
break
}
default: {
workUnitStore satisfies never
}
}
}
// If the params object is not a proxy
return params
}
function createPlaceholderParamsForInstantValidation(
params: Params,
placeholderValue: string
): Params {
const placeholderParams: Params = {}
for (const key in params) {
placeholderParams[key] = placeholderValue
}
return placeholderParams
}
// const METADATA_ID = '__metadata_id__'
// export function excludeMetadataIdFromServerParams<TParams extends Params>(
// params: TParams
// ): Omit<TParams, '__metadata_id__'> {
// const workUnitStore = workUnitAsyncStorage.getStore()
// if (workUnitStore) {
// switch (workUnitStore.type) {
// case 'request': {
// // In build-time instant validation, params can be wrapped in a proxy
// // that doesn't allow us to destructure the params to extract __metadata_id__.
// // (If all params are provided, we don't use a proxy, so we can skip creating one here as well)
// if (workUnitStore.validationSamples) {
// const { isExhaustiveParamsProxy } =
// require('./instant-samples') as typeof import('./instant-samples')
// if (isExhaustiveParamsProxy(params)) {
// return {} as any
// }
// }
// // fallthrough
// }
// case 'cache':
// case 'private-cache':
// case 'unstable-cache':
// case 'prerender-legacy':
// case 'prerender-ppr':
// case 'prerender-client':
// case 'prerender':
// case 'prerender-runtime':
// case 'validation-client':
// case 'generate-static-params': {
// break
// }
// default: {
// workUnitStore satisfies never
// }
// }
// }
// // If the params object is not a proxy
// const { [METADATA_ID]: _, ...cleanedParams } = params
// return cleanedParams
// }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment