How to design a shopping site, from the listing to the cart (frontend)
The first sections are free. The deep dives, level expectations and quiz are part of Pro.
On this page
- The problem
- Clarifying questions
- The requirements menu
- Non-functional requirements
- Core entities
- The API
- The high-level design
- What breaks first
- The deep dives
- Entity fields
- From the listing to a product and back
- A first page that shows fast and can be found by search engines
- A cart that survives a closed tab
- Filters and a very long listing
- Variants
- What each level is expected to show
- The 30-second answer
- Quiz
The problem
The web client for a store: a listing with filters, a page for one product, and a cart, used mostly on phones. Shoppers go from the listing into a product and back many times, so the answer turns on where each piece of state lives. The filters belong in the address bar, so Back, a reload and a pasted link all bring the same view. The results and the scroll position belong in a client-side cache, so Back can draw at once. The cart belongs on the server, keyed by an id the browser remembers, shown optimistically and priced by the server. The first screen of the listing and the product page is drawn on the server, so a slow phone and a search engine both get the product and its price in the first response.
Clarifying questions
| Ask | Why it matters | A typical answer |
|---|---|---|
Do we have a server that can render HTML for us, or a static host and an API? | It decides whether the listing and the product page can arrive with the product already in them. Without a rendering server, the first screen waits for the code and then the data, and a crawler may index an empty shell. | Assume a server that can render pages and also serves the |
How big is the catalog, and how many filters does a listing carry? | It settles whether the browser can hold a category and filter it itself, and how much filter state the listing must keep and restore. | A catalog of 500,000 products at most, with thousands in a category. Filters sit in 10 to 30 sections, with multi-select, ranges and sort. |
Do guests get a cart, and must it survive a closed tab? The same cart on another device? | A cart for signed-in shoppers can live under the account. A guest cart that must survive a closed tab needs something the browser keeps between visits, and the same cart on a phone and a laptop pushes it onto the server. | Guests get a cart, and it must still be there the next day. Treat the same cart across devices as a follow-up, and payment as out of scope. |
What are the speed targets, and on what device? | The targets decide where pages are drawn and how images load. A target on a fast laptop and the same target on a slow phone lead to different designs. | Main content within 2.5 seconds on phones, judged at the 75th percentile; a tap answered within 200 milliseconds; layout shift of 0.1 or lower. |
Does the product page need live stock and price, or is a value from a build a few minutes old fine? | It decides whether product pages can be prebuilt as files or must be drawn per request, and whether a change after the page is drawn needs handling. | Price and stock come with the first response and should be current for that request. A live feed that pushes changes to open pages is out of scope. |
Numbered pages or endless scrolling in the listing? | They differ in how a shopper returns to a place and in how much the page holds in memory, and the prompt leaves the choice open. | Your call. Say what each costs and pick one for this store. |
Non-functional requirements
| Requirement | What it forces |
|---|---|
The main content shows within 2.5 seconds on a phone, at the 75th percentile | The largest element at the top of the product page, usually its first photo, has to be on screen early on a slow phone. In this design that means drawing the first screen on the server so the image is in the HTML, giving that image a high fetch priority, and judging the figure on real phone visits at the 75th percentile rather than on one fast laptop. |
A tap answers within 200 milliseconds | Add to cart and a filter tick must show a change on screen within the budget, and over a slow connection a round trip can take longer than that. So in this design the badge and the filter state update locally first and the request goes out after. It also argues against heavy work on the main thread during a tap, such as filtering a whole category in the browser. |
Listing and product pages can be read by search engines | Crawlers can run scripts, with limits, so a client-drawn shell may be indexed incompletely. In this design the server sends HTML that holds the products, prices and names, each listing view has a stable URL, and filtered URLs carry a canonical link, a signal search engines use to fold duplicates into one page. |
Layout shift stays at 0.1 or less while images load | Each image carries its width and height, or a reserved aspect ratio, so the browser allots space before the bytes arrive. Late sections such as recommendations get a reserved height too. |
Usable with a keyboard and a screen reader | Filter sections are fieldsets with legends, a live region announces the result count after a change and the cart total after an update, and add to cart is a real button with a readable label. |
Core entities
| Entity | What it holds |
|---|---|
Product summary | What a listing card holds, and what the product page can draw before its detail arrives. |
Product detail | The summary plus what the product page needs: photos, options and the description. |
Listing view | The one object that describes what the listing shows; the URL is its written form. |
Cart | The cart as the server last sent it, plus what the browser has changed since. |
Cart line | One product and option in the cart, with the quantity the shopper wants. |
The API
| Endpoint | Notes |
|---|---|
| One request for a page of results and the facet counts that go with it, so the grid and the filter counts agree. The parameters are the listing view as written in the URL. On the first visit the server draws the page from the same call. |
| Fetched when a product opens, after the page has drawn from the summary. Also what the server uses to draw the product page for a crawler or a cold visit. |
| Called on page load with the cart id cookie, so a guest coming back the next day finds the same cart. Returns the version the browser compares later answers against. |
| Sets the target quantity of one line; zero removes it. Idempotent, so a retry after a dropped connection is safe. The server reprices and returns the whole cart, which replaces the local one, or refuses with a reason the browser shows. |
| Folds the guest's cart into the signed-in account's cart server-side and reports the differences, such as a quantity combined or an item now out of stock, so the browser can tell the shopper instead of silently dropping anything. |
The high-level design
Step 1 of 5
Three pages and one server
Start with the shape most teams would ship first: a listing page, a product page and a cart page, each fetching what it needs from the server's API when it mounts. The listing holds its filters in component state, a tap on a card opens the product page, and add to cart appends to a cart held in the cart page's memory. It works end to end, and it has the faults the rest of the design fixes: Back to the listing remounts it with no filters, a reload or a pasted link shows the default view, each page is blank until its code and data arrive, and the cart dies with the tab.
What breaks first
The deep dives
From the listing to a product and back. A shopper ticks boxes in several filter sections, taps a product, and presses Back. What do they see at each step, and what did the listing have to remember for that to work?
A first page that shows fast and can be found by search engines. Someone taps a search engine result for a product on a slow phone. What arrives first, what do they see while the rest loads, and how would you know the page meets its target?
A cart that survives a closed tab. A guest adds a few items over a slow connection, closes the tab, and comes back the next day. Where was the cart the whole time, and what did they see at each tap?
Filters and a very long listing. A category holds thousands of products and the shopper ticks one filter after another faster than the results return. How does a tick become a request, which answer reaches the grid, and how much of the catalog is in the page at once?
Concepts this guide uses
Sources
- web.dev: Web Vitals
- web.dev case study: Shopping for speed on eBay.com
- web.dev: Browser-level image lazy loading for the web
- web.dev: Optimize resource loading with the Fetch Priority API
- web.dev: Optimize Largest Contentful Paint
- web.dev: Optimize Cumulative Layout Shift
- web.dev: Rendering on the web
- techinterview.org: Build a filter sidebar with URL state sync
- allahabadi.dev: Design a shopping cart and checkout
- frontendschool.in: E-commerce product listing, scalable frontend design
- MDN: History.replaceState()
- MDN: Working with the History API
- MDN: Web Storage API
- MDN: Using HTTP cookies
- MDN: AbortController