PaddleHQ / paddle-js-wrapper

Wrapper to load Paddle.js as a module and use TypeScript definitions when working with methods.
Apache License 2.0
39 stars 6 forks source link

[Bug]: Paddle.js v2 also broken on pages with AMD #54

Open jakajancar opened 1 month ago

jakajancar commented 1 month ago

What happened?

I've reported this to your support team a while ago for Paddle.js v1:

The paddle.js script, when loaded asynchronously alongside other scripts that potentially define an AMD module loader, behaves unpredictably. If it happens to load first, it defines window.Paddle. However, if it loads last, then, because require.amd is defined, it assumes being loaded by an AMD require() (despite this not being the case) and does nothing [*].

(* Returns a value, but that is discarded. window.Paddle is not set.)

I have worked around the issue by doing something like:

    const url = 'https://cdn.paddle.com/paddle/paddle.js'
    if (window.define && window.define.amd) {
        console.log('[paddle] Loading Paddle.js using AMD')
        loading = new Promise(resolve => window.require([url], resolve))
    } else {
        console.log('[paddle] Loading Paddle.js without AMD')
        loading = getLibrary(url, 'Paddle') // does the document.createElement('script') thingie and returns window.Paddle
    }

Paddle.js v2 is still problematic and you either need to fix it there, or make a similar workaround in this wrapper/loader.

Steps to reproduce

See above.

What did you expect to happen?

No response

How are you integrating?

No response

Logs

No response

vijayasingam-paddle commented 1 month ago

Hi @jakajancar , Thank you for raising this issue with us.

I am sorry that you are running into this issue. I do not believe our library was built to load as an AMD package.

I will do some testing and come up with a fix for this problem.

Thank you.

jakajancar commented 1 month ago

Hey @vijayasingam-paddle,

you can see it at the beginning of https://cdn.paddle.com/paddle/v2/paddle.js:

!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):...

If define[.amd] is present, it calls it instead of setting window.Paddle.