Nx + Next.js = ❤

Rate this content
Bookmark

Nx is a next generation suite of build tools. Next.js is state-of-the-art framework for building web applications. Together they bring order to the chaos of building multiple web applications across many teams.


This talk is a quick rundown of how Nx can help you maintain multiple Next.js apps, with multiple teams, while sharing components and libraries for consistent user experience.

9 min
17 Jun, 2021

Video Summary and Transcription

Hey everybody, my name is Adam Ellibert, aka Big AB, and I am a core contributor on a project called NX. Today, I'm going to go through a whirlwind tour of a really cool way to do web development using NX. NX is a suite of dev tools to improve developer experience, and it's extensible, plugin-based, and open-source. I'll show you some of the commands that NX provides, such as creating an NX workspace, generating schematics, serving as a dev server, building applications, and running tests. NX also has a variety of plugins, including the Next.js plugin. And if you're not comfortable with command-line interfaces, there's a GUI available for you. I'm going to create an NX workspace and choose Next.js as my first application. Then I'll create another app called Uber for Star and add a dashboard page. I'll also create a shared components library and configure storybook for it. Finally, I'll use NxServe to serve my Uber 4 app. This setup includes a splash page, a dashboard page, and a design system using storybook. We've got shared components for our marketing website and Uber 4 app. We also have end-to-end directories for Cypress tests and jest for unit tests. Our Uber 4 app is similar to any other Next.js app with pages and a nested dashboard page. We import shared components by prefixing them with the org name and library name. NX handles dependencies, ensuring everyone is on the same version and allows scoping imports to the org. Computation caching optimizes test and build runs by caching results that are unaffected by code changes. NX brings easy application creation with consistent commands and shared library extraction. Generators save time and enforce best practices. NX provides modern tools like Cypress, Storybook, Prettier, ESLint, and Next.js. The Next.js plugin creates and configures Next.js applications, serving them in dev mode and building production-ready and static apps. Visit nx.dev for more information.

Available in Español

1. Introduction to NX and Next.js

Short description:

Hey everybody, my name is Adam Ellibert, aka Big AB, and I am a core contributor on a project called NX. Today, I'm going to go through a whirlwind tour of a really cool way to do web development using NX. NX is a suite of dev tools to improve developer experience, and it's extensible, plugin-based, and open-source. I'll show you some of the commands that NX provides, such as creating an NX workspace, generating schematics, serving as a dev server, building applications, and running tests. NX also has a variety of plugins, including the Next.js plugin. And if you're not comfortable with command-line interfaces, there's a GUI available for you.

ReactJS 1.0 ReactJS 1.0 Presentation ReactJS 1.0 Presentation 1.0 Dev Developer ReactJS 1.0 Developer 1.0 Presentation 2.0 Developer ReactJS 1.0 Presentation 2.0 Developer ReactJS 1.0 Presentation 2.0 Developer ReactJS 1.0 Presentation 2.0 Developer ReactJS 1.0 Developer 2.0 Developer

Hey everybody, my name is Adam Ellibert, aka Big AB, and I am a core contributor on a project called NX. And today, I'm going to go through a whirlwind tour of a really cool way to do web development. So you've probably already heard of a project called Next.js. It's that cool framework for making Jamstack sites either statically generated or SSR, or that awesome, sweet hybrid approach, well, of the two. But what you haven't probably heard of is NX. And so what is NX? OK, it's basically a suite of dev tools to improve developer experience. And it's a CLI package on npm, so you can do npm install nx. You'll get all these cool commands that you can do to help out your development. It's extensible because it's plugin-based. So you can basically make it do whatever you want, and it's open-source. It's MIT licensed. So if you feel like you want to contribute or you need a fork or something, it's all completely open source. I'm going to show you a bunch of commands that will help out your development that NX gives you. So the first one is create NX workspace. If you're used to create react app, this is really our Create Next app, this is very familiar, this is going to create an NX workspace, which in our world is a monorepo. It's where your organization is going to hold all your applications and all your shared libraries in one space, and that'll really help development. I'll explain why further. Then it's got this NX generate. So the concept of schematics comes from Angular, but NX lets you use it across any framework, including Next.js and React. NX serve is basically like a dev server. And it uses something called builders, also from Angular, but again, sharing out this. And schematics and builders come from plugins, which is sort of the heart of all of what NX does. Then there's an NX build, which is basically taking an application and compiling it into some sort of artifact. NX test is for running whatever unit test runner you have configured, and we have the same thing for end-to-end tests. So now, I said that NX is at the heart of plugins, and we have a lot of them. You can run NX lists, and see a whole list of them. There's official ones, and then there's community ones. And the one I'm going to focus on today is the Next.js plugin for NX. And in just eight commands, I'm going to show you something awesome, and how easy it is to get started to get rolling. If any of these commands scare you, I do want to mention that there is a GUI that you can use, that you don't have to memorize commands.

2. Creating an NX Workspace and Setting Up Next.js

Short description:

I'm going to create an NX workspace and choose Next.js as my first application. Then I'll create another app called Uber for Star and add a dashboard page. I'll also create a shared components library and configure storybook for it. Finally, I'll use NxServe to serve my Uber 4 app. This setup includes a splash page, a dashboard page, and a design system using storybook.

They're much easier to discover and see what you can do. And it's a plugin for VS code, called NX Console.

So to start off, I'm just going to create an NX workspace. I'm going to say, create npx, create NX workspace, and then the organization name. This could be whatever organization. And again, you're going to have all your applications and your libraries in one place. So name that appropriately.

And then it's going to ask you, oh, what do you want to put the first application there? What kind of application do you want it to be? And I'm going to choose Next.js, because it's awesome. This is going to be, let's just say, my marketing website, or something that's going to be my first application.

Now don't get scared about this thing. I'm going to run all through these, and then I'm going to show you what I made. So these commands are basically just, I'm going to create another application. I'm going to do nxg, which is short for generate. I'm going to use the Next.js plugin to create another app called Uber for Star, because I don't know what I'm making, it's just a demo here. And then I'm going to use the Next.js plugin again to add a page, which is a Next.js concept, to that Uber 4, and I'm going to call it this dashboard.

Then I'm going to create a shared library, which is going to be shared components, which both the marketing site and the Uber 4 Star are going to use. Then I'm going to add some components to that library. Then I'm going to use our storybook plugin to configure storybook for that shared components library to get sort of a design system up and running. And then I'm just going to use NxServe to serve my Uber 4 app. And let me show you what kind of that gives me.

So this is sort of a splash page that you're going to get on here. You'll note we added that dashboard page, so I've got that dashboard page. It's empty now, but it's basically, it's all set up for you. We've got storybook all set up for you. And this is great, storybook has like a ton of things you can add. It's sandbox development for each of the components, as well as like, you can add documentation and a whole design system here. Let me show you some of the code real quick. We've got, you know, here's our workspace. And like I said, there's sort of a split of our products into apps and libs. And apps is your applications, libs is your shared libraries.

3. Shared Components and Dependency Handling in NX

Short description:

We've got shared components for our marketing website and Uber 4 app. We also have end-to-end directories for Cypress tests and jest for unit tests. Our Uber 4 app is similar to any other Next.js app with pages and a nested dashboard page. We import shared components by prefixing them with the org name and library name. NX handles dependencies, ensuring everyone is on the same version and allows scoping imports to the org. Computation caching optimizes test and build runs by caching results that are unaffected by code changes.

We've got that shared components thing in here. And this is where those components live. This is the source code for those components. And in our apps, so we've got our marketing website and our Uber 4 app, you know, whatever that is. But you might also notice here, we've got these end to end directories as well. And these are for writing Cypress test. These are all configured for you're all set up. So you can just start writing end to end tests, which is a great experience in a great way. Mixed with also we have jest for writing unit tests, a fantastic way to work.

And so our Uber 4 app is just like any other X Next.js app that you might expect. It's got pages public, whatever. And in our pages, you can see there's that dashboard page we created and you can have this nested if you're familiar with Next.js. And I just wanted to show you. So we're gonna use these shared components inside our index page here. And the way you import them is just, you know, we're just sort of prefixing them with this scoped. My org, that's what I named the org. You can name it whatever you want. And then just the name of the library. And that's how easy it is to share code across the applications and a breakout code into different shared things.

Now, one of the main parts of an NX repo is the fact that we've got one package Jason in the whole thing. It's at the root. And the reason this is important, is because NX sort of handles all these dependencies for you. And why that's good is because this will protect you from like a whole swath of problems that mono repos can sometimes run into where you've got one project that is lagging behind, it's versions behind, it's hard to update, this department never ever updates. So, there are like six versions behind and it's really frustrating. You sort of get to ignore all that because NX is gonna make sure that everybody's moving together, everybody's upgrading together cause it's one set of dependencies. It also handles that import syntax that I talked about where you can sort of scope it to the org making it really easy to share code. And then because NX handles these dependencies, you get to sort of have a way to sort of compensate for some of the problems that mono repos can have. One major thing is once you get into like hundreds of shared libraries and hundreds of apps, you don't wanna run them all every time you wanna run your tests or wanna run your build. And so what NX does, it's got this thing called computation caching where after you've run a test or a build, it sort of caches the results and puts them somewhere. And now the next time you run that, if it knows that it doesn't have to change because none of this code could possibly affect it, it just hands you back that cash.

4. NX Benefits and Next.js Plugin

Short description:

NX brings easy application creation with consistent commands and shared library extraction. Generators save time and enforce best practices. NX provides modern tools like Cypress, Storybook, Prettier, ESLint, and Next.js. The Next.js plugin creates and configures Next.js applications, serving them in dev mode and building production-ready and static apps. Visit nx.dev for more information.

It's like, oh, I don't need to change. I don't need to rerun. Here's the results from it, making it super fast. And then we can even take that one step further with an offering we have called NX Cloud, which basically takes those computations and it puts them in the cloud. So if I run a build that takes seven minutes on my machine and then my coworker immediately runs that same build, theirs will be almost instant because it'll just download the result from the cache and display it immediately or put it into the build or whatever it needs to be, super cool stuff.

And so what NX brings to the table is it allows you to sort of easily create these applications with all these commands that are easy and consistent amongst all your teams. It makes extracting shared libraries super easy because everybody is moving together with their dependencies and you can always sort of make sure that you're not gonna break anything by my update because everything's always going together. And the generators, like I said, to save a bunch of time and also keep things consistent and a way to reinforce best practices, both with generators, as well as ESLint rules for one, across all your code bases. It's good stuff and same CLI and stuff. It has all these modern tools, Cypress, Storybook, Prettier ESLint built-in because of the plugins.

And of course, Next.js, because of our Next.js plugin. And our Next.js plugin in particular can create and configure Next.js applications, which is super cool. It serves them in the Next.js dev mode. It can create pages and components and it can build production-ready Next.js apps ready to deploy, as well as, of course, Next.js feature of completely static apps. So if that interests you at all, this was crazy fast, but you wanna learn more, go to nx.dev, the easiest URL in the world to remember and go through one of the tutorials, check out what we can do, and of course, try out our Next.js thing. I'll answer any questions after this.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

React Advanced Conference 2022React Advanced Conference 2022
25 min
A Guide to React Rendering Behavior
Top Content
React is a library for "rendering" UI from components, but many users find themselves confused about how React rendering actually works. What do terms like "rendering", "reconciliation", "Fibers", and "committing" actually mean? When do renders happen? How does Context affect rendering, and how do libraries like Redux cause updates? In this talk, we'll clear up the confusion and provide a solid foundation for understanding when, why, and how React renders. We'll look at: - What "rendering" actually is - How React queues renders and the standard rendering behavior - How keys and component types are used in rendering - Techniques for optimizing render performance - How context usage affects rendering behavior| - How external libraries tie into React rendering
React Summit Remote Edition 2021React Summit Remote Edition 2021
33 min
Building Better Websites with Remix
Top Content
Remix is a new web framework from the creators of React Router that helps you build better, faster websites through a solid understanding of web fundamentals. Remix takes care of the heavy lifting like server rendering, code splitting, prefetching, and navigation and leaves you with the fun part: building something awesome!
React Advanced Conference 2023React Advanced Conference 2023
33 min
React Compiler - Understanding Idiomatic React (React Forget)
React provides a contract to developers- uphold certain rules, and React can efficiently and correctly update the UI. In this talk we'll explore these rules in depth, understanding the reasoning behind them and how they unlock new directions such as automatic memoization. 
React Advanced Conference 2022React Advanced Conference 2022
30 min
Using useEffect Effectively
Top Content
Can useEffect affect your codebase negatively? From fetching data to fighting with imperative APIs, side effects are one of the biggest sources of frustration in web app development. And let’s be honest, putting everything in useEffect hooks doesn’t help much. In this talk, we'll demystify the useEffect hook and get a better understanding of when (and when not) to use it, as well as discover how declarative effects can make effect management more maintainable in even the most complex React apps.
React Summit 2022React Summit 2022
20 min
Routing in React 18 and Beyond
Top Content
Concurrent React and Server Components are changing the way we think about routing, rendering, and fetching in web applications. Next.js recently shared part of its vision to help developers adopt these new React features and take advantage of the benefits they unlock.In this talk, we’ll explore the past, present and future of routing in front-end applications and discuss how new features in React and Next.js can help us architect more performant and feature-rich applications.
React Advanced Conference 2021React Advanced Conference 2021
27 min
(Easier) Interactive Data Visualization in React
Top Content
If you’re building a dashboard, analytics platform, or any web app where you need to give your users insight into their data, you need beautiful, custom, interactive data visualizations in your React app. But building visualizations hand with a low-level library like D3 can be a huge headache, involving lots of wheel-reinventing. In this talk, we’ll see how data viz development can get so much easier thanks to tools like Plot, a high-level dataviz library for quick & easy charting, and Observable, a reactive dataviz prototyping environment, both from the creator of D3. Through live coding examples we’ll explore how React refs let us delegate DOM manipulation for our data visualizations, and how Observable’s embedding functionality lets us easily repurpose community-built visualizations for our own data & use cases. By the end of this talk we’ll know how to get a beautiful, customized, interactive data visualization into our apps with a fraction of the time & effort!

Workshops on related topic

React Summit 2023React Summit 2023
170 min
React Performance Debugging Masterclass
Featured WorkshopFree
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)
React Advanced Conference 2021React Advanced Conference 2021
132 min
Concurrent Rendering Adventures in React 18
Top Content
Featured WorkshopFree
With the release of React 18 we finally get the long awaited concurrent rendering. But how is that going to affect your application? What are the benefits of concurrent rendering in React? What do you need to do to switch to concurrent rendering when you upgrade to React 18? And what if you don’t want or can’t use concurrent rendering yet?

There are some behavior changes you need to be aware of! In this workshop we will cover all of those subjects and more.

Join me with your laptop in this interactive workshop. You will see how easy it is to switch to concurrent rendering in your React application. You will learn all about concurrent rendering, SuspenseList, the startTransition API and more.
React Summit Remote Edition 2021React Summit Remote Edition 2021
177 min
React Hooks Tips Only the Pros Know
Top Content
Featured Workshop
The addition of the hooks API to React was quite a major change. Before hooks most components had to be class based. Now, with hooks, these are often much simpler functional components. Hooks can be really simple to use. Almost deceptively simple. Because there are still plenty of ways you can mess up with hooks. And it often turns out there are many ways where you can improve your components a better understanding of how each React hook can be used.You will learn all about the pros and cons of the various hooks. You will learn when to use useState() versus useReducer(). We will look at using useContext() efficiently. You will see when to use useLayoutEffect() and when useEffect() is better.
React Advanced Conference 2021React Advanced Conference 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.
React Advanced Conference 2021React Advanced Conference 2021
145 min
Web3 Workshop - Building Your First Dapp
Top Content
Featured WorkshopFree
In this workshop, you'll learn how to build your first full stack dapp on the Ethereum blockchain, reading and writing data to the network, and connecting a front end application to the contract you've deployed. By the end of the workshop, you'll understand how to set up a full stack development environment, run a local node, and interact with any smart contract using React, HardHat, and Ethers.js.
React Summit 2023React Summit 2023
151 min
Designing Effective Tests With React Testing Library
Featured Workshop
React Testing Library is a great framework for React component tests because there are a lot of questions it answers for you, so you don’t need to worry about those questions. But that doesn’t mean testing is easy. There are still a lot of questions you have to figure out for yourself: How many component tests should you write vs end-to-end tests or lower-level unit tests? How can you test a certain line of code that is tricky to test? And what in the world are you supposed to do about that persistent act() warning?
In this three-hour workshop we’ll introduce React Testing Library along with a mental model for how to think about designing your component tests. This mental model will help you see how to test each bit of logic, whether or not to mock dependencies, and will help improve the design of your components. You’ll walk away with the tools, techniques, and principles you need to implement low-cost, high-value component tests.
Table of contents- The different kinds of React application tests, and where component tests fit in- A mental model for thinking about the inputs and outputs of the components you test- Options for selecting DOM elements to verify and interact with them- The value of mocks and why they shouldn’t be avoided- The challenges with asynchrony in RTL tests and how to handle them
Prerequisites- Familiarity with building applications with React- Basic experience writing automated tests with Jest or another unit testing framework- You do not need any experience with React Testing Library- Machine setup: Node LTS, Yarn