-
Notifications
You must be signed in to change notification settings - Fork 251
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
plugins: Support obtaining React/Vue reference after Bugsnag.start() #839
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementation looks good. Maybe worth adding test coverage for the lazy loading case?
d75db7d
to
c61c04e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested the new lazy and non-lazy APIs in a TypeScript React example project and they look good to me.
I'm not sure the backwards compatibility is working though. If I do:
Bugsnag.start({
apiKey: 'YOUR_API_KEY',
plugins: [new BugsnagPluginReact(React)]
})
const ErrorBoundary = Bugsnag.getPlugin('react');
Then I end up with undefined
.
Consider also mentioning in changelog/upgrade guide the new preferred approach.
ba57e21
to
7d23518
Compare
This PR updates the browser plugins to support passing a reference to the framework in use at a point in time after Bugsnag has been started. The details are outlined below:
@bugsnag/plugin-react
Sometimes it will be the case that a
React
reference will not be available until after Bugsnag has been initialised.This PR introduces an alternative API to lazily pass the reference, but maintains backward compatibility.
The two modes of operation are now as follows:
Existing
New (lazy)
New (non-lazy)
Whilst the original interface remains, the non-lazy invocation also provides a similar method for obtaining the error boundary to the lazy version. This means it is easier to type.
An attempt has been made to help prevent footguns. For example, if you instantiate the plugin without a reference to React, but then pass the result of
getPlugin('react')
directly to a render, the following error is displayed:@bugsnag/plugin-vue
Similarly the Vue plugin has been updated to maintain backward compatibility, but also to support a framework reference later on.
Existing
New (lazy)
Types
Along with these updates, better typing has been provided to the plugins.
In order to achieve good types for the
getPlugin()
method, rather than centralizing all of the types in the top level notifier, thereby requiring dependencies on React, Vue etc., for the top level notifiers I used the “module augmentation” method – as described in the TypeScript handbook here.In order to do this, I needed to undo some of the changes that were made in this PR, since there we added a prepublish step to each of the top-level notifiers and plugins to bundle the types locally inside their package.
So, with each notifier and plugin having a local copy of the
@bugsnag/core
types insidedist/types/bugsnag-core
in its package and each having a reference to the@bugsnag/core
module, it means we can’t use the module augmentation technique, since it will only augment its local copy.The reason for applying this change in the past was to prevent each notifier having an install-time dependency on
@bugsnag/core
, just in case the consumers were using types. We’re now in the position where the alternative is for people installing@bugsnag/js
, they’d also getReact
,Vue
,Express
etc., just in case they intend to use those plugins. So now it seems like a dependency on@bugsnag/core
is the better approach.The changes I have made mean that Bugsnag modules installed together on a project will reference the same single version of
@bugsnag/core
, enabling each of the plugin’s definitions to augment the definition of the client’sgetPlugin()
method to give it a more detailed type.In a separate PR I will add improvements to the types for plugins that were not touched here.