0% completed
The Parts of a Web Client
On This Page
Four Layers
The View
Client State
The Data Layer
The Platform Layer
Data Flows One Way
Where Business Rules Live
Using This in an Interview
Key Takeaways
Practice Questions
A web client is everything a product sends to the browser and runs there: the HTML, the styles, the JavaScript, and the data that this code holds while the page is open. For Basket, the web client is the whole store as the shopper sees it, from the search box to the live map of a courier on the way.
A web client of any size is easier to design when it is split into parts with separate jobs. Each part has one responsibility. It also has a short list of parts it may talk to, and a few things it must never do. When those rules are clear, a change to the checkout does not break the product list, and a new engineer can tell where a piece of code belongs.
What follows names the four layers of Basket's web client, the job of each one, and the direction in which data moves between them.
Four Layers
A layer is a group of code with one kind of job. Basket's client has four of them, and each one is described in its own section below.
| Layer | Its job | In Basket | It must never |
|---|---|---|---|
| View | Draw the current state and report what the shopper did | product card, search box, cart button | Send requests or decide prices |
| Client state | Hold the data the open page needs, in one agreed shape | the cart, the search text, the chosen filters | Know how the data arrived |
| Data layer | Talk to the server: requests, saved responses, live updates | the product request, the order tracking connection | Draw anything |
| Platform | Pick the screen for a URL, produce the HTML, build and deliver the files | routes, the build, the CDN | Hold the shopper's data |
Look at which layer touches the server's API: only the data layer. The view reads from the client state, and the client state is filled by the data layer. The platform layer sits under all three, because it decides which screen runs and how its files reach the browser.
The View
The view is the layer that turns the current state into what the shopper sees, and turns what the shopper does into events. It is made of components, small functions that each draw one piece of the screen from the data they are given. In React, the data that a component receives from its parent is called its props.
interface ProductCardProps { product: { id: string; name: string; priceCents: number; imageUrl: string } quantityInCart: number onAddToCart: (productId: string) => void }
The card receives a product and a number, and it reports a tap through onAddToCart. It does not send the request to the cart API, and it does not decide whether the product can be bought. Because of this, the same card can appear in several places. It works in the product list, in the search results, and in the "Buy again" row of the account page.
Client State
State is the data the page holds while it is open. Some of it belongs to one component only, such as whether a dropdown is open. Other parts are shared: the number of items in the cart appears in the header, on every product card, and on the checkout page.
Shared client state lives in a store, an object outside the components that holds the shared data, changes it only through named actions, and notifies every component that reads a changed value. An action is a plain description of something that happened, such as "item added: product p-118, quantity 1". Components send actions, and only the store changes the data.
This rule means there is one copy of the cart in the page. The header and the checkout read the same copy, so they cannot show two different counts.
The Data Layer
The data layer is the code that talks to the server on behalf of the rest of the client: it sends requests, keeps recent responses, and holds any live connection. In Basket it has three jobs:
- Requests. It sends the product list request, the search request and the "Place order" request, and it turns a failure into a value the state can hold, such as "failed, can retry".
- Saved responses. A cache is a stored copy of a response, kept so that the same data does not have to be downloaded again. When the shopper presses the back button, the data layer can return the 30 products it already has. Components may read a saved response from this cache, but only the data layer writes to it.
- Live updates. Order tracking needs news from the server while the page is open. The data layer opens one connection, reopens it when it drops, and turns each message into an action for the store.
Keeping server calls out of the components puts retries, login headers and the response format in one place. When the API moves to a new version, the change is made in the data layer and no component changes.
The Platform Layer
The platform layer is the code and tooling that decide which screen runs for a URL and how its files reach the browser. It has three jobs.
The router maps a URL to a screen, so /products/p-118 opens the product page for product p-118. Because the screen comes from the URL, the back button works and a shared link opens the same product.
The platform also decides where rendering happens for each route: on the server, in the browser, or partly in each. Basket can make that choice route by route, because the product page and the checkout have different needs.
A build step turns the source code into the files the browser downloads. Its main output is a few bundles, files that each combine many source modules, and each file name includes a short hash of that file's content. A CDN (content delivery network) is a set of servers in many cities that keep copies of those files, so a shopper downloads them from a server nearby. A new release produces new file names, which lets the CDN keep each file for a long time and lets old tabs find the files they still need.
Data Flows One Way
In Basket's client, data moves in one direction around a loop. Values travel from the store down to the components, and changes travel back to the store only as actions. This rule is called one-way data flow.
Here is the loop for one tap on "Add to cart":
- The shopper taps the button, and the product card calls
onAddToCart("p-118"). - The
onAddToCarthandler asks the data layer to add the item, and the data layer sends the request to the cart API. - The server checks the stock and the limits, then answers with the updated cart.
- The data layer turns that answer into an action, and the store saves the new cart.
- The store notifies the header badge and the product card, and both draw the new count.
Notice that no arrow goes from the view straight to the store's data, and no arrow goes from the server straight to a component. Every change enters the store through an action, so a wrong cart count has a small number of places to check.
The alternative is to let components change shared data directly. If the header and the checkout can both edit the count, the value the shopper sees depends on which edit ran last, and that order can change between two clicks.
Where Business Rules Live
A business rule is a rule the store sets about its products and orders, such as "at most 6 of a discounted item per order" or "free delivery for orders over 50 dollars".
The server owns every rule that affects money, stock or permissions, because the shopper can read and change any code that runs in the browser. The client may repeat a rule to give fast feedback, for example by disabling the plus button at 6. The server still checks the limit again when the request arrives.
Inside the client, a repeated rule belongs in a plain function in the state or data layer, such as one function that counts the items in a cart. The header and the checkout then call the same function. A rule copied into two components stops matching the first time one copy is edited and the other is not.
Using This in an Interview
Draw the four layers before any detail, and say what each one owns. For example: "Components only draw and report events. The store holds shared state, the data layer owns every request and the live connection, and the server checks every price again."
Two follow-up questions often come next:
- "Where would you put retry logic for a failed request, and why there?"
- "The header shows 3 items and the checkout shows 2. How do you find the cause?"
Key Takeaways
- A web client is everything that runs in the browser, and it is easier to change when each part has one job.
- The view draws state and reports events; it never sends requests or decides prices.
- Client state that several components need lives in one store and changes only through actions.
- The data layer is the only part that calls the server's API, so retries, caching and live connections live in one place.
- The platform layer maps URLs to screens, chooses where rendering happens, and builds and delivers the files.
- Data flows one way, and every rule about money, stock or permissions is checked again on the server.
These four layers give a design a place for every decision. The next lesson, Requirements for a Frontend Design, turns to the promises Basket makes to its shoppers, which decide how much each layer has to do.
Practice Questions
Try each question first, then open the answer.
1. A teammate makes each product card fetch its own details from /api/products/{id}. The list shows 30 cards. What goes wrong, and which layer should own the request?
The page sends 30 requests instead of one, and the data layer should own the request. Each card now handles its own errors, retries and login headers, so the same logic is repeated 30 times. The card also cannot be reused in places that already have the product data. The data layer should fetch the page of 30 products once, and the cards should receive them as props.
</details>2. Which layer owns each of these jobs in Basket? (a) Opening the tracking page for /orders/5512. (b) The number of items in the cart. (c) Reconnecting after the courier updates stop. (d) Graying out "Add to cart" when an item is sold out.
(a) Platform, (b) client state, (c) data layer, (d) view. The router in the platform layer maps the URL to the tracking screen. The cart count is shared state in the store. The live connection belongs to the data layer. The view draws the grayed-out button, but it reads "sold out" from the data it was given instead of deciding it.
</details>3. A shopper changes a product's price to 0.01 in the browser's developer tools and then places the order. What stops the order going through at that price?
<details> <summary>Show answer</summary>The server works out the price again and ignores the price shown in the browser. Any value in the page can be changed by the shopper, so a price in the client is only for display. The order request should send product ids and quantities, and the server should compute the total from its own data.
</details>4. The header shows 3 items in the cart, but the checkout page shows 2. Basket uses one-way data flow with a single store. Where do you look first?
<details> <summary>Show answer</summary>Look for a component that keeps its own copy of the cart instead of reading the store. With one store, two components that read it cannot disagree. A mismatch usually means one of them copied the count into its own local state once and never updated it. The fix is to make both read the same value from the store.
</details>5. Basket's build puts a hash of each file's content into the file name. How does that help the CDN and the tabs that stay open after a release?
<details> <summary>Show answer</summary>A file with a given name never changes, so it can be kept for a long time, and old names can stay available. A new release produces new names, so the CDN never serves a stale file under a current name. Because the old files are separate, they can stay on the CDN for a while, and the old tabs described in Frontend System Design vs Backend System Design can still load them.
</details>Reading Progress
0%
On This Page
Four Layers
The View
Client State
The Data Layer
The Platform Layer
Data Flows One Way
Where Business Rules Live
Using This in an Interview
Key Takeaways
Practice Questions