Skip to content

Commit

Permalink
fix!: properly handle context when using multiple nuxt instances
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Aug 23, 2024
1 parent 35af11c commit 615811b
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 615811b

Please sign in to comment.