Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration with NextJS/SSR applications #13

Open
nicholas-c opened this issue Oct 7, 2022 · 0 comments
Open

Integration with NextJS/SSR applications #13

nicholas-c opened this issue Oct 7, 2022 · 0 comments

Comments

@nicholas-c
Copy link

nicholas-c commented Oct 7, 2022

We're currently in the process of integrating mParticle into a NextJS application, however when we include the @mparticle/web-braze-kit there is many occurrences of window is undefined problems which happen.

Although we initialise mParticle and braze-kit within a useEffect, the import itself just runs window functions on import (Which occurs Serverside, and window is undefined)

This leads our block of code to look something like this which is quite messy.

try {
    (async () => {
        /**
         * Dynamic import as the package uses `window.xxx` within it's internals, breaks SSR/ISR
         * Braze will hook into mParticle and call a `Fowarding` endpoint,
         * this then does a xmlhttprequest to Braze itself.
         */
        const brazeTracking = await import('@mparticle/web-braze-kit');
        brazeTracking.register(mParticleConfig);

        /**
         * Because the above is Dynamic import, and async, the kit isn't always registered when mParticle init's
         * it causes race conditions where sometimes it will init before, and other times it wont
         * but the .kits object the above register call does is required when mParticle init's so here we are 🥺
         */
        mParticle.init(process.env.NEXT_PUBLIC_MPARTICLE_API_KEY!, mParticleConfig);
    })();
} catch (e) {
    console.error('Failed to load Braze mParticle integration', e);

    // Just run mparticle even if the above fails
    mParticle.init(process.env.NEXT_PUBLIC_MPARTICLE_API_KEY!, mParticleConfig);
}

Is there any plans to rework the window calls so this block of code can be streamlined and reduced in risk/complexity?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant