Skip to content

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
Asked at
Practice this question

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

AskWhy it mattersA 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 /products and /cart API. Listing and product pages must be readable by search engines.

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.

The requirements menu

RequirementScopeWhy

Browse a listing of products, filter and sort it, and come back to or share the same view

Core

The listing is where browsing happens, and coming back to the same view is where a naive build fails first.

Open a product page with its photos, its options such as size, its price and its stock

Core

The product page is the landing page from search engine results, and it is where the options, price and stock are shown before the add in this design.

Add to a cart, change quantities and remove items, as a guest or signed in

Core

A shopper who does not want to sign in still needs a cart, and the guest cart is where the storage questions start.

Keep the cart when the tab closes and the shopper comes back later

Core

A cart that lives in the page's memory is lost with the tab, and the requirement asks for it to be there when the shopper comes back.

Show the active filters as removable chips above the grid

Optional

Take it if there is time: with filters in 10 to 30 sections, chips are how a shopper sees what is applied and removes one without hunting. It adds little once the filter state is a single object.

Show a count next to each filter value

Optional

Take it if the server returns facet counts with results. It helps a shopper avoid empty results, but the counts can lag the grid after a fast change, which you then have to handle.

Keep a list of recently viewed products

Optional

Take it for a store where shoppers compare and return. It is a small list in browser storage, but it crowds a short round if the core flows are not done.

Save an item for later outside the cart

Optional

Leave it unless asked: it is a second list with the same storage and merge questions as the cart, so it doubles that work.

Push each price and stock change live to open listings

Out of scope

An open connection per listing is heavy for a page that is left often and soon. Refresh price and stock when the product page opens, and let the server refuse a stale add.

A search box with suggestions as someone types

Out of scope

Its own question with its own problems: answers out of order, a pause before asking. The listing takes a query string and leaves the box to another round.

Payment

Out of scope

Checkout and payment are a separate round: idempotent charges and card tokenization have nothing to do with the listing.

How the server ranks search results

Out of scope

The client sends a sort and filters and draws what comes back; ranking is a backend question.

Reviews written by shoppers

Out of scope

Reviews are a section the product page can load late; writing them is a form of its own.

Seller tools

Out of scope

Listing management is a different product with different users.

Non-functional requirements

RequirementWhat 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

EntityWhat 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

EndpointNotes

GET /products?category&filters&sort&cursor → { products, facets, next_cursor, total }

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.

GET /products/{id} → product detail

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.

GET /cart → cart

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.

PUT /cart/lines/{sku} { quantity } → cart

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.

POST /cart/merge (at sign-in) → { cart, changes }

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

tapGETdetailaddListingProductServerCart page

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.

filterschangeresultsaheadtapsummaryGETdetailHTMLimageaddidnowbadgePUTAddress barListing pageFilter panelResults and scrollIdle prefetchProduct pageServerSearch engineImage hostClient cartCart id cookieCart badgeCart page
The whole design

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 concept that helps here: Where state livesA concept that helps here: Client caching and normalized stores

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 concept that helps here: Rendering strategies: client, server, staticA concept that helps here: Core Web VitalsA concept that helps here: Loading images

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?

A concept that helps here: Optimistic updates and rollbackA concept that helps here: Where state livesA concept that helps here: Conflict resolution on the client

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?

A concept that helps here: Pagination and infinite scrollA concept that helps here: Debounce and request cancellationA concept that helps here: List virtualization

Concepts this guide uses

Sources

  1. web.dev: Web Vitals
  2. web.dev case study: Shopping for speed on eBay.com
  3. web.dev: Browser-level image lazy loading for the web
  4. web.dev: Optimize resource loading with the Fetch Priority API
  5. web.dev: Optimize Largest Contentful Paint
  6. web.dev: Optimize Cumulative Layout Shift
  7. web.dev: Rendering on the web
  8. techinterview.org: Build a filter sidebar with URL state sync
  9. allahabadi.dev: Design a shopping cart and checkout
  10. frontendschool.in: E-commerce product listing, scalable frontend design
  11. MDN: History.replaceState()
  12. MDN: Working with the History API
  13. MDN: Web Storage API
  14. MDN: Using HTTP cookies
  15. MDN: AbortController
Sign in to report a mistake