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: remove Nuxt context conflict #723

Merged
merged 2 commits into from
Aug 3, 2024
Merged
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
20 changes: 14 additions & 6 deletions packages/storybook-addon/src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* https://github.com/storybookjs/storybook/blob/main/docs/configure/index.md#configure-story-rendering
*
* We use it to load the Nuxt app in the preview iframe.
* This should contain the same setup as the what Nuxt does in the background.
* This should contain the same setup as what Nuxt does in the background.
* https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/entry.ts
*/

Expand Down Expand Up @@ -34,9 +34,15 @@ setup(async (vueApp, storyContext) => {
if (!key) {
throw new Error('StoryContext is not provided')
}
const nuxtAppName = `nuxt-app-${key}`
const nuxtCtx = getContext(nuxtAppName)
if (nuxtCtx.tryUse()) {

// 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)
if (storyNuxtCtx.tryUse()) {
// Nothing to do, the Nuxt app is already created
return
}
Expand All @@ -63,12 +69,14 @@ setup(async (vueApp, storyContext) => {

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

globalCtx.set(nuxt, true)

await applyPlugins(nuxt, pluginsTyped)
await nuxt.hooks.callHook('app:created', vueApp)
await nuxt.hooks.callHook('app:beforeMount', vueApp)
nuxtCtx.set(nuxt, true)

// TODO: The following are usually called after the app is mounted
// but currently storybook doesn't provide a hook to do that
Expand Down