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

histoire-plugin-nuxt: provide from plugin not working with useNuxtApp in component #666

Open
6 tasks done
CIB opened this issue Jan 22, 2024 · 4 comments
Open
6 tasks done
Labels
to triage This issue needs to be triaged

Comments

@CIB
Copy link

CIB commented Jan 22, 2024

Describe the bug

When using provide from a nuxt plugin, and inserting the provided value into a component using useNuxtApp, the provided value will not be available, and an error will be thrown.

Reproduction

Stackblitz: https://stackblitz.com/edit/nuxt-starter-7avtmp?file=components%2Ftest.vue

In plugins/example.ts:

  return {
    provide: {
      example: { foo: 'bar' },
    },
  };

In components/test.vue

const nuxtApp = useNuxtApp();
const foo = nuxtApp.$example.foo;

Then try to start histoire dev with a story that includes <Test />, and an error will be thrown that nuxtApp.$example is undefined.

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.14.0 - /usr/local/bin/pnpm
  npmPackages:
    @histoire/plugin-nuxt: ^0.17.8 => 0.17.8 
    @histoire/plugin-vue: ^0.17.8 => 0.17.8 
    histoire: ^0.17.8 => 0.17.8

Used Package Manager

npm

Validations

@CIB CIB added the to triage This issue needs to be triaged label Jan 22, 2024
Copy link

stackblitz bot commented Jan 22, 2024

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@dreitzner
Copy link

Same issue when using nuxtApp.provide('helpers', helpers); (which I guess has the same logic under the hood.

@positiveprogrammer
Copy link

This is because Histoire/plugin-next stub out useNuxtApp?

@0x100101
Copy link

0x100101 commented Jun 12, 2024

This is because Histoire/plugin-next stub out useNuxtApp?

That's the problem.

The stub could replicate a simple provide by doing something like this:

const nuxtApp = {
    runWithContext: async fn => await fn(),
    provide(key, value) {
        const propertyName = '$' + key;
        nuxtApp[propertyName] = value;
    }
}

export const useNuxtApp = () => nuxtApp

However, this doesn't completely solve the issue, because I'm seeing that plugins don't get access to the stubbed nuxtApp. So anything they provide still won't be available to components in histoire.

export default defineNuxtPlugin(async (nuxtApp) => {
  // nuxtApp here is not the stubbed one
})
export default defineNuxtPlugin(() => {
  return {
    // This is not going to interact with the stubbed nuxtApp
    provide: {
      img: useImage(),
    },
  }
})

I don't fully understand the context around why the decision was made to stub nuxtApp.

If I disable it (by removing the imports:sources hook) here: https://github.com/histoire-dev/histoire/blob/main/packages/histoire-plugin-nuxt/src/index.ts#L147, histoire is able render all of my components as expected without any noticeable issue.

@Akryum, @danielroe, or others. Some additional high-level info may help someone address the issue.

Related: #710

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to triage This issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

4 participants