Sessions 1 and 2

Cheat sheet

The mental models, debugging moves and shipping checks worth keeping nearby.

On this page

The three layers

  • HTMLWhat is on the page: headings, buttons, images and lists.
  • CSSHow it looks: layout, colour, spacing and type.
  • JavaScriptWhat it does: clicks, requests and page changes.

React, Next.js, Tailwind and @pw-tech/omni-ui sit on top of these three.

The one idea

UI = f(state)

Change the data and the framework redraws the screen. If two values must agree, derive one from the other instead of storing two copies. Stored copies drift; derived values cannot.

Who writes the HTML?

Four rendering models and the trade-offs they make
ModelAnalogyBuilt bySEOFreshness
CSRRaw ingredients delivered to your house. You cook.BrowserRiskyAlways live
SSRRestaurant cooks fresh when you order.Server, per requestGoodAlways live
SSGThe thali was made this morning. Grab and go.Build, onceGoodFrozen at build
ISRReady thali, remade when older than a limit.Build plus occasional server workGoodStale by at most N seconds

Hydration is when JavaScript makes the server-rendered page interactive. Before it arrives, the page can look ready while its buttons do nothing.

View Source shows what the server sent. Elements shows what exists now. They are different views of the page.

The DOM

HTML is the recipe. The DOM is the dish: a live tree the browser builds from the HTML. JavaScript searches and changes this tree.

The browser can repair broken HTML by closing tags, adding a tbody, or splitting invalid nesting. That is another reason View Source and Elements can differ.

What a visual change asks the browser to do
ChangeAnalogyBrowser work
Colour, background, shadowRepaint a wallStyle, then paint
Width, height, margin, font sizeResize furnitureStyle, layout, then paint
transform, opacitySlide a sticker on glassComposite only

For smooth animation, prefer transform and opacity.

SEO and visibility

Googlebot crawls a URL, renders the page, decides whether to index it, then ranks it. Crawling and indexing are different steps.

What each visibility control does
ControlWhat it doesWhat it does not do
robots.txt DisallowAsks polite bots not to crawl a path.Hide the URL, secure a page, or let Google read its noindex rule.
noindexAsks search engines not to show the page in results.Work if crawling is blocked.
nofollowAsks bots not to follow links from a page.Hide the page itself.
LoginActually restricts access to the page.Replace a search directive on a public page.
CanonicalHints which URL is the preferred version.Force Google to use that URL.

Classic mistake: combine Disallow and noindex. Google cannot fetch the page to read noindex, so the URL may remain indexed as blocked.

For non-production: put login in front, send X-Robots-Tag: noindex, nofollow on every response, set the root layout to noindex, and do not block crawling in robots.txt.

Release checks

curl -sI https://<staging-domain> | grep -i x-robots-tag
curl -sI https://www.pw.live | grep -i x-robots-tag

The staging response must say noindex. Production should print nothing.

See what Google sees

Use Search Console URL Inspection, Rich Results Test, DevTools with JavaScript disabled, or a site:yourdomain.com search.

Six useful DevTools moves

Start with the tab that answers your question
TabQuestion
ElementsWhy does it look like that? A struck-through rule lost.
ConsoleIs something red? Include the error in the bug report.
NetworkDid the browser request the right data?
ThrottlingDoes it still work on Slow 4G and a slower CPU?
Device toolbarDoes it work at phone width?
LighthouseWhat do the performance and accessibility checks report?

Whose bug is it?

Request is red or returns 4xx/5xx          -> backend
Request is 200, data is right, screen wrong -> frontend
Request never appears                      -> frontend never asked

Performance

Core Web Vitals and supporting timing metrics
MetricHuman questionGoodPoor
TTFBHow long before the server said anything?At most 0.8sOver 1.8s
FCPWhen did any content appear?At most 1.8sOver 3.0s
LCPWhen did the main thing appear?At most 2.5sOver 4.0s
INPWhen I tap, does the page respond?At most 200msOver 500ms
CLSDoes the page jump around?At most 0.1Over 0.25

LCP, INP and CLS are the Core Web Vitals. They are measured at the 75th percentile of real users. FID retired; INP replaced it in March 2024. TBT is a lab stand-in for INP.

Field data comes from real users and real devices. Lab data is a controlled diagnosis. When they disagree, believe the field data.

Three fixes to try first

  1. Set image width and height, and reserve space for late-loading content to reduce CLS.
  2. Do not lazy-load the hero image. Serve it as WebP or AVIF to help LCP.
  3. Keep long work out of click handlers to improve INP.

Images

  • Use WebP or AVIF for photos.
  • Use srcset and sizes so a phone does not download a 3000-pixel image.
  • Set width and height to reserve space.
  • Prioritize the hero image; lazy-load content below the fold.

Virtualization and bundles

Virtualization renders only the list items on screen and recycles them as you scroll. A list of 10,000 posts might keep about 10 items in the DOM. TanStack Virtual and react-window do this on the web; RecyclerView and FlatList use the same idea.

  • Tree shaking: remove code that is never imported. It works with ES modules.
  • Code splitting: load a feature only when it is needed, with lazy(() => import(...)) or next/dynamic.
  • Check a package's size before adding it with pkg-size.dev or bundlephobia.com.

Caching

Browser cache and app query cache -> CDN -> server cache -> database
Common cache headers and when to use them
ResourceCache-ControlReason
Hashed JavaScript filepublic, max-age=31536000, immutableThe filename changes with the content.
HTML pageno-cache plus short CDN s-maxageCheck for a new deploy.
Public APIpublic, s-maxage=60, stale-while-revalidate=300Let the CDN absorb repeat traffic.
Personal APIprivate, no-storeKeep one person's data out of shared caches.
  • no-cache means keep a copy but check before using it. no-store means do not keep it.
  • max-age applies to browsers too. s-maxage applies to shared caches such as a CDN.
  • In Next.js, export const revalidate = 60 sets an ISR interval. revalidatePath and revalidateTag clear the Next.js cache, not the CDN.
  • Cmd+Shift+R clears your browser cache. A stale page can also mean a deploy is unfinished or a CDN still has an old copy.
  • HTML, JavaScript, CSS and JSON compress well with gzip or Brotli. Check content-encoding.

If another user's name appears on your screen, treat it as a P0. A shared cache may be serving personal data.

React in six lines

function DishCard({ dish, onAdd }) {
  const [qty, setQty] = useState(0);
  const total = qty * dish.price;
  useEffect(() => {
    const id = setInterval(tick, 1000);
    return () => clearInterval(id);
  }, []);
  return <button onClick={() => onAdd(dish)}>Add: {total}</button>;
}
  • Props are the arguments, such as dish and onAdd.
  • State is component memory and the signal to redraw.
  • Derived values are calculated from state and cannot go stale.
  • Effects connect to timers, APIs and analytics. Clean them up.
  • Keys should use a stable ID, never an array index.

The PW stack

Common tools and where they appear
ToolWhat it isExample repo
ReactThe UI libraryEverywhere
Next.jsReact with a server, routing and SEO toolspw-talk-web, center-page, pw-live-class-fe
Rsbuild and Module FederationBuild and load widgets at runtimereact-batches, help-centre-react-fe
Tailwind CSSStyle by composing utility classesModern repos
TanStack QueryLoading, errors, caching and refetching for API callsMost repos
@pw-tech/omni-uiShared component libraryMost repos
pnpmPackage manager. Avoid mixing package managers in one repo.Most repos
Sentry and Grafana FaroError and performance monitoringMost repos
CloudFrontCDN in front of pw.liveCheck x-cache in DevTools

Module Federation is like a shaadi buffet: different teams run their own counters, but guests see one party.

Working with AI

Give an AI four things before it writes code:

  1. Brand: exact colours, font, radius and spacing.
  2. Structure: the sections, in order.
  3. Constraints: file count, dependencies and layout approach.
  4. Quality bar: semantic HTML, image dimensions and alt text, visible focus, responsive layout and no layout shift.
Build a responsive page with semantic HTML and CSS Grid.
Use real buttons, visible :focus-visible states, and meaningful alt text.
Reserve space for images and late-loading content. Keep it usable at phone width.

Ask it to explain the result line by line, then review the output. AI can write a first draft; it cannot decide whether the draft is good.

Five-minute pre-ship check

  • Console has no errors.
  • Network requests succeed and none take over one second.
  • Page works on Slow 4G and 4x CPU throttling.
  • No horizontal scroll at phone width.
  • Tab through the page and check that focus stays visible.
  • Lighthouse accessibility score is above 90.
  • Loading, empty and error states are present.