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

Can Not Re-assign $apollo #924

Closed
RedPlatypus opened this issue Feb 12, 2020 · 4 comments
Closed

Can Not Re-assign $apollo #924

RedPlatypus opened this issue Feb 12, 2020 · 4 comments

Comments

@RedPlatypus
Copy link

RedPlatypus commented Feb 12, 2020

Describe the bug
Multiple web-components on a page implementing vue-apollo 3.0.3. lead to re-definition error of $apollo

To Reproduce
Steps to reproduce the behavior:

  1. Build 2 vue components seperatly that both implement vue-apollo using whatever your bundler is. We're using https://cli.vuejs.org/guide/cli-service.html#vue-cli-service-build Vue-Cli-Service
  2. Import both of these web-components into a new page
  3. See error

Uncaught TypeError: Cannot redefine property: $apollo

Expected behavior

  1. This error would not occur
  2. vue-apollo would check if it's redefining $apollo
  3. In packages>vue-apollo>src>index.js the Apollo package would wrap define in a check to see if $apollo is already defined.
  // Lazy creation
  if (!Vue.prototype.hasOwnProperty('$apollo')) {
    Object.defineProperty(Vue.prototype, '$apollo', {
      get () {
        if (!this.$_apollo) {
          this.$_apollo = new DollarApollo(this)
        }
        return this.$_apollo
      },
    })
  }

Versions
vue: "^2.6.11"
vue-apollo: 3.0.3
apollo-client: "^2.6.4"

Additional context
Seems that this was a past issue, and had been resolved, and then removed?
#358

66b8cc9#diff-6c29e3b5395a867dff4aa2f0dc7124b2

I would have added a PR, but could not figure out how to build dist files.

@JoshuaJarman
Copy link

👏 following. experiencing the same issue with multiple web component. 👍

@JoshuaJarman
Copy link

this not only affects standard web components built using vue, it affects any page with multiple vue applications or independently built components.

really hoping this basic sanity check makes the next update as it is such a quick and obvious fix.

JoshuaJarman added a commit to JoshuaJarman/vue-apollo that referenced this issue Feb 22, 2020
vuejs#924

Added the recommended sanity check .hasOwnProperty that is best practice for .defineProperty to fix .$apollo redefinition bug when using more than one Vue application on a page, or multiple vue built standard web components, like a web component library.

This bug is a show stopper for using vue-apollo in standard web components but fortunately was easily fixable by adding the standard best practices check around the block to prevent redefinition.

Also worth noting that this same issue it has occurred and crept back into this project several times.
@JoshuaJarman
Copy link

JoshuaJarman commented Feb 22, 2020

Added a pull request with the proposed fix. Thanks! 👍
#930

@Akryum Akryum closed this as completed in 98bc132 Jul 27, 2020
@selfagency
Copy link

i am having this same problem right now with vite

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants