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

Using "Loader Script" create a lot eventos of TypeError: null is not an object (evaluating 'co.document.body.appendChild') #13707

Closed
3 tasks done
jwv opened this issue Sep 17, 2024 · 5 comments · Fixed by #13717
Labels
Package: browser Issues related to the Sentry Browser SDK

Comments

@jwv
Copy link

jwv commented Sep 17, 2024

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/browser

SDK Version

8.30.0

Framework Version

No response

Link to Sentry event

https://fptic.sentry.io/issues/5739481656/?project=4504650871209984
https://fptic.sentry.io/issues/5738977641/?project=4504650871209984
https://fptic.sentry.io/issues/5739065410/?project=4504650871209984
https://fptic.sentry.io/issues/5739243469/?project=4504650871209984

Reproduction Example/SDK Setup

<script>
    window.sentryOnLoad = function () {
        Sentry.init({
            release: "[email protected]",
            tracesSampleRate: 0.5,
            maxBreadcrumbs: 50,
            sendDefaultPii: true, // debug
            initialScope: {
                user: {"logged":false},
            },
            ignoreErrors: [
                'gCrWeb',
                'Non-Error exception captured',
                'Non-Error promise rejection captured',
                'Event `ErrorEvent` captured as exception with message `Script error.`',
                "Can't find variable: _0x",
                "chrome-extension",
            ],
            denyUrls: [
                /google-analytics\.com/i,
                /www\.googletagmanager\.com/i,
                /graph\.facebook\.com/i,
                // Chrome extensions
                /extension/i,
                /extensions\//i,
                /^chrome:\/\//i,
                // Safari
                /webkit-masked-url/i,
            ],
        });

        Sentry.lazyLoadIntegration("captureConsoleIntegration").then(
            (integration) => {
                Sentry.addIntegration(integration({
                    levels: ['warn', 'error', 'debug', 'assert']
                }));
            },
        );

        Sentry.lazyLoadIntegration("httpClientIntegration").then(
            (integration) => {
                Sentry.addIntegration(integration());
            },
        );

        Sentry.lazyLoadIntegration("contextLinesIntegration").then(
            (integration) => {
                Sentry.addIntegration(integration());
            },
        );
    };
</script>

Steps to Reproduce

Using "Loader Script" with only "Enable Performance Monitoring" enabled.

This configuration will create a lot of events on sentry about:
TypeError: null is not an object (evaluating 'co.document.body.appendChild')
File: https://browser.sentry-cdn.com/8.30.0/bundle.tracing.min.js

Expected Result

No events created.

Actual Result

TypeError: null is not an object (evaluating 'co.document.body.appendChild')

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Sep 17, 2024
@github-actions github-actions bot added the Package: browser Issues related to the Sentry Browser SDK label Sep 17, 2024
@andreiborza
Copy link
Member

andreiborza commented Sep 18, 2024

Hello @jwv thanks for writing in.

I tried this out locally with the following file and couldn't reproduce:

<!DOCTYPE html>
<html lang="en">
<head>
    <script>
        window.sentryOnLoad = function () {
            Sentry.init({
                dsn: <redacted>
                release: "[email protected]",
                tracesSampleRate: 0.5,
                maxBreadcrumbs: 50,
                sendDefaultPii: true, // debug
                initialScope: {
                    user: {"logged": false},
                },
                ignoreErrors: [
                    'gCrWeb',
                    'Non-Error exception captured',
                    'Non-Error promise rejection captured',
                    'Event `ErrorEvent` captured as exception with message `Script error.`',
                    "Can't find variable: _0x",
                    "chrome-extension",
                ],
                denyUrls: [
                    /google-analytics\.com/i,
                    /www\.googletagmanager\.com/i,
                    /graph\.facebook\.com/i,
                    // Chrome extensions
                    /extension/i,
                    /extensions\//i,
                    /^chrome:\/\//i,
                    // Safari
                    /webkit-masked-url/i,
                ],
            });

            Sentry.lazyLoadIntegration("captureConsoleIntegration").then(
                (integration) => {
                    Sentry.addIntegration(integration({
                        levels: ['warn', 'error', 'debug', 'assert']
                    }));
                },
            );

            Sentry.lazyLoadIntegration("httpClientIntegration").then(
                (integration) => {
                    Sentry.addIntegration(integration());
                },
            );

            Sentry.lazyLoadIntegration("contextLinesIntegration").then(
                (integration) => {
                    Sentry.addIntegration(integration());
                },
            );
        };
    </script>
    <script src="https://js.sentry-cdn.com/<redacted>.min.js" crossorigin="anonymous"></script>
</head>
<body>
Hello World
<br/>
<button onclick="breaktheworld()">Break the world</button>
</body>
</html>

Running it via npx serve.

Do you have a minimal reproduction repo or stackblitz?

@jwv
Copy link
Author

jwv commented Sep 18, 2024

Hello @andreiborza thanks for take some time to analyze the problem.

I also can't reproduce this problem either, appears to be some combination of OS, Browser and Internet connection.

Did you take the time to review the events I sent you in the links?

Later i will create stackblitz and try to debug this events.

It's not a big problem because i can always filtered out this events.

Image
Image

Thank you again.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Sep 18, 2024
@andreiborza
Copy link
Member

@jwv I had a look at the events but couldn't really tell what the issue is from there sorry.

@Lms24
Copy link
Member

Lms24 commented Sep 18, 2024

Hey, I took a quick look and realistically, the only way how this could happen is that document.body is undefined/null. I tried reproducing this by serving an html page without a <body> element but it looks like browsers automatically create one if there's none.

So my running theory is that some "exotic" browser doesn't automatically add a body element 🤔

This very likely originates from lazyLoadIntegration where we inject a script tag into the document body (source to load the integration CDN bundle.

We could/should guard this, or use a similar mechanism as in our loader script to work around this. I'll take a look if we can fix this easily.

var _currentScriptTag = _document.scripts[0];
var _newScriptTag = _document.createElement(_script);
_newScriptTag.src = _sdkBundleUrl;
_newScriptTag.crossOrigin = 'anonymous';
// Once our SDK is loaded
_newScriptTag.addEventListener('load', function() {
try {
// Restore onerror/onunhandledrejection handlers
_window[_onerror] = _oldOnerror;
_window[_onunhandledrejection] = _oldOnunhandledrejection;
// Add loader as SDK source
_window.SENTRY_SDK_SOURCE = 'loader';
var SDK = _window[_namespace];
var oldInit = SDK.init;
// Configure it using provided DSN and config object
SDK.init = function(options) {
var target = _config;
for (var key in options) {
if (Object.prototype.hasOwnProperty.call(options, key)) {
target[key] = options[key];
}
}
oldInit(target);
};
sdkLoaded(callbacks, SDK);
} catch (o_O) {
console.error(o_O);
}
});
_currentScriptTag.parentNode.insertBefore(_newScriptTag, _currentScriptTag);

@AbhiPrasad
Copy link
Member

This was released with https://github.com/getsentry/sentry-javascript/releases/tag/8.31.0 - thanks for the GH issue @jwv!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: browser Issues related to the Sentry Browser SDK
Projects
Archived in project
4 participants