airbrake / airbrake-js

Airbrake JavaScript Notifier
https://airbrake.io
MIT License
422 stars 139 forks source link

Prerender fails due to reliance on window #678

Closed jnpwebdeveloper closed 4 years ago

jnpwebdeveloper commented 4 years ago

We're having an issue where our build fails due to the browser package not correctly checking if the global window object exists.

We looked at several options and think perhaps the best place to fix this would be directly here. Is this something you can help with or point us in the right direction? This is the error we are getting after running npx preact build --verbose:

image

I'm a bit unfamiliar with how I'd contribute to this kind of setup as you have the two sub-folders which I'm guessing push up the two NPM packages.

sgray commented 4 years ago

Thanks, I'll take a look. Are you using @airbrake/browser 1.1.1?

jnpwebdeveloper commented 4 years ago

Thank you. Yes we are.

"dependencies": {
    "@airbrake/browser": "^1.1.1",
    "preact": "^10.3.2",

For now we've wrapped the notifier ourselves here which seems to work on our end but feels a bit clunky. Would be nice to have this part and parcel of the package.

class ErrorBoundary extends Component {
  isBrowser() {
    return (typeof window !== 'undefined');
  }
  constructor(props) {
    super(props);
    this.state = { hasError: false };
    if (this.isBrowser())  {
      this.airbrake = new Notifier({
        projectId: process.env.PREACT_APP_AIRBRAKE_PROJECT_ID,
        projectKey: process.env.PREACT_APP_AIRBRAKE_PROJECT_KEY,
      });
    }
  }