| import type { FallbackRouteParams } from '../../server/request/fallback-params' |
| import type { Params } from '../request/params' |
| import { |
| createPrerenderResumeDataCache, |
| createRenderResumeDataCache, |
| type PrerenderResumeDataCache, |
| type RenderResumeDataCache, |
| } from '../resume-data-cache/resume-data-cache' |
| import { stringifyResumeDataCache } from '../resume-data-cache/resume-data-cache' |
|
|
| export enum DynamicState { |
| |
| |
| |
| DATA = 1, |
|
|
| |
| |
| |
| HTML = 2, |
| } |
|
|
| |
| |
| |
| export type DynamicDataPostponedState = { |
| |
| |
| |
| readonly type: DynamicState.DATA |
|
|
| |
| |
| |
| readonly renderResumeDataCache: RenderResumeDataCache |
| } |
|
|
| |
| |
| |
| export type DynamicHTMLPostponedState = { |
| |
| |
| |
| readonly type: DynamicState.HTML |
|
|
| |
| |
| |
| readonly data: object |
|
|
| |
| |
| |
| readonly renderResumeDataCache: RenderResumeDataCache |
| } |
|
|
| export type PostponedState = |
| | DynamicDataPostponedState |
| | DynamicHTMLPostponedState |
|
|
| export async function getDynamicHTMLPostponedState( |
| data: object, |
| fallbackRouteParams: FallbackRouteParams | null, |
| resumeDataCache: PrerenderResumeDataCache | RenderResumeDataCache |
| ): Promise<string> { |
| if (!fallbackRouteParams || fallbackRouteParams.size === 0) { |
| const postponedString = JSON.stringify(data) |
|
|
| |
| return `${postponedString.length}:${postponedString}${await stringifyResumeDataCache( |
| createRenderResumeDataCache(resumeDataCache) |
| )}` |
| } |
|
|
| const replacements: Array<[string, string]> = Array.from(fallbackRouteParams) |
| const replacementsString = JSON.stringify(replacements) |
| const dataString = JSON.stringify(data) |
|
|
| |
| const postponedString = `${replacementsString.length}${replacementsString}${dataString}` |
|
|
| |
| return `${postponedString.length}:${postponedString}${await stringifyResumeDataCache(resumeDataCache)}` |
| } |
|
|
| export async function getDynamicDataPostponedState( |
| resumeDataCache: PrerenderResumeDataCache | RenderResumeDataCache |
| ): Promise<string> { |
| return `4:null${await stringifyResumeDataCache(createRenderResumeDataCache(resumeDataCache))}` |
| } |
|
|
| export function parsePostponedState( |
| state: string, |
| params: Params | undefined |
| ): PostponedState { |
| try { |
| const postponedStringLengthMatch = state.match(/^([0-9]*):/)?.[1] |
| if (!postponedStringLengthMatch) { |
| throw new Error(`Invariant: invalid postponed state ${state}`) |
| } |
|
|
| const postponedStringLength = parseInt(postponedStringLengthMatch) |
|
|
| |
| |
| const postponedString = state.slice( |
| postponedStringLengthMatch.length + 1, |
| postponedStringLengthMatch.length + postponedStringLength + 1 |
| ) |
|
|
| const renderResumeDataCache = createRenderResumeDataCache( |
| state.slice(postponedStringLengthMatch.length + postponedStringLength + 1) |
| ) |
|
|
| try { |
| if (postponedString === 'null') { |
| return { type: DynamicState.DATA, renderResumeDataCache } |
| } |
|
|
| if (/^[0-9]/.test(postponedString)) { |
| const match = postponedString.match(/^([0-9]*)/)?.[1] |
| if (!match) { |
| throw new Error( |
| `Invariant: invalid postponed state ${JSON.stringify(postponedString)}` |
| ) |
| } |
|
|
| |
| const length = parseInt(match) |
| const replacements = JSON.parse( |
| postponedString.slice( |
| match.length, |
| |
| match.length + length |
| ) |
| ) as ReadonlyArray<[string, string]> |
|
|
| let postponed = postponedString.slice(match.length + length) |
| for (const [key, searchValue] of replacements) { |
| const value = params?.[key] ?? '' |
| const replaceValue = Array.isArray(value) ? value.join('/') : value |
| postponed = postponed.replaceAll(searchValue, replaceValue) |
| } |
|
|
| return { |
| type: DynamicState.HTML, |
| data: JSON.parse(postponed), |
| renderResumeDataCache, |
| } |
| } |
|
|
| return { |
| type: DynamicState.HTML, |
| data: JSON.parse(postponedString), |
| renderResumeDataCache, |
| } |
| } catch (err) { |
| console.error('Failed to parse postponed state', err) |
| return { type: DynamicState.DATA, renderResumeDataCache } |
| } |
| } catch (err) { |
| console.error('Failed to parse postponed state', err) |
| return { |
| type: DynamicState.DATA, |
| renderResumeDataCache: createPrerenderResumeDataCache(), |
| } |
| } |
| } |
|
|
| export function getPostponedFromState(state: PostponedState): any { |
| if (state.type === DynamicState.DATA) { |
| return null |
| } |
|
|
| return state.data |
| } |
|
|