Qwik Compared to Next.js: Choosing the Future Framework for Web Development

2024-05-14 10:41:30

Qwik, as an innovative web development framework, has revolutionized traditional development experience. Through its unique “resumability” design principle, it has gained widespread recognition and favor. After assessing the development experience, the ability to integrate with the ecosystem, and the forward-thinking perspective, Qwik has become the preferred framework for many developers, especially those seeking to be

Qwik, as an innovative web development framework, has revolutionized traditional development experience. Through its unique “resumability” design principle, it has gained widespread recognition and favor. After assessing the development experience, the ability to integrate with the ecosystem, and the forward-thinking perspective, Qwik has become the preferred framework for many developers, especially those seeking to be truly at the forefront of web development technology.

Looking back at the journey of front-end development, it started from basic JavaScript and jQuery, went through numerous frameworks and libraries, and eventually led to the emergence of React. Since its introduction, React has won the favor of engineers with its intuitive design and widespread community support. However, despite React’s unshakable position, the emergence of Qwik has still attracted widespread attention in the development community.

The core concept of the Qwik framework, “resumability,” means that it can execute a program to a certain stage on the server, pause, and then resume execution on the client-side without needing to download the entire application logic. This is fundamentally different from traditional front-end frameworks, which often require the page to be fully rendered and the necessary JavaScript downloaded before client-side interactions can take place.

In summary, Qwik solves the problem of client-server interaction in a resource-saving and highly efficient way. This design philosophy can bring unprecedented development efficiency and user experience, and that may be the reason why Qwik stands out in the hearts of many developers.

By default, all rendering is performed on the server and is only transferred to the client in very specific situations, through the use of specific functions like useVisibleTask$ and isBrowser. Normally, server-side rendering is carried out smoothly. This information is just the beginning; for more details on the unique aspects of the Qwik framework, please refer to the official documentation, as Qwik resolves many issues other frameworks have long attempted to address.

Compared to other frameworks, Qwik is emerging and has not been developed for a long time. Although the developer community’s discussion about it is not yet very enthusiastic, Qwik indeed has potential. Developers who first learned about Qwik at the All Things Open Conference may find it novel, and it is advisable to invest time in reading Qwik’s related documentation thoroughly, which will be worth the effort.

In contrast, there have been countless articles about Next.js, which is actually an important framework based on the React library, and has become the preferred choice for React development. The official documentation of Next.js describes it as such: “Next.js is a React framework for building full-stack web applications. Developers can use React components to build user interfaces while taking advantage of the additional functionality and optimizations provided by Next.js. Next.js abstracts and automatically configures the tools required by React, such as packaging, compiling, etc., at the lower level, allowing developers to spend more time focusing on building the application itself rather than on the configuration process.”

Comparison between Qwik and Next.js

Next, we will compare Qwik to Next.js from seven perspectives, so that we can understand in which areas each framework performs better, and you can make judgments based on what you believe are the most important features.

Server vs Client

Next.js has a clear distinction between server and client components. However, for the Qwik framework, this is almost a non-issue. Almost all content is rendered on the server by default, which I personally consider an advantage. In this comparison, the winner is Qwik.

Below is a code example from the Next.js official documentation:


// Next.js code
import SearchBar from './searchbar'; // SearchBar is a client component
import Logo from './logo'; // Logo is a server component
// Layout is by default a server component
export default function Layout({ children }: { children: React.ReactNode }) {
return (
<nav>
<Logo />
<SearchBar />
</nav>
<main>{children}</main>
)
}
// SearchBar component
'use client' export default function SearchBar() {
return <main>Search!</main>;
}
// Logo component
'use client' export default function Logo() {
return <main>Logo!</main>;
}

In the Qwik framework, there is no need to define so explicitly:


// Qwik code
import { component$ } from '@builder.io/qwik';
import SearchBar from './searchbar';
import Logo from './logo';
export default component$(() => {
return (
<nav>
<Logo />
<SearchBar />
</nav>
<slot />
)
});
// SearchBar.tsx
export default component$(() => {
return <main>Search!</main>;
});
// Logo.tsx
export default component$(() => {
return <main>Logo!</main>;
});

It can be seen that the code structures of both are very similar; in fact, they both use JSX syntax.

In the Qwik framework, there is no need to explicitly specify during development whether the application runs on the client or the server side; by default, all rendering starts from the server. This default setting greatly simplifies the development process, creating a better development experience. For developers who have used Next.js, deciding whether a component should render on the server side or execute on the client side is often a decision that requires careful consideration. But in Qwik, such choices are not necessary.

While Qwik also offers caching features, allowing developers to set the duration of the cache, it does not directly invalidate the cache as Next.js does. Whether this will affect the long-term success of Qwik remains to be seen. Although this may not necessarily be a serious problem in practice, foreseeably, it could become a potential concern for developers.

Winner: Next.js. Next.js provides a convenient method to invalidate the cache, as shown in the following example:

                // Next.js cache invalidation example code
                export default async function Page() {
                    const res = await fetch('https://...', { next: { tags: ['collection'] } })
                    const data = await res.json()
                    // ...
                }
                
'use server'
import { revalidateTag } from 'next/cache'
export default async function action() { revalidateTag('collection') }

Qwik, on the other hand, resolves the issue by re-executing all the routeLoader$s functions affecting the page when operations that could lead to data changes occur on the server side. While effective, this lacks the finer control mechanism.

Next.js and the React ecosystem are naturally compatible, with native integration from creation. In contrast, Qwik accesses the vast React ecosystem through the qwikify$ function. However, the official Qwik documentation recommends using it as a migration tool rather than a long-term solution. This is because React components wrapped by qwikify$ require separate rendering and hydration, which may affect performance. Nevertheless, Qwik offers much flexibility during the hydration process of React components. For example, you can instruct Qwik to start hydrating React components only when the browser is idle, and it provides many other control mechanisms.

Qwik also has a unique feature; it does not preload the React library but only loads it when rendering pages containing relevant React components and under certain conditions (such as when the component is visible on the page). This strategy helps ensure performance and loading speed.

Qwik offers developers more abundant control options compared to Next.js. Although qwikify$ is considered a migration tool, its effectiveness is not to be underestimated, and developers can adopt various strategies to solve or mitigate potential performance impacts. In this comparison, Qwik demonstrates superior performance.

When using Next.js, one point to note is that components that should only run on the client side can’t be used directly in server-side components. The solution is to wrap the third-party component inside another component and use the use client directive within that component. This is similar to Qwik, which, however, offers a higher degree of controllability. I particularly appreciate Qwik’s control over the hydration process, which allows developers to finely control this process based on different scenarios such as page loading, user idle, mouse hover, and other events. In comparison, Next.js’s control capability in this regard is more limited or completely lacking.

It’s also worth mentioning that at the time of writing this text, Qwik does not yet have a native chart library. In contrast, there are many more chart libraries available to developers based on React, sometimes even an abundance of choices.

Integrating modern chart libraries like Chart.js into the Qwik framework is a breeze, yet chart rendering is confined to the client-side, which does not fully unleash the potential of Qwik. We are aware of the importance of server-side rendering (SSR) for charts to enhance user experience. However, there is no dedicated chart library for Qwik that supports this yet. Although server-side rendering can be achieved using SVG chart libraries or writing SVG directly, there isn’t an official solution provided.

In comparison to React’s ecosystem, which already has chart libraries capable of server-side rendering, giving Next.js an advantage in this respect. However, when it comes to state management, Qwik offers native signal functionality with ease. For those familiar with React’s useState, using signals in Qwik is even more intuitive. Next.js has not yet provided a perfect signal solution, even though some users have tried to force Preact’s signal functionality into Next.js through “monkey patches,” but such attempts do not always work perfectly.

The clear winner is Qwik.

Next.js example code:


'use client'
function HomePage() {
  // ...
  const [likes, setLikes] = React.useState(0);
  function handleClick() {
    setLikes(likes + 1);
  }
  return (
    

      {/* ... */}
      
    

  );
}

Qwik example code:


export default component$(() => {
  // ...
  const likes = useSignal(0);
  return (
    

      {/* ... */}
      
    

  );
})

In Qwik, signals can even be passed as props to child components and easily modified without the need for callback functions as in React.

Qwik Parent.tsx example code:


export default component$(() => {
  // ...
  const likes = useSignal(0);
  return (
    

      
    

  );
})

Qwik Child.tsx example code:


type Props = {
  likes: Signal<number>;
};
export default component$<Props>((props) => {
  return (
    

      
    

  );
})

In terms of development servers, Qwik adopts Vite as its backbone, and Vite has increasingly become an important tool in front-end development.

Vite is equipped with a series of remarkable features, including efficient module handling, hot module reloading, and a built-in reverse proxy, among others. These powerful features provide strong reasons for choosing Vite, and detailed information can be referred to in relevant explanations. Compared to Next.js, which uses SWC and Turbo for building and development, Vite shows more obvious performance advantages.

Winner: Qwik. Mentioning server-side rendering, this topic was touched upon in the “Server versus Client” section, but here we will delve deeper. In terms of when server-side rendering components and the browser receives the first batch of HTML content generated by the framework, Next.js and Qwik may have different methods, but the task they complete is similar. While they may appear to be similar on the surface, different control mechanisms unique to each framework provide developers with different experiences.

As you flip through Next.js’s documentation on loading-ui-and-streaming, you’ll find that React Suspense can achieve “instant” loading and progressive rendering of UI components, which is undoubtedly an exceptional feature. Although Qwik does not directly offer such instant features, you can still achieve the same results through some methods. According to Next.js, “Even for server-centric routing, the navigation experience is instantaneous.”

Let’s go deeper and discuss the core of this issue. Suppose server-side rendered components, loading external resources such as product listings on the backend, may take some time (possibly up to 5 seconds). Before the product list is fully loaded and rendered into HTML, users will see no page content during this entire period. We all agree that this leads to a very poor user experience, giving the impression that the browser is not acting or not responding.

Next.js suggests using React Suspense and loading.js to deal with such situations. Suspense allows developers to use fallback components as a temporary substitute during the data loading process. Once the data is fully loaded, the actual components are then replaced. This feature not only provides an excellent development experience but also enhances user interactivity.

Qwik has a different strategy; it has a function called routeLoader$ that only executes on the server. The Promise has to be fulfilled before rendering the page, so for product components, routeLoader$ is called, and the Promise is resolved after 5 seconds, and then the page is rendered. Although Qwik does not have the concept of Suspense, you can still achieve similar effects by using server$ streaming. At the same time, this method gives you more control over the data loading process.

For example, you could load the first 10 products on the page first and then load the rest. While this is just a contrived scenario, it effectively demonstrates the potential advantages of certain technologies. An interesting discussion on GitHub about the Qwik framework shows how to use streaming to load data. Although implementing such actions in Qwik may seem more complicated, it shows an important advantage.

When comparing Next.js with Qwik, due to the more concise and intuitive development experience provided by Next.js through React Suspense, Next.js obviously takes the lead in terms of simplicity. Winner: Next.js. However, Qwik can also achieve similar functionalities and offers finer control methods, though it is not as smooth as Next.js.

My main reasons for choosing Qwik as a development tool are that Qwik offers a more outstanding development experience — in most cases, developers don’t need to manage server and client-side components. Qwik is more user-friendly for development. This doesn’t mean that Qwik has done excessive abstraction, but rather because of Qwik’s fundamental design philosophy that everything is initially server-side rendered and only client-side rendered when necessary. It can work automatically without the need to specifically denote client or server-side, making it more developer-friendly.

While the Qwik ecosystem is still in its infancy, you can take advantage of the large React ecosystem. Although hydration operations bring certains performance overhead, in real-world applications, this overhead is often negligible. Meanwhile, Next.js also faces the cost of hydration without any other options. However, in Qwik, developers have the ability to control when hydration happens, and eventually, they may rewrite React components into native Qwik components.

Regarding data flow management, Signals are superior to useState in React, and there seems to be little controversy on this point. If there is any controversy, it might stem from the discussion of whether RxJS is superior to Signals. The resumability of Qwik is another highlight, and this feature may become a basic requirement in future frameworks. Even technologies like React Server components, which perform similar operations by serializing data after rendering and transferring it to the client, require that all server component code be serializable, meaning you can’t use life cycle hooks like useEffect() or state.

All in all, although React Server Components are also moving in the right direction, the approach adopted by Qwik seems to have more advantages for the time being. However, this doesn’t mean that Qwik will definitely become the mainstream framework in the future, although its methods have solved many issues that other frameworks (like Next.js) need to mitigate. When using Next.js (or any React-based framework), by default, the data packet received by the browser increases linearly with the increase of third-party components. In Qwik, developers have more control, so this relationship is not simply linear.

Under normal circumstances, unless specifically stated otherwise, browsers do not pass on JavaScript. Taking a component that includes a chart library as an example, even if the page has already loaded the corresponding chart library, you can still control the timing of the library’s load. That is to say, you can achieve this effect: if the chart library is only used for a modal dialog, you can specify that the corresponding library is only loaded when the modal dialog is triggered. This is one of the great advantages of the Qwik framework.

Similar to Qwik, Next.js offers dynamic import capabilities, but its operations are not as straightforward as Qwik’s. Qwik framework can offer more advanced control; it can implement streaming server responses in client-side click events using async generators. For example, you can look at a specific example, from which you will discover that this is a surprising technique. React can also mimic the above behavior in Next.js through Server components, but because of the fundamental design support of Qwik, Next.js/React cannot replicate the exact effects of Qwik.

Qwik’s useTask Hook is somewhat similar to React’s useEffect Hook, but Qwik introduced Signals to simplify usage, making it more concise and clear-cut than the combined use of useEffect and useState in React, while also reducing boilerplate code and having more rational logic.

In summary, the Qwik framework is superior to Next.js in certain technical aspects. However, choosing either Next.js or Qwik is a wise decision. Both have detailed documentation, good development momentum, and have been widely applied in actual production environments. Although Qwik has shown stronger advantages in multiple technical fields, what truly excites me is the smoothness of the Qwik development experience. Not every framework or language can bring this kind of feeling, but coding with Qwik feels exceptionally wonderful each time.