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

fix(vue-app): fallback to global nuxt instance of $root is not available #9008

Merged
merged 4 commits into from
Mar 31, 2021

Conversation

vcordes79
Copy link

@vcordes79 vcordes79 commented Mar 18, 2021

Types of changes

return window.$nuxt if $root is not set

  • Bug fix (a non-breaking change which fixes an issue)
  • New feature (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Description

$nuxt was not always accessible (if $root was not set), so return the property from window object

fixes #8995

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly. (PR: #)
  • I have added tests to cover my changes (if not applicable, please state why)
  • All new and existing tests are passing.

return window.$nuxt if $root is not set
@vcordes79
Copy link
Author

fixes #8995

@vcordes79 vcordes79 changed the title make sure $nuxt is set if possible fix: make sure $nuxt is set if possible Mar 18, 2021
@pi0 pi0 changed the title fix: make sure $nuxt is set if possible fix(vue-app): fallback to global nuxt instance of $root is not available Mar 22, 2021
@@ -46,7 +46,7 @@ Vue.component(Nuxt.name, Nuxt)

Object.defineProperty(Vue.prototype, '<%= globals.nuxt %>', {
get() {
return this.$root.$options.<%= globals.nuxt %>
return this.$root.$options.<%= globals.nuxt %> || (process.client && typeof window !== 'undefined' && window.<%= globals.nuxt %>)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm worried this will lead to inconsistent behaviour between client/server rendering.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed we have to double-check this. Check-related issue #8995 is due to usage of the second app by vuetify.

Copy link
Author

@vcordes79 vcordes79 Mar 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any inconsistant behaviour between client/server.

In the server it will be this.$root.$options.<%= globals.nuxt %> if it's defined. If it's not defined, it will still be undefined, because window is not defined server side. I see there can be an error when accessing a property on undefined - the changed code would fix this.

On the client it won't change anything if $root exists - only if it does not. Could you please explain, what the problem is?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks OK, but as @pi0 mentioned, let double check, a test for this would be test.

BTW, I think we can split this complex expression into if/else, sth like:

const globalNuxt = this.$root.$options.<%= globals.nuxt %>

if(process.client && !globalNuxt && window) {
  return window.<%= globals.nuxt %>
}

return globalNuxt

@pi0 pi0 merged commit c04a793 into nuxt:dev Mar 31, 2021
@pi0 pi0 mentioned this pull request Apr 1, 2021
@PayelKarmakar06
Copy link

PayelKarmakar06 commented Apr 14, 2021

I recently tried upgrading to 2.15.4, but I am still facing $root not available issue.
Why don't we add a null check against $root?
The control is not going beyond - const globalNuxt = this.$root.$options.$nuxt , as this.$root is not available. Hence the next if block -
if (process.client && !globalNuxt && typeof window !== 'undefined') {
return window.$nuxt
}
is of no use to me.

What would rather work is -
const globalNuxt = this.$root ? this.$root.$options.$nuxt : null // null check added
if (process.client && !globalNuxt && typeof window !== 'undefined') {
return window.$nuxt
}

@danielroe danielroe added the 2.x label Jan 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

custom components in vuetify-dialog not working
5 participants