summaryrefslogtreecommitdiff
path: root/shared/apps-common/src/jet/prefetched-intents/get-prefetched-intents.ts
blob: 4d591860a31fa7483dbaa849e278a8ac0bec2224 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { getCookie } from '@amp/web-app-components/src/utils/cookie';
import type { LoggerFactory } from '@amp/web-apps-logger';
import { isSome } from '@amp/web-apps-utils';
import { deserializeServerData, stableStringify } from './server-data';
import { type PrefetchedIntent, isPrefetchedIntents } from './types';

export function getPrefetchedIntents(
    loggerFactory: LoggerFactory,
    options?: { evenIfSignedIn?: boolean; featureKitItfe?: string },
): Map<string, unknown> {
    const logger = loggerFactory.loggerFor('getPrefetchedIntents');
    const evenIfSignedIn = options?.evenIfSignedIn;
    const itfe = options?.featureKitItfe;

    const data = deserializeServerData();
    if (!data || !isPrefetchedIntents(data)) {
        return new Map();
    }

    // We avoid prefetched intents in two scenarios:
    //
    // Condition 1: User is signed in (and evenIfSignedIn is false)
    // It's possible/likely that dispatching an intent when signed in behaves
    // differently.
    //
    // Condition 2: ITFE is enabled in Feature Kit
    // When ITFE is active, we discard prefetched intents so that media API
    // calls are triggered in the browser, allowing Feature Kit to inject ITFE
    // into those calls.
    if ((!evenIfSignedIn && getCookie('media-user-token')) || itfe) {
        logger.info(
            'Discarding prefetched intents - signed in user or ITFE enabled',
        );
        return new Map();
    }

    logger.debug('received prefetched intents from the server:', data);
    return new Map(
        data
            .map(
                ({
                    intent,
                    data,
                }: PrefetchedIntent): [string, unknown] | null => {
                    try {
                        if (intent.$kind.includes('Library')) {
                            return null;
                        }
                        // NOTE: PrefetchedIntents.get depends on stableStringify
                        return [stableStringify(intent), data];
                    } catch (e) {
                        return null;
                    }
                },
            )
            .filter(isSome),
    );
}