Skip to content

Commit

Permalink
fix!: properly handle context when using multiple nuxt instances (#762)
Browse files Browse the repository at this point in the history
<!--
☝️ PR title should follow conventional commits
(https://conventionalcommits.org).
In particular, the title should start with one of the following types:

- docs: 📖 Documentation (updates to the documentation or readme)
- fix: 🐞 Bug fix (a non-breaking change that fixes an issue)
- feat: ✨ New feature/enhancement (a non-breaking change that adds
functionality or improves existing one)
- feat!/fix!: ⚠️ Breaking change (fix or feature that would cause
existing functionality to change)
- chore: 🧹 Chore (updates to the build process or auxiliary tools and
libraries)
-->

### 🔗 Linked issue

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

### 📚 Description

With nuxt/nuxt#28392 it is now possible to key
the context to the correct nuxt instance.

Breaking change since we need the newest nuxt version for this to work.

<!-- Describe your changes in detail -->
<!-- Why is this change required? What problem does it solve? -->
  • Loading branch information
tobiasdiez authored Aug 24, 2024
1 parent 35af11c commit 150f010
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 203 deletions.
8 changes: 4 additions & 4 deletions packages/storybook-addon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@
"release": "pnpm changelogen --release --push && pnpm publish"
},
"peerDependencies": {
"nuxt": "^3.12.0",
"nuxt": "^3.13.0",
"vite": "^5.2.0",
"vue": "^3.4.0"
},
"dependencies": {
"@nuxt/kit": "^3.12.2",
"@nuxt/schema": "^3.12.2",
"@nuxt/vite-builder": "^3.12.2",
"@nuxt/kit": "^3.13.0",
"@nuxt/schema": "^3.13.0",
"@nuxt/vite-builder": "^3.13.0",
"@rollup/plugin-replace": "^5.0.7",
"@storybook/builder-vite": "^8.2.9",
"@storybook/vue3": "^8.2.9",
Expand Down
17 changes: 8 additions & 9 deletions packages/storybook-addon/src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,9 @@ setup(async (vueApp, storyContext) => {
throw new Error('StoryContext is not provided')
}

// This is the `nuxtApp._name`, it's the same for all stories.
const appId = 'nuxt-app'
const globalCtx = getContext(appId)

// This is the `nuxtApp.globalName`, it's different for each story.
const storyNuxtAppName = `nuxt-app-${key}`
const storyNuxtCtx = getContext(storyNuxtAppName)
// Create a new nuxt app for each story
const storyNuxtAppId = `nuxt-app-${key}`
const storyNuxtCtx = getContext(storyNuxtAppId)
if (storyNuxtCtx.tryUse()) {
// Nothing to do, the Nuxt app is already created
return
Expand All @@ -68,11 +64,14 @@ setup(async (vueApp, storyContext) => {
}

const nuxt = createNuxtApp({
id: storyNuxtAppId,
vueApp,
globalName: storyNuxtAppName,
})

globalCtx.set(nuxt, true)
// Provide the Nuxt app as context
storyNuxtCtx.set(nuxt, true)
// ...also for calls of useNuxtApp with the default key
getContext('nuxt-app').set(nuxt, true)

await applyPlugins(nuxt, pluginsTyped)
await nuxt.hooks.callHook('app:created', vueApp)
Expand Down
Loading

0 comments on commit 150f010

Please sign in to comment.