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

Error: [nuxt] instance unavailable / Context conflict #661

Closed
tobiasdiez opened this issue Jun 27, 2024 · 13 comments · Fixed by #762
Closed

Error: [nuxt] instance unavailable / Context conflict #661

tobiasdiez opened this issue Jun 27, 2024 · 13 comments · Fixed by #762
Labels
status: help wanted Good idea, but the core developers will not work on it at the moment type: bug Something isn't working

Comments

@tobiasdiez
Copy link
Collaborator

tobiasdiez commented Jun 27, 2024

With #619, composable and other things using the nuxt instance are generally working. However, sometimes when switching between stories one still gets a [nuxt] instance unavailable error.

Todo: find a way to reproduce this reliably and investigate what's the problem

Workaround: refresh the page

@tobiasdiez tobiasdiez added type: bug Something isn't working status: help wanted Good idea, but the core developers will not work on it at the moment labels Jun 27, 2024
@tobiasdiez tobiasdiez changed the title Error: [nuxt] instance unavailable Error: [nuxt] instance unavailable / Context conflict Jul 14, 2024
@obulat
Copy link
Contributor

obulat commented Jul 16, 2024

When running the examples using dev locally, I can reliably reproduce the context conflict when I navigate to the second story.

The problem is that the module sets nuxt to only the current context. However, when installs plugins for the second story's nuxt app, it gets the context using the nuxtApp._name, not the nuxtApp.globalName. The _name is always equal to the appId, so it's nuxt-app in every story. Thus, when installing plugins for the second story, Nuxt is re-using the first story's context, which causes a context conflict.

I naively fixed this by adding the following in the packages/storybook-addon/src/preview.ts:

const appId = `nuxt-app`

const nuxtGlobalCtx = getContext(appId)
const currentStoryNuxtAppName = `${appId}-${key}`
const currentStoryNuxtCtx = getContext(currentStoryNuxtAppName)
if (currentStoryNuxtCtx.tryUse()) {
    return
}

After createNuxtApp:

- globalCtx.set(nuxt, true)
+ nuxtGlobalCtx.set(nuxt, true)

tobiasdiez added a commit that referenced this issue Aug 3, 2024
<!---
☝️ PR title should follow conventional commits
(https://conventionalcommits.org)
-->

### 🔗 Linked issue

<!-- If it resolves an open issue, please link the issue here. For
example "Resolves #123" -->
Related #661

### ❓ Type of change

<!-- What types of changes does your code introduce? Put an `x` in all
the boxes that apply. -->

- [ ] 📖 Documentation (updates to the documentation or readme)
- [x] 🐞 Bug fix (a non-breaking change that fixes an issue)
- [ ] 👌 Enhancement (improving an existing functionality)
- [ ] ✨ New feature (a non-breaking change that adds functionality)
- [ ] 🧹 Chore (updates to the build process or auxiliary tools and
libraries)
- [ ] ⚠️ Breaking change (fix or feature that would cause existing
functionality to change)

### 📚 Description

<!-- Describe your changes in detail -->
<!-- Why is this change required? What problem does it solve? -->
This PR updates the global nuxt context, setting its `nuxt` property to
the story's nuxt when setting up the current story.

Previously, when navigating from one story to another story, Nuxt would
get the context using the app's `_name`, which is the same for all
stories, instead of using the `globalName`, which is different for each
story.

Co-authored-by: Tobias Diez <code@tobiasdiez.de>
@maximilian-schwarz
Copy link

I think this issue is still there. I have create a reproduction. The context conflict only occurs if you create multiple stories.

Reproduction:
https://stackblitz.com/edit/nuxt-starter-dz4rbl

If you got to the "SingleStory" docs everything is working fine. You you got to the "MultiStory" docs you can see the context issue.

@ademyalcin27
Copy link

@tobiasdiez , You closed the issue I opened but the problem still persists.

@maximilian-schwarz
Copy link

@ademyalcin27 can you please create a reproduction? I checked it with the nigthly build and everything is working

@ademyalcin27
Copy link

@ademyalcin27 can you please create a reproduction? I checked it with the nigthly build and everything is working

Yes I did here, #768

@maximilian-schwarz
Copy link

@ademyalcin27 can you please create a reproduction? I checked it with the nigthly build and everything is working

Yes I did here, #768

In your reproduction you use the version 8.2.0. The issue is currently only available with the nightly build. I think the team will include the fix in the next release ;)

to test the fix use the nightly build as dependency.

{
  "devDependencies": {
    "@nuxtjs/storybook": "npm:@nuxtjs/storybook@nightly"
  }
}

@ademyalcin27
Copy link

@ademyalcin27 can you please create a reproduction? I checked it with the nigthly build and everything is working

Yes I did here, #768

In your reproduction you use the version 8.2.0. The issue is currently only available with the nightly build. I think the team will include the fix in the next release ;)

to test the fix use the nightly build as dependency.

{
  "devDependencies": {
    "@nuxtjs/storybook": "npm:@nuxtjs/storybook@nightly"
  }
}

I tried this version but still doesn't work autodoc

@maximilian-schwarz
Copy link

@ademyalcin27 can you please create a reproduction? I checked it with the nigthly build and everything is working

Yes I did here, #768

In your reproduction you use the version 8.2.0. The issue is currently only available with the nightly build. I think the team will include the fix in the next release ;)
to test the fix use the nightly build as dependency.

{
  "devDependencies": {
    "@nuxtjs/storybook": "npm:@nuxtjs/storybook@nightly"
  }
}

I tried this version but still doesn't work autodoc

You need minimum "nuxt": "3.13.0".

@ademyalcin27
Copy link

@ademyalcin27 can you please create a reproduction? I checked it with the nigthly build and everything is working

Yes I did here, #768

In your reproduction you use the version 8.2.0. The issue is currently only available with the nightly build. I think the team will include the fix in the next release ;)
to test the fix use the nightly build as dependency.

{
  "devDependencies": {
    "@nuxtjs/storybook": "npm:@nuxtjs/storybook@nightly"
  }
}

I tried this version but still doesn't work autodoc

You need minimum "nuxt": "3.13.0".

Yes its Im using 3.13.0 , but still Im getting same eror

@tobiasdiez
Copy link
Collaborator Author

Could you please share an updated reproduction?

@tobiasdiez tobiasdiez reopened this Aug 29, 2024
@ademyalcin27
Copy link

Could you please share an updated reproduction?

https://stackblitz.com/edit/nuxt-starter-ga2at1?file=package.json

@tobiasdiez
Copy link
Collaborator Author

"@storybook-vue/nuxt": "^8.2.0",

is not needed and pulls in the older version without the fix

@ademyalcin27
Copy link

"@storybook-vue/nuxt": "^8.2.0",

is not needed and pulls in the older version without the fix

Thank you this solve my problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted Good idea, but the core developers will not work on it at the moment type: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants