Files
Portfolio/src/entry-client.ts

48 lines
1.9 KiB
TypeScript

import { createApp } from './app';
import { validateAndFreezeProductionInputSnapshot } from './content/production-input-snapshot';
import type { ProductionInputs } from './content/types';
declare const __PORTFOLIO_TEST_BUILD__: boolean;
const TEST_BUILD = typeof __PORTFOLIO_TEST_BUILD__ !== 'undefined' && __PORTFOLIO_TEST_BUILD__;
const requestsTestFault = TEST_BUILD
&& new URL(location.href).searchParams.has('__portfolio_fault')
&& new URL(location.href).searchParams.has('__portfolio_test_token');
if (requestsTestFault) {
const { installAuthorizedTestHooks } = await import('./testing/fault-hooks');
await installAuthorizedTestHooks();
}
const requestsPerformanceTrace = TEST_BUILD
&& new URL(location.href).searchParams.get('__portfolio_performance') === 'drive-trace.v1'
&& new URL(location.href).searchParams.has('__portfolio_test_token');
if (requestsPerformanceTrace) {
const { installAuthorizedPerformanceInstrumentation } = await import('./testing/performance-instrumentation');
await installAuthorizedPerformanceInstrumentation();
}
export const readProductionInputsSnapshot = (source: Document): Readonly<ProductionInputs> => {
const snapshot = source.getElementById('portfolio-inputs');
if (
snapshot === null ||
snapshot.tagName !== 'SCRIPT' ||
snapshot.getAttribute('type') !== 'application/json'
) {
throw new Error('The inert portfolio input snapshot is unavailable.');
}
const serializedInputs = snapshot.textContent;
if (serializedInputs === null || serializedInputs.length === 0) {
throw new Error('The inert portfolio input snapshot is empty.');
}
return validateAndFreezeProductionInputSnapshot(JSON.parse(serializedInputs) as unknown);
};
const appRoot = document.getElementById('app');
const hydrationFault = TEST_BUILD
&& window.__PORTFOLIO_TEST_HOOKS__?.fault === 'F9-hydration-failure';
if (appRoot !== null && !hydrationFault) {
createApp(readProductionInputsSnapshot(document)).mount(appRoot);
}