Nuxt DevTools: The Ultimate Toolbox
note: this was prepared for VueConf Toronto 2023.
Hi everyone! Today, we'll explore the power and versatility of Nuxt DevTools and how it can enhance your Nuxt Development Experience.
What is Nuxt DevTools?
Nuxt DevTools is a Nuxt Module that provides a rich set of debugging and development tools. It's designed to simplify and optimize the development process, making it easier to build strong and performant Nuxt applications. It is built by Anthony Fu, and it's open-sourced under Nuxt repositories.
The purpose of it is simple, to speed up the development process and make it easier to debug and optimize our Nuxt projects. It provides a rich set of tools that we will explore in a bit.
Instalation
Nuxt DevTools comes pre-enabled when you initialize a new Nuxt project, so you don't need to worry about that. However, if you have a project without DevTools, you can simply use the wizard 🧙
Features of Nuxt DevTools
Let's dive into a project and see Nuxt DevTools in action!
the video will be uploaded shortly! mean while you can explore Nuxt DevTools yourself!
So, now that we know what Nuxt DevTools looks like, what's your favorite tab?
Custom Module: CRUD Generator
Okay, Time to get our hands dirty :) Run the below command to generate the module's project.
Module Structure
so if you go to my-module
directory, you can see that we have 3 main directories:
- client: this directory is responsible for the module's UI.
- playground: a plain nuxt project to test your module.
- src: the module's code
RPC
Now that we've set up the structure for our module, it's time to dive into one of the most exciting parts...
What is RPC?
RPC stands for Remote Procedure Call, a powerful concept that enables communication between different parts of your application. With RPC, you can trigger functions or actions on the server-side (module's code - src) from the client-side (module's UI - client). This opens up a world of possibilities for creating dynamic and interactive modules.
Coding Time
First of all, we need to define some types for our RPC. let's create a file: ~/src/types.ts
Now it's time to initialize our module's RPC. Begin by creating a file under ~/src/rpc.ts
in your module's directory. This file will contain the functions that you can call from your module's UI to perform various actions on the server-side.
In this example, we've created an main function to setup the RPC in out module which has 2 simple functions that we can call:
- the
getModulesOptions
will simply pass the options of our module. - the
generateApiCRUD
will create CRUD(create, read, update, delete) files in projects/server/api/
directory.
Once you've defined your RPC functions, you can call them from your module's UI, creating dynamic interactions that enhance the user experience; but before that we have to add setupRPC
to devtools.ts
now let's create the util function useViteWebSocket
now the module side is done. let's go to client side. we need a RPC composable so we can call the module's RPC's. create a file at ~/client/composables/rpc.ts
make sure to use the same RPC_NAMESPACE
so now we can simply call rpc in our client side and call a server rpc (e.g. getModulesOptions
): ~/client/pages/index.vue
Finished Project
And voilà ! we're done. tried to keep this as simple as possible, you can find the source code of the finished project with a better architecture at github.
also there is a starter-project for DevTools module with RPC structure here.
you can learn more about Nuxt DevTools in it documentation
Real World Examples
Here are some real-world examples that demonstrate the versatility and power of Nuxt DevTools: