Skip to content

Commit

Permalink
fix(web): Gracefully degrade Proxy usage to fix IE11 (#2759)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentgoudet authored Apr 29, 2020
1 parent fd28a3a commit b61f909
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions core/src/web-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,31 @@ export class CapacitorWeb {
// get the typed benefits of the provided plugins in PluginRegistry
this.Plugins = {} as any;

// Build a proxy for the Plugins object that returns the "Noop Plugin"
// if a plugin isn't available
this.Plugins = new Proxy<any>(this.Plugins, {
get: (target, prop) => {
if (typeof target[prop] === 'undefined') {
let thisRef = this;
return new Proxy<any>({}, {
get: (_target, _prop) => {
if (typeof _target[_prop] === 'undefined') {
return thisRef.pluginMethodNoop.bind(thisRef, _target, _prop, prop);
} else {
return _target[_prop];
// Gracefully degrade in non-Proxy supporting engines, e.g. IE11. This
// effectively means that trying to access an unavailable plugin will
// locally throw, but this is still better than throwing a syntax error.
if ('Proxy' in window) {
// Build a proxy for the Plugins object that returns the "Noop Plugin"
// if a plugin isn't available
this.Plugins = new Proxy<any>(this.Plugins, {
get: (target, prop) => {
if (typeof target[prop] === 'undefined') {
let thisRef = this;
return new Proxy<any>({}, {
get: (_target, _prop) => {
if (typeof _target[_prop] === 'undefined') {
return thisRef.pluginMethodNoop.bind(thisRef, _target, _prop, prop);
} else {
return _target[_prop];
}
}
}
});
} else {
return target[prop];
});
} else {
return target[prop];
}
}
}
});
});
}
}

pluginMethodNoop(_target: any, _prop: PropertyKey, pluginName: string) {
Expand Down

0 comments on commit b61f909

Please sign in to comment.