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

Accessing composables in the DevTools #216

Closed
zoey-kaiser opened this issue May 3, 2023 · 6 comments
Closed

Accessing composables in the DevTools #216

zoey-kaiser opened this issue May 3, 2023 · 6 comments

Comments

@zoey-kaiser
Copy link

Hello! Thank you for all the time and hard work you have put into this project! It really sets Nuxt apart from Next and other frameworks! 🥳

We at the sidebase team are now slowly looking to add a Nuxt DevTools integration for our authentication package. I have been browsing the docs and source code for a while now and have also looked at how other modules handled creating their pages, however I have sadly not been able to find a solution.

We are using one main composable for authentication in @sidebase/nuxt-auth named useAuth(), which returns information about the current session, available providers and much more. This data is super helpful for debugging issues with our authentication and we would love to add a browser for this data inside the DevTools. How can I access the composable with the correct information inside the DevTools? Whenever I try to use it (without importing or importing it from #imports) it says it does not exist. When I try importing it from the source or dist folder it also does not work. Can you give me any pointers?

Thanks a lot in advance!

For reference here is the PR I am working in, along with the replication of our setup: sidebase/nuxt-auth#364

@antfu
Copy link
Member

antfu commented May 3, 2023

You want to access client state from a composable to your own DevTools view, did I understand correctly?

Composables are compile time so at runtime we can't import them dynamically (#imports module does not actually exists but will be compiled away).

The solution I can think about, is that maybe you could register a dev-time-only plugin to expose the state to somewhere like the nuxt/app instance, and access the instance using useDevtoolsClient() from the kit.

@zoey-kaiser
Copy link
Author

Thanks for your quick answer! Ill have a shot at this and let you know how it goes!

@arashsheyda
Copy link
Member

@antfu Do you think it's a good idea to provide useAuth in a plugin file?

example runtime/plugin.ts:

 const auth = useAuth()
  return {
    provide: {
      auth
    }
  }

so it can be accessible like: const { status } = client.value?.host.nuxt.vueApp.config.globalProperties?.$auth

@zoey-kaiser
Copy link
Author

Hi @arashsheyda and @antfu

Thanks again for both of your help! I used a similar approach as you outlines @arashsheyda and it works now! What is the future plan for composable inside the DevTools?

I generally prefer the Syntax of composables over that of plugins, which is why we went with that route initially. Will there be any downsides to providing a composable + plugin now (as we are only adding this for DevTools support).

@antfu
Copy link
Member

antfu commented May 3, 2023

As I mentioned, auto-imported composables are compiling time, while DevTools is runtime + server config. So for devtools, your composable might not even be shipped to composable. We might be able to do a proxy plugin like you did to forward them, but that might be inefficient and might not be commonly used. I think your custom plugin approach works perfectly.

@zoey-kaiser
Copy link
Author

Sounds good! Thanks for all your help ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants