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!: properly handle context when using multiple nuxt instances #762

Merged
merged 1 commit into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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