A Modern Approach to Next.js Development

Build composable and modernised Next.js apps with Bit!

Leimonis Konstantinos
Bits and Pieces

--

Traditional development approaches and tools encounter difficulties in achieving effective component isolation. As applications become increasingly complex, the management and reuse of components across different projects pose growing challenges.

This is where Bit steps in to offer a modern and efficient solution.

Bit is a build system designed to create composable software. Using Bit, you can build components as independent pieces of code shared and collaborated via remote component hosting.

Understanding App Composability

Composability is a key concept in software development, where applications are constructed using loosely coupled and reusable components. This approach allows for the flexible rearrangement and reuse of elements to generate new features and projects.

Imagine having your own personalized toolkit of finely crafted components that can be easily integrated into creating a nextjs app. These components, encapsulated with distinctive logic and styles, create a cohesive and modular architecture. Like Lego, these components can be combined seamlessly to create intricate and feature-rich applications.

Example: a dependency graph of independent, reusable (Bit) Next.js and React components

This method offers a plethora of benefits, including:

  • Enhanced maintainability: Complex applications become significantly easier to manage and update when composed of independent components.
  • Improved reusability: Reusable components can be shared across different applications, saving time and effort and promoting consistency.
  • Specialization and collaboration: Each team can specialize in specific parts of the application development (user management, APIs, forms, etc.) with a clear scope and share their components with other teams.
  • Better testability: Independent and smaller components are more straightforward to test individually, leading to a more robust and reliable application.

Quick start on using Next.js with Bit

Integrating Bit with Next.js project is a simple and easy process.

Step 1: Install Bit in your React project

First, use Bit’s version manager (bvm) to install Bit globally and then initialize a workspace with Next.js.

npx @teambit/bvm install
bit new nextjs my-workspace --env frontend.nextjs/nextjs-env --empty

Step 2: Creating and tracking components

Once Bit is set up, use the Bit CLI to create and track components.

bit create react ui/my-button

This will create a Button component. You can build your component and code using standard React and Next.js syntax. Once you’re ready to ship your component onto the world, you can run the command:

bit snap -m "create button component"

Sharing components is where Bit shines the most. Push your components to bit.cloud to make them accessible to your team or the broader community.

bit export

If necessary, you can also publish your components to npm’s registry instead of Bit’s registry.

Step 4: Consuming Bit components in Next.js applications

Now that your components are on bit.cloud, let’s explore how to consume them in other Next.js projects.

Create a Next.js application

In your workspace, create a new Next.js application:

bit create nextjs apps/my-nextjs-app

Run the app locally:

bit run my-nextjs-app

Alternatively, explore your app as a component in the workspace UI:

bit start

Your app component should look something like this:

This application is also a component. Unlike our button component, it can be deployed to cloud providers like Netlify and Vercel.

Check out the deployed application on Vercel.

Importing Bit components

Components can be installed like regular Node packages or imported with their source files to be developed and updated.

As shown above, you can consume these components like any other package. If the new Next.js application is in a different workspace, you first need to install it:

bit install @my-org/my-scope.ui.my-button

Next, you can import it like you’d typically would and continue using it:

import { MyButton } from '@my-org/my-scope.ui.my-button';

The benefits of using Bit for Next.js Development

There are many benefits of using Next.js with Bit. For a better understanding, let’s look at them in detail.

Bit components and Next.js components

In Next.js, a component is a class that contains the logic and the template of a specific part of your application.

Bit components are more like modular repositories. In the context of Next.js, their source code and artifacts can be considered a React library. It is a collection of files that are related to a specific functionality. This means that a Bit component can contain multiple React components and other files such as services, directives, and stylesheets.

Think of Bit components as packages. Each component will have its own package.json file and be distributed with its package name.

Component-centric approach

Bit is a modern development tool that treats components as first-class citizens, making them easy to manage, organize, and share across projects.

Instead of dealing with cumbersome monorepos, you can focus on building and maintaining independent pieces of functionality.

Each Bit component will be automatically compiled and available in the node modules of your workspace. That means that you can import them like any other package.

This simplifies development because you do not need to build your React libraries before you run your app; everything is already set up for you.

Isolation and dependency management

Bit isolates components and manages their dependencies seamlessly, ensuring consistent behavior across different applications.

Bit keeps track of the dependency tree and can understand the components that rely on each other. It parses the component’s source code to delete dependencies and generates the package.json file automatically. It also categorizes dependencies as runtime, peer, and dev dependencies.

You do not need to worry about manually creating and maintaining your library package file. Of course, you can still change it manually if you have any specific requirements in terms of dependencies.

Each component is then versioned independently, providing granular control over updates and changes. You do not need to wait for large-scale releases to push bug fixes and new features.

Bit’s collaboration and sharing capabilities

Bit facilitates developers’ collaboration and sharing of components by providing a centralized hub for your components. The bit.cloud platform is a repository where developers can discover, share, and collaborate on components. You can set up different levels of access to your organization’s code, and even non-developers can follow along and interact with the progress of your projects, providing valuable feedback.

This means you can easily find out if your organization has already created the necessary component instead of reinventing the wheel. You can check its code, API, and documentation to see if it meets your expectations. You can also verify if it is compatible with your current version of Next.js. If not, you can submit a change request or fork it to gain time.

Effortless component documentation

Documentation is often an overlooked aspect of software development. Bit, however, makes it easy to generate and maintain documentation for individual components. This enhances collaboration and becomes a valuable resource for developers using shared components.

Besides markdown documentation, you can also create compositions that simulate components in different variations for testing, visualization, and discoverability purposes. This is similar to other solutions like Storybook, except that it is already integrated, so you do not need to set it up.

Advanced component testing

Testing is a critical aspect of software development, and Bit simplifies the process by enabling developers to test components in isolation and integrate them into your applications.

When working on packages imported into different applications, a monorepo was previously the only fast solution to test whether your code changes were functional. But with Bit, you get the advantages of a mono repository while keeping the versatility of independent components. You can import everything in the same workspace locally to simulate a mono repository, but then every component is individually packaged and versioned.

Using Bit’s CI, named Ripple CI, you can ensure that your changes do not break your components’ dependents, as they can be automatically tested against the code you pushed to bit.cloud.

Streamlining continuous integration with Ripple CI

Bit utilizes Ripple CI as its default CI, offering a highly optimized CI experience specifically designed for component-driven software. Ripple CI provides several benefits for Next.js development:

  • Parallelized component builds: Ripple CI parallelizes component builds, significantly reducing build times and improving development efficiency.
  • Efficient builds with optimized component dependencies: Ripple CI only builds affected components and their dependencies, ensuring efficient use of resources and optimizing build performance.
  • Faster feedback loops with incremental component build: Ripple CI provides faster feedback loops by incrementally building components as they change, allowing developers to identify and address issues early on.
  • Automatic propagation of dependent component builds: Ripple CI automatically propagates the builds of dependent components, including those not maintained in your workspace, ensuring that the entire application is always up-to-date. You can easily detect if your modifications contain bugs or breaking changes.
  • Out-of-the-box configuration: Ripple CI is automatically integrated with Bit, requiring no additional configuration, thus allowing developers to focus on their code rather than spending time fine-tuning CI settings.

Basically, Ripple CI transforms all your organization’s code into a big monorepo, where you see the direct impact of your Bit components and how they interact with the other components and applications that depend on them.

Conclusion

Bit and Ripple CI have drastically improved how to approach Next.js development. It introduces a modern and efficient process for building composable applications. By embracing app composability, developers can create more maintainable, reusable, and testable applications while enhancing collaboration and productivity.

With its powerful features, seamless integration with Next.js, and out-of-the-box support for Ripple CI, Bit is the ideal solution for developers seeking to elevate their NExt.js development skills and create the best web applications.

If you want to explore a sample application with Bit and Next.js, feel free to explore this scope in Bit Cloud.

I hope you have found this useful! Thank you for reading!

--

--