Lessons in this course (12)

6. Cookies, sessions, caching and CORS

Intermediate★ Must knowLesson 6 of 12

Cookies — small key/value data the server asks the browser to store and send back:

AttributeMeaning
HttpOnlyJavaScript can't read it (protects against XSS stealing it)
SecureOnly sent over HTTPS
SameSite=Lax/Strict/NoneControls sending on cross-site requests (CSRF protection)
Domain, Path, Expires/Max-AgeScope and lifetime

Session — the server keeps data (in memory, Redis, SQL) keyed by a session id stored in a cookie. In a web farm, sessions must be shared (distributed cache) or the load balancer must use sticky sessions.

HTTP caching:

HeaderEffect
Cache-Control: public, max-age=31536000, immutableCache for a year (fingerprinted static files like app.3f9a.js)
Cache-Control: no-storeNever cache (sensitive pages)
ETag + If-None-MatchRevalidate: server answers 304 Not Modified without a body
Last-Modified + If-Modified-SinceSame with dates

CORS — a browser rule: JavaScript on origin A may read responses from origin B only if B allows A via Access-Control-Allow-Origin. Non-simple requests send an OPTIONS preflight first.