DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • JavaScript Frameworks: The Past, the Present, and the Future
  • The Role of JavaScript Frameworks in Modern Web Development
  • Angular vs. Flutter for Web App Development: Know Which One Is Best?
  • Optimizing React Apps for Web Development: A Comprehensive Guide

Trending

  • The 7 Pillars of Meeting Design: Transforming Expensive Conversations into Decision Assets
  • Reactive Ops to Autonomous Infrastructure: How Agentic AI Is Redefining Modern DevOps
  • Why Your RAG Pipeline Will Fail Without an MCP Server
  • How Reactive Scaling Drains Your Cloud Budget Without Warning
  1. DZone
  2. Coding
  3. Frameworks
  4. How Qwik Works

How Qwik Works

Qwik claims the fastest front-end framework right now. It offers lightning-fast page load times, regardless of the size and complexity of the web application.

By 
Suthasan Jeganathan user avatar
Suthasan Jeganathan
·
Aug. 11, 23 · Opinion
Likes (3)
Comment
Save
Tweet
Share
5.5K Views

Join the DZone community and get the full member experience.

Join For Free

There are several so-called front-end frameworks in the web development context, and each has its own unique approach to handling speed and performance. Qwik is a front-end framework that adopted the 0 hydration or dehydration approach at an early stage, which could be popular for web application context in the future.

This article describes the key concepts of Qwik and how to get started.

Key Concepts

  1. Resumable and hydration
  2. Prefetching
  3. Optimizer  
  4. Qwikloader 
  5. Qwikcity

Hydration and Resumablity

Resumable can be linked to hydration, which means loading HTML first without JavaScript, then "hydrate" interactivity wherever needed.

The following are collectively known as the Hydration process, which happens on a server-side rendered (SSR) page loaded at the client:

  1. Listeners: Attach events on the DOM nodes to make the application interactive.
  2. Component tree: Building up an internal data structure representing the application component tree.
  3. State: Restore data, if any, from an SSR

HydrationQwik does not require hydration to resume an application on the client. All other frameworks' hydration replays all the application logic on the client. This is expensive in the perspective of page load time because,

  1. The frameworks have to download all of the component code associated with the page.
  2. The frameworks have to rebuild the listener location and the internal component tree.

Qwik instead pauses execution on the server and resumes execution on the client. That means the Qwik frame doesn't need to download components, its state, and the template for listeners. Instead, it executes using its own mechanism with QRL, Prefetching, Optimizer, and Qwikload.

Prefetching

Qwik does not download every file only when you click or trigger an event, but after doing the first load to show the page, the framework will start automatically downloading the code necessary for the web app using a service worker. A service worker is a script that runs independently in the background of the browser.

After loading static HTML,  JavaScript is downloaded and kept in the browser cache. This process is called Prefetching.

For example, consider the below piece of code in Qwik before rendering:

<button onClick$={() => (radus.value = randomValue())}>Radus28</button>


The code after rendering through SSR, the HTML can look like this:

JavaScript
 
<button
on:click="app_component_div_div_button_onclick_ysxw.js#app_component_div_div_button_onClick_YuUw[0]"
q:id="2"
>


The <Button has a peculiar attribute on:click, which is omitted by the browser. Qwik executes this using Qwikloader. This attribute value is called QRL or a Qwik URL.

Using Qwik URL, Qwikloader will understand what code to fetch and execute from the chunk generated by the optimizer.

Optimizer

Qwik's objective is to delay loading code for as long as possible. To do that, Qwik uses an Optimizer to re-arrange the code for lazy loading. The Optimizer is a code-level transformation that runs as part of the rollup.

Example:

JavaScript
 
export const Counter = component$(() => {
  const count = useSignal(0);
 
  return <button onClick$={() => count.value++}>{count.value}</button>;
});


Below are the transformations that the Optimizer applied to the code to enable lazy load.

JavaScript
 
const Counter = component(qrl('./chunk-a.js', 'Counter_onMount'));


chunk-a.js:
JavaScript
 
export const Counter_onMount = () => {
  const count = useSignal(0);
  return <button onClick$={qrl('./chunk-b.js', 'Counter_onClick', [count])}>{count.value}</button>;
};


chunk-b.js:
JavaScript
 
const Counter_onClick = () => {
  const [count] = useLexicalScope();
  return count.value++;
};


Qwikloader

The Qwik framework requires a tiny piece of JavaScript called Qwikloader, which loads at the beginning and knows how to download the rest of the application on an as-needed basis. 

Qwikloader is Smaller and faster as;

  • Small: about 1 kb minified.
  • Executes in less than 5ms, even on mobile devices (initial cost, not per event cost).

Qwikcity

The Qwikcity is a meta framework for Qwik framework, such as Next.js for react, Analog for Angular, Sveltekit for Svelte, and Nuxt.js for Vue.js

It comprised of :

  • Qwik — The framework itself
  • City —
    • Router
    • Middleware
    • Endpoints
    • Data fetching

Use cases:

  1. E-commerce sites
  2. Blogs
  3. CMS
  4. Sites that need layouts, routing, and data fetching

Getting Started

To get started locally, you will need

  • Node.js >= 16.8
  • A modern IDE (For example, Atom or VS Code)

Use your CLI:

npm create qwik@latest


The command above will prompt you to name the application and a few other steps:

npm start


Alternatively, yarn can be used.

Example:

yarn create qwik
yarn start


For further details, refer to the Getting Started Official document.

JavaScript Lazy loading Web development Framework

Opinions expressed by DZone contributors are their own.

Related

  • JavaScript Frameworks: The Past, the Present, and the Future
  • The Role of JavaScript Frameworks in Modern Web Development
  • Angular vs. Flutter for Web App Development: Know Which One Is Best?
  • Optimizing React Apps for Web Development: A Comprehensive Guide

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook