• Resolved incoog

    (@incoog)


    Hi!

    I’m experiencing an issue with SiteGround Optimizer where it prevents an external JavaScript file from loading properly. The script in question is Klarna’s Web SDK, which is required for their On-Site Messaging to function.

    Issue Summary:

    • When “Combine JavaScript Files” is enabled, the following script does not load at all:
      <script async data-environment="production" src="https://js.klarna.com/web-sdk/v1/klarna.js" data-client-id="[REDACTED]"> </script>
    • I have tried excluding https://js.klarna.com/web-sdk/v1/klarna.js from SiteGround Optimizer’s “Exclude from JavaScript Combination” setting, but the script is still not loading.
    • The script is also not included in the combined JavaScript file, meaning it is being completely removed rather than merged.
    • As a result, Klarna’s On-Site Messaging does not work on my site.

    What I Have Tried:

    1. Manually excluding Klarna’s script from SiteGround Optimizer settings:
      • Added https://js.klarna.com/web-sdk/v1/klarna.js under Exclude from JavaScript Combination“ → Does not work.
      • Added https://js.klarna.com/web-sdk/v1/klarna.js under Exclude from JavaScript Minification“ → Does not work.
    2. Using SiteGround’s recommended PHP filter
      sgo_js_minify_excludeThis also does not work—the script is still missing or not loading or combined when JavaScript combination is enabled.
    3. Ensuring wp_enqueue_script() loads Klarna correctly and modifying script_loader_tag → Still does not work.

    Expected Outcome:

    I need Klarna’s script to always load separately, without being blocked, removed, or optimized by SiteGround Optimizer. Since the recommended exclusion methods have not worked, can you confirm how to properly exclude an external script like this?

    Thank you for your help!

    This is my code:

    function enqueue_klarna_onsitemessaging_js() {
    wp_enqueue_script(
    'klarna-sdk',
    'https://js.klarna.com/web-sdk/v1/klarna.js',
    array(),
    null,
    false
    );
    }
    add_action('wp_enqueue_scripts', 'enqueue_klarna_onsitemessaging_js');
    function add_klarna_onsitemessaging_js_tag($tag, $handle) {
    if ('klarna-sdk' === $handle) {
    return '<script async data-handle="klarna_onsitemessaging" data-environment="production" data-client-id="[REDACTED]"></script>';
    }
    return $tag;
    }
    add_filter('script_loader_tag', 'add_klarna_onsitemessaging_js_tag', 10, 2);

    add_filter( 'sgo_js_minify_exclude', 'js_minify_exclude' );
    function js_minify_exclude( $exclude_list ) {
    $exclude_list[] = 'klara_onsitemessaging';
    $exclude_list[] = 'klarna-sdk';
    return $exclude_list;
    }

    Best regards,
    Robin Alfredsson

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Plugin Support Svetoslav Vasev

    (@svetoslavvasev)

    Hello @incoog ,

    You can try excluding the external JS script from being combined by using the following filter:
    add_filter( 'sgo_javascript_combine_excluded_external_paths', 'js_combine_exclude_external_script' );
    function js_combine_exclude_external_script( $exclude_list ) {
    $exclude_list[] = 'script-host.com';
    $exclude_list[] = 'script-host-2.com';
    return $exclude_list;
    }

    In your case, the filter should look like this:
    add_filter( 'sgo_javascript_combine_excluded_external_paths', 'js_combine_exclude_external_script' );
    function js_combine_exclude_external_script( $exclude_list ) {
    $exclude_list[] = 'js.klarna.com';
    return $exclude_list;
    }

    In case this solution doesn’t yield the expected result, please submit a support inquiry via your SiteGround account and we’ll gladly review the matter further.

    Best Regards,

    Svetoslav Vasev

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.