[RFC] Plugins #9133
Replies: 77 comments 5 replies
-
Interesting concept. In case you haven't seen it yet, here's an interesting read about some considerations around plugin systems that I think apply here as well. How would you handle ordering and plugins depending on each other, and with that, deduplication? |
Beta Was this translation helpful? Give feedback.
-
This'll be awesome! We also need ability to modify the webpack config, for example to make Perhaps plugins can use the exact same |
Beta Was this translation helpful? Give feedback.
-
Love it 👊 |
Beta Was this translation helpful? Give feedback.
-
What I need for
|
Beta Was this translation helpful? Give feedback.
-
This looks really cool!
I could see erroring when missing env vars being a source of frustration — sometimes, when working quickly, you install some packages but don’t end up using them. The next time the developer restarts their server in development, it would error. Perhaps instead, it could warn in development when the server first loads, skip loading the plugin, and then error during the production build? With a message that says to either provide the missing env vars or uninstall the plugin (with the package name, and ideally, a command to copy paste to uninstall) Ideally, plugins would be able to optionally provide a label saying how to find the values for the env var, so that fixing the error is more clear. But feel free to ignore this, this is really cool and maybe this suggestion is premature |
Beta Was this translation helpful? Give feedback.
-
I think it would be cleaner with hooks, and follow the hooks style. But again that's a matter of preference, and up to debate. Two options would be great. |
Beta Was this translation helpful? Give feedback.
-
I'd also add autodetection for With autodetection, I'm curious how to handle cases when order of plugin execution matters. E.g. one might want to make Sentry's |
Beta Was this translation helpful? Give feedback.
-
Can't wait. 👍🏻 |
Beta Was this translation helpful? Give feedback.
-
I am so excited about this 🎉 Let me know about the Sentry plugin - willing to help out! |
Beta Was this translation helpful? Give feedback.
-
Great idea. I will appreciate it if a repo link for full fledged sample next-plugin is provided. Also, any timelines on this? |
Beta Was this translation helpful? Give feedback.
-
I share the same concern about loading order, as well as for
It'd be nice if this could be configured for libraries that use transpilation so they can output to a separate folder. Also it might be nice if a plugin could be organized as:
though that may be overkill/perf problematic and a code smell for an overly large plugin.
I think this would be super helpful for developing and testing plugins without mucking with linking/ |
Beta Was this translation helpful? Give feedback.
-
It would be great if during the build stage, Next.js would be able to pass the routes of the application:
Thanks! Edit: also, bear in mind that people usually put their final code inside a |
Beta Was this translation helpful? Give feedback.
-
We won't be exposing the webpack config to plugins as it's by far the easiest way to cause deoptimization. Instead we'll provide methods for aliasing modules for react-native-web for example. |
Beta Was this translation helpful? Give feedback.
-
Initially we won't be supporting these as they also sound like pitfalls for deoptimization. For example:
Furthermore
This is not possible as in development there is on-demand-entries. |
Beta Was this translation helpful? Give feedback.
-
It's sort of dangerous to do so though, as it would be easy for you to forget providing environment variables in a certain environment and that would cause the plugin to either be excluded or error out based on the description. Potentially we could do that behavior in development and fail if the env var is not set in production builds though. |
Beta Was this translation helpful? Give feedback.
-
Hi, this is probably more a "next step" but something you'd want to consider for plugins is UI. That probably goes beyond the idea of "plugins" to form fully independent "packages". An example: say I want to implement a debug package, with an admin interface. You'd like it to add pages available for admin users. That could mean having a "prebuild" or "install" step for plugins, that would allow for more complex file manipulation like adding pages into the users project. That's not a very good example but you get the point, that could be enhancing Next.js with a blog connected to a 3rd party headless cms for example or stuffs like that. |
Beta Was this translation helpful? Give feedback.
-
Here is a link to a more detailed reference to what we want to implement in Vulcan with Next: VulcanJS/vulcan-next#3 The issue might not be very clear to non-Meteor/non-Vulcan devs but basically what we try to do is to bring the power of Meteor isomorphic packages and Vulcan dynamic generation (Mongo collection, graphql API, routes and overrideable React components) to what we could call the "static" ecosystem, through Next. A powerful plugin system in Next means an easier transition for us. To sum it up, our plugins plugin should be able:
Edit: example that inspires us are Apollo Universal Starter Kit package system (it's fully dynamic but the ideas are there) and the idea of CSF in Storybook. |
Beta Was this translation helpful? Give feedback.
-
I'm wondering the same as @HaNdTriX (comment link) and would like to understand how this would fit within a page-centered approach. Would there ever be a need for a plugin to run or be used on certain pages and not others? And if yes, how would this be supported within the context of this RFC? |
Beta Was this translation helpful? Give feedback.
-
👍 I've hacked in something in a fork to enable NewRelic monitoring of the export process (ratehub@9a46ba7) and would be great to be able to do this with hooks. |
Beta Was this translation helpful? Give feedback.
-
Hi guys, if it can be of any help this is my first attempt as loading Material UI and Styled Components as packages. Far from being done, but I've managed to abstract Example of a material UI document enhancer They rely on this common interface:
This example is limited to stylesheet collection, but could generalize to other things that need to wrap the app, fill a global variable during render and then get the result. (Also, it may be broken or not working as expected.. I'll let it rest during the week-end before checking it back). |
Beta Was this translation helpful? Give feedback.
-
Here's a blog post we wrote about how we are using plugins today! https://www.grouparoo.com/blog/nextjs-plugins |
Beta Was this translation helpful? Give feedback.
-
After fighting to create some reusable NPM packages for Next, and partly failing to do so (#17262, martpie/next-transpile-modules#120), maybe the first step toward a plugin system would be to write official guidelines about writing NPM packages for Next. I think in particular about the built step: since the package is meant to be used in a Next app, the build requirements are a bit different than for an usual package. For instance you can ship non transpiled TypeScript code, but you have to take care about not breaking Next.js |
Beta Was this translation helpful? Give feedback.
-
Wrapping of the main app component would be very needy, for plugins wanting to provide data using React Context. E.g. https://www.gatsbyjs.com/docs/browser-apis/#wrapRootElement Might have skipped it, could not find a comment on it |
Beta Was this translation helpful? Give feedback.
-
Could we get an official update on the status of this RFC and any expected changes/finalization? |
Beta Was this translation helpful? Give feedback.
-
I came across this thread trying to write a plugin for Elastic APM. It looks like there's a rewrite going on in #20759. Here's hoping there's going to be movement on finalizing plugins at some point.. |
Beta Was this translation helpful? Give feedback.
-
What’s the status of this? This for sure will be a game changer for Next.js. I can imagine plugins for authentication, third party services, analytics, telemetry, among others. |
Beta Was this translation helpful? Give feedback.
-
Disabling plugin system prevent us from upgrating next.js to the latest one. we got bunch of server side and client side initializers, that has been run in the old plugin system. It worked well until 10.3. My point is would you really want to support a plugin system in the future. If not, I’d rather modify all of it now, otherwise I can wait until that is supported. |
Beta Was this translation helpful? Give feedback.
-
Update: As others have mentioned, we have removed the previously experimental version of this RFC that was in Next.js. Even though it was marked as experimental and mentioned to not use, some folks still ended up taking it to production, leading to a bad experience for everyone. After extensive testing, we decided we needed to iterate on the API more to satisfy all use cases for plugins for Next.js and to make it extensible for the future. tl;dr We are still planning to add a plugin system to Next.js. |
Beta Was this translation helpful? Give feedback.
-
Hi guys, I am struggling and feeling a bit alone on this:
And I am strugging a lot to do just this.
Would anyone volunteer to give a shot at writing a demo fullstack package that fulfill those requirements? I can provide a demo of what I've done so far with Esbuild (with Lerna, Turborepo and all 🚀) I think having an example like this would help writing more packages for Next.js app, even if there is no plugin system yet, it shouldn't prevent people to write good old NPM package. |
Beta Was this translation helpful? Give feedback.
-
any update on this? 😢 |
Beta Was this translation helpful? Give feedback.
-
Authored together with @ijjk.
Goals
Background
Over time the Next.js examples directory has grown to 210+ examples. These examples show how to integrate external libraries for styling, error reporting, data fetching, and many other applications.
Most of these examples require a bit of configuration in various parts of Next.js, for example, in
_document
,_app
, or pages. And sometimes the implementation details have to match exactly the example otherwise the library behaves in a different way. We've seen this for the example with material-ui that has a flash of unstyled content if_document
is not implemented.There are also examples that are using
getInitialProps
where it's not always needed, one of the examples of this is the redux example. It currently requires havinggetInitialProps
which causes all pages to opt-out of automatic static rendering.To solve these issues and provide an overall better developer experience we intend to introduce a plugin system.
Plugins will be able to hook into specific lifecycle parts of Next.js.
For example, to execute a specific bit of code after hydration happens or to inject a script like Google Analytics into the body of every page.
The main goal for the plugin system will be to make you more productive while preserving the Next.js production performance characteristics.
One way we're trying to achieve this is that attached lifecycle methods are automatically code-split in a similar fashion as pages.
This new plugins system will provide you with the best developer experience when integrating libraries and we're excited to see the new ways of integrating and extending Next.js' functionality this system will enable.
Proposal
To provide a seamless plugin experience, Next.js will add built-in support for auto-detecting packages with the naming convention
@next/plugin-
,next-plugin-
, or@<scope>/next-plugin-
. We will also investigate allowing local plugins in aplugins
ornext-plugins
directory that don't require being installed.Each of these plugins will have a meta field named
nextjs
in theirpackage.json
that gives us some extra information about the plugin like the requiredenv
values it relies on.The only other required step for a plugin is to have a
src
directory with the lifecycle methods it wants to hook into.To start, we explored what it would take to create a Google Analytics plugin and a Sentry plugin. Creating these example plugins gave us insight into the lifecycle methods that were needed.
Currently exposed lifecycle methods
The naming scheme for lifecycle methods will be based on:
name
will be based on the lifecycle, potentially prefixed byon
if it's an event.The
context
will beclient
,server
orbuild
. It's appended at the end of the file name so that if a lifecycle is available on both client and server it will be sorted together in editors.Some of the initial methods that come to mind:
An example folder structure for one of these plugins would look like this
Following this structure allows Next.js to provide a zero-config experience for plugins. Not only that, but it also allows us to code split these methods efficiently, only loading the parts of a plugin that are necessary at a specific point. We can also detect which parts of a plugin are being less efficient since they are all their own file and warn users of this.
Providing information to plugins
Although we aim to make adding plugins to a project as simple as possible, some plugins still require some information like a
token
or anid
.To handle this, we allow you to provide these values using the current
env
config innext.config.js
and the values will be injected into the plugin where needed during build time.If a value listed in a plugin's
required-env
is missing in yournext.config.js
'senv
we will error and require you provide these values so that the plugin is able to function correctly. We may need to investigate name spacing these values to prevent collision between plugins.Beta Was this translation helpful? Give feedback.
All reactions