Hello React, dear Berlin, how's it going on? I'm Sanket Sahu, and I run a company called GeekyAnds, and I co-created a UI component library for React Native and React called Nativebase. These days I'm working on the Glue Stack ecosystem, which is on the lines of Nativebase version 3, universal UI component library, and tools around design system.
But today I'm going to talk about something else. It's about how we can build a Wordpress-like plugin system, but for Next.js. We got multiple things from PHP to React, and what explains better than this meme? So why not plugins? If you don't know yet, WordPress plugins are like a bunch of plugins which are segregated into folders, and as soon as you drop them into your project, we get like functional behavior, functionality changes, as soon as we add these plugins. Much like a Chrome extension, but a bit different when you compare it with like npm packages. In packages you have to install and then integrate yourself, while plugins, they add end user functionality.
So this is the dashboard of WordPress where we have multiple plugins. And this is an example from the old days how you can upload a plugin. This is an example of contact form, and as soon as you upload it, you have that feature available on your website. So why not for next? So using this plugin system, we can go from this folder structure where we have say, for example, the auth behavior or authentication distributed across your app, right? From say, pages slash login.js components, login form.js, and then API for the same thing, login and log out. Then middleware, and store, and a bunch of different things. So with the plugin system, we can co-locate all these things inside its own module system or namespace wherein as soon as you add or remove this plugin, then that auth behavior is removed from your app, right? So in this experiment, I have tried to do a file based plugin system inspired by WordPress, and these plugins enable visual and non-visual features per plugin. I've got the checklist from WordPress, which is a file based system, persistent store per plugin, register components, routes, functions, event system, and dependency management. Although this is just an experiment, I've tried to imitate these three behaviors, file system based, register components, and register routes. And this is how the architecture looks like. So we have this plugin system, and all the plugins talk to that. This plugin system adds as an intermediate step at different places in Next.js. We have a root provider for plugin system and we have some implementation in middleware.ts and somewhere in the all.tsx file. It maintains its own store for routes and components. So, yeah, this is how the architecture looks like. Yeah, so some time for some demo. Great. So let me just try this. Okay, so how does the code look like? So we have a plugins folder and as soon as we install or add these folders, we get those functionality. So a quick demo would be this one. So this is a Next.js app, which has the plugin system installed. So what I've done is let's remove all these plugins into the download folder. And if I go and refresh this, so this is now a website which does not have any plugins installed. Say, if I want to do say, a Hello World plugin, which essentially adds an alert message, as soon as we drop it, we see this Hello World being activated.
Comments