this repo has no description
1// This must be imported first to ensure base styles are imported first
2import '~/styles/app-store.scss';
3
4import App from '~/App.svelte';
5import { bootstrap } from '~/bootstrap';
6import { registerActionHandlers } from '~/jet/action-handlers';
7import { PrefetchedIntents } from '@amp/web-apps-common/src/jet/prefetched-intents';
8import {
9 CompositeLoggerFactory,
10 ConsoleLoggerFactory,
11 DeferredLoggerFactory,
12 setContext,
13} from '@amp/web-apps-logger';
14
15import { setHTMLAttributes } from '@amp/web-apps-localization';
16import { ERROR_KIT_CONFIG } from '~/config/errorkit';
17import {
18 ErrorKitLoggerFactory,
19 setupErrorKit,
20} from '@amp/web-apps-logger/src/errorkit';
21import { setupRuntimeFeatures } from '~/utils/features/runtime';
22
23export async function startApplication() {
24 const onyxFeatures = await setupRuntimeFeatures(
25 new DeferredLoggerFactory(() => logger),
26 );
27 const consoleLogger = new ConsoleLoggerFactory();
28 const errorKit = setupErrorKit(ERROR_KIT_CONFIG, consoleLogger);
29 const logger = new CompositeLoggerFactory([
30 consoleLogger,
31 new ErrorKitLoggerFactory(errorKit),
32 ...(onyxFeatures ? [onyxFeatures.recordingLogger] : []),
33 ]);
34
35 let url = window.location.href;
36
37 // TODO: this is busted for some reason? rdar://111465791 ([Onyx] Foundation - PerfKit)
38 // const perfkit = setupBrowserPerfkit(PERF_KIT_CONFIG, logger);
39
40 // Initialize Jet, and get starting state.
41 const { context, jet, initialAction, storefront, language, i18n } =
42 await bootstrap({
43 loggerFactory: logger,
44 initialUrl: url,
45 fetch: window.fetch.bind(window),
46 prefetchedIntents: PrefetchedIntents.fromDom(logger, {
47 evenIfSignedIn: true,
48 featureKitItfe: onyxFeatures?.featureKit?.itfe,
49 }),
50 featuresCallbacks: {
51 getITFEValues(): string | undefined {
52 return onyxFeatures?.featureKit?.itfe;
53 },
54 },
55 });
56
57 // TODO: fix perfkit - rdar://111465791 ([Onyx] Foundation - PerfKit)
58 // setPageSpeedContext(context, perfkit, logger);
59 setContext(context, logger);
60
61 // Add lang + dir tag to HTML node
62 setHTMLAttributes(language);
63
64 // Using a container element to avoid svelte hydration
65 // "clean up" from removing tags that have
66 // been add to the <body> tag in our HTML file.
67 const container = document.querySelector('.body-container');
68
69 const app = new App({
70 target: container,
71 context,
72 hydrate: true,
73 });
74
75 // Initialize action-handlers.
76 registerActionHandlers({
77 jet,
78 logger,
79 updateApp: (props) => app.$set(props),
80 });
81
82 if (initialAction) {
83 // TODO: rdar://73165545 (Error Handling Across App): handle throw
84 await jet.perform(initialAction);
85 } else {
86 app.$set({
87 page: Promise.reject(new Error('404')),
88 isFirstPage: true,
89 });
90 }
91}
92
93// If we export default here, this will run during tests when we do
94// `import { startApplication } from '~/browser';`. To avoid this, we guard using the
95// presence of an ENV var only set by Vitest.
96
97// This is covered by acceptance tests
98if (!import.meta.env?.VITEST) {
99 startApplication();
100}