Web front end (framework-independent)¶
Scope: any web front end, whatever the framework or build tool, with a focus on accessibility, performance, security, resilience and testing. Framework-specific rules are in React, and the general guidelines still apply, including the test strategy in Testing and delivery. Games that run in the browser are in Web games. Cross-platform accessibility principles and the laws behind them, including the WCAG 2.2 Level AA baseline (A11Y-1), are in Accessibility.
Entries are sorted by score from 1 to 10 (criteria): 9โ10 start here or strongly recommended, 6โ8 recommended, 1โ5 further reading or conditional.
Adopted references¶
- 10 Web Content Accessibility Guidelines (WCAG) 2.2 (W3C Recommendation): the baseline for accessibility. Testable success criteria at levels A, AA and AAA.
- 9 ARIA Authoring Practices Guide (W3C WAI): patterns for custom widgets such as tabs, menus, comboboxes and dialogs, with the roles, states and keyboard support each one needs.
- 9 Cross Site Scripting Prevention Cheat Sheet (OWASP): the baseline for XSS defences, including output encoding, HTML sanitization, safe sinks and the escape hatches in frameworks.
- 8 The most effective ways to improve Core Web Vitals (web.dev): the highest-impact fixes for loading, responsiveness and layout stability, in priority order.
- 8 HTTP Headers Cheat Sheet (OWASP): which security headers to send, with recommended values.
- 8 Content Security Policy Cheat Sheet (OWASP): how to build, deploy and test a CSP.
- 8 Best Practices (Playwright): how to write resilient end-to-end tests: test user-visible behaviour, isolate tests, use locators and web-first assertions, debug failures on CI with traces, and test across browsers.
- 7 Accessibility (MDN): guides and references for accessible HTML, CSS, JavaScript and ARIA.
- 7 Baseline (web.dev): which web platform features work across the core browsers, as Newly or Widely available.
- 7 Web Vitals (web.dev): the Core Web Vitals (LCP, INP and CLS), their "good" thresholds and how to measure them.
- 7 Component Testing (Vitest): testing the components of any framework in a real browser with Browser Mode, including what makes a good component test, user interactions, accessibility and mocking.
Key rules¶
Accessibility¶
- 9
WEB-2Use native HTML elements, such asbutton,a,inputandselect, before ARIA. Native elements come with keyboard support, roles and states. If you give an element an ARIA role instead, you have to build the behaviour that role promises yourself. (MDN, ARIA, APG, Read me first: No ARIA is better than bad ARIA) - 7
WEB-3Run automated accessibility checks, such as axe-core, in CI and in end-to-end tests. They find only part of the problems, axe-core 57% of WCAG issues on average, so they add to the testing by hand inA11Y-7and don't replace it. (axe-core, MDN, Accessibility tooling and assistive technology) - 6
WEB-22After each client-side navigation in a single-page app, change the document's<title>to describe the new view, and move focus to the new content, such as its main heading, or announce the new page to screen readers. Unlike a full page load, most routers neither move focus nor announce the changed title by themselves. Platform examples:ANGULAR-10,VUE-12,SVELTE-15andASTRO-12. (W3C, Understanding WCAG 2.2 SC 2.4.2 Page Titled, BBC GEL, Routing, Gatsby, User testing of accessible client-side routing techniques)
Performance¶
- 7
WEB-4Measure Core Web Vitals from real users at the 75th percentile, separately for mobile and desktop, and set priorities from that field data. Use lab tests, which are reproducible, to debug and to compare changes. (web.dev, Web Vitals, web.dev, Why lab and field data can be different) - 7
WEB-5Ship less JavaScript: split off code that the first render doesn't need, remove unused code, and use Baseline platform features instead of JavaScript reimplementations. (web.dev, The most effective ways to improve Core Web Vitals) - 7
WEB-6Give every image and embed explicit dimensions, offer sizes that fit the layout withsrcsetandsizes, and lazy-load only images outside the initial viewport. (web.dev, The most effective ways to improve Core Web Vitals, web.dev, Browser-level image lazy loading, web.dev, Responsive images) - 7
WEB-7Put a content hash or version in the URLs of static assets and cache them for a long time withimmutable, and serve HTML withCache-Control: no-cache(plusprivatewhen it's personalised) so that it's always revalidated. (MDN, HTTP caching)
Security¶
- 9
WEB-8Render untrusted data through the framework's automatic escaping ortextContent, and sanitize it with DOMPurify before it reaches an HTML sink such asinnerHTMLor React'sdangerouslySetInnerHTML. (OWASP, Cross Site Scripting Prevention, OWASP, DOM based XSS Prevention) - 7
WEB-9Send security headers with every response:Strict-Transport-Security,X-Content-Type-Options: nosniff, aReferrer-Policy, and the CSPframe-ancestorsdirective against framing. (OWASP, HTTP Headers) - 7
WEB-10Add a strict, nonce- or hash-based Content Security Policy as a second layer of defence against XSS, and try it inContent-Security-Policy-Report-Onlymode before enforcing it. (OWASP, Content Security Policy, web.dev, Mitigate XSS with a strict CSP) - 6
WEB-11Load third-party scripts with Subresource Integrity, or serve your own reviewed copies. (MDN, Subresource Integrity, OWASP, Third Party JavaScript Management)
Compatibility and resilience¶
- 6
WEB-12Choose a Baseline target, such as Baseline Widely available, from your users' browser data, and set it once in Browserslist so that build and lint tools share it. (web.dev, How to choose your Baseline target, web.dev, Use Baseline with Browserslist) - 5
WEB-13Build core user journeys with progressive enhancement: HTML that works on its own first, then CSS and JavaScript that enhance it, so that a script that fails to load doesn't stop users. (GOV.UK Service Manual, Building a robust frontend using progressive enhancement, MDN, Progressive enhancement)
Testing¶
- 7
WEB-14Find elements in tests the way users find them: by role and accessible name first, then by label or text, and by test ID only when nothing visible identifies the element. Don't select by CSS class, XPath or DOM structure, which change with the markup. This is how to followREACT-6(React) andVUE-15(Vue) in any framework, with Testing Library queries or Playwright locators. (Testing Library, About Queries: Priority, Playwright, Locators) - 7
WEB-15Never wait for a fixed time in a test: use actions that wait automatically and assertions that retry until the page reaches the expected state, such as Playwright'sawait expect(locator).toBeVisible()or Testing Library'sfindByqueries. Tests that wait for a fixed time are inherently flaky. (Playwright,page.waitForTimeout, Playwright, Best Practices: Use web first assertions, Testing Library, Async methods) - 7
WEB-16Isolate every test: run it in a fresh browser context, let it build the state and data it needs instead of relying on earlier tests, and when tests change server-side state, give each parallel worker its own user account. Isolated tests can run in any order and in parallel, and one failure doesn't cascade into others. (Playwright, Isolation, Playwright, Authentication: one account per parallel worker, Fowler, Eradicating Non-Determinism in Tests) - 7
WEB-17Run the end-to-end tests in CI on every pull request, in each browser engine you support, and keep a trace of every failure. In Playwright, add a project per browser, chosen from your browser targets (WEB-12), and settraceto'on-first-retry'or'retain-on-failure'. A trace holds a DOM snapshot of each action, plus screenshots, network requests and console logs. (Playwright, Best Practices, Playwright, Trace viewer: Tracing on CI) - 7
WEB-18Treat a flaky test as a bug: quarantine it outside the blocking pipeline, file a bug for it, and fix it within a set time. Don't let automatic retries hide flakiness: Playwright reports a test that passes only on retry as flaky, so fail the run or alert on it. (Fowler, Eradicating Non-Determinism in Tests, Google Testing Blog, Flaky Tests at Google and How We Mitigate Them, Playwright, Retries) - 6
WEB-19Test components in a real browser, for example with Vitest Browser Mode, and keep end-to-end tests to the few user journeys that carry the product's core value. This is how a web front end follows the test pyramid (TEST-1in Testing and delivery): component tests in a browser catch layout, focus and browser API problems that DOM simulations such as jsdom miss, and they run faster than end-to-end tests. (Vitest, Component Testing: Why Browser Mode?, Vocke, The Practical Test Pyramid) - 6
WEB-20Mock HTTP at the network level, for example with Mock Service Worker, instead of mockingfetchor your API client modules, and make requests without a handler fail the test. The application code then runs unchanged, and the same handlers can also serve the app during development. In end-to-end tests, stub only third-party services that you don't control, for example with Playwright'spage.route. (Vitest, Mocking Requests, Dodds, Stop mocking fetch, Playwright, Best Practices: Avoid testing third-party dependencies) - 5
WEB-21Keep visual comparison (screenshot) tests to a few stable pages and components, hide dynamic content such as dates, and create and compare the screenshots in the same environment, with the same operating system and browser versions. Rendering differs between platforms, browsers and fonts, and visual tests that fail on unrelated changes stop giving useful feedback. (Playwright, Visual comparisons, web.dev, Learn Testing: Visual tests)
How to enforce¶
Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.
- 7 axe-core rules: automated checks tagged by WCAG version and level, including
aria-allowed-role,aria-required-childrenandnested-interactivefor misused ARIA. Run them in end-to-end tests with @axe-core/playwright. Partly checksA11Y-1,WEB-2andWEB-3. - 6 Lighthouse CI: fails CI when lab metrics, resource sizes such as total script bytes, or individual audits such as
unsized-imagesmiss your assertions. Lab tests can't measure INP. Partly checksWEB-4,WEB-5andWEB-6. - 6 Size Limit: fails CI when the size or estimated download and run time of your JavaScript, dependencies included, exceeds a budget. Partly checks
WEB-5. - 6 eslint-plugin-no-unsanitized (Mozilla): flags assignments to
innerHTMLand calls such asinsertAdjacentHTMLordocument.writethat don't go through an escaping function. Partly checksWEB-8. - 6 HTTP Observatory CLI (MDN): scans a deployed site and grades its CSP, HSTS,
X-Content-Type-Options, framing protection,Referrer-Policy, cookies and Subresource Integrity. Run it against a staging deployment in CI. ChecksWEB-9, and partlyWEB-10andWEB-11. - 6 @eslint/css
use-baseline: flags CSS properties, values, at-rules and selectors outside your Baseline target, unless they're guarded with@supports. Partly checksWEB-12. - 6 Playwright
failOnFlakyTests: fails the run when a test passes only on retry, so retries can't hide new flakiness. Set it on CI, or pass--fail-on-flaky-tests. Partly checksWEB-18. - 6 Mock Service Worker
onUnhandledFrame(onUnhandledRequestbefore MSW 3): set to'error', it turns every request that no handler covers into an error, so tests can't reach the real network by accident. Partly checksWEB-20. - 6 Vitest
sequence.shuffle: runs test files and tests in random order, which exposes tests that depend on others having run first. Partly checksWEB-16. - 5 eslint-plugin-compat: flags JavaScript web APIs that the browsers in your Browserslist config don't support. Partly checks
WEB-12. - 5 eslint-plugin-playwright
no-wait-for-timeout: the recommended config flagspage.waitForTimeout(), andprefer-web-first-assertionsflags assertions that don't retry. Turn onno-raw-locatorsto flag CSS and XPath locators. Partly checksWEB-14andWEB-15. - 5 eslint-plugin-testing-library
no-container: flagscontainer.querySelectorand other container methods, andno-node-accessflags DOM traversal and direct node access instead of queries and user events. Partly checksWEB-14.
Tutorials and further reading¶
- 7 Learn Performance (web.dev): a course on web performance concepts and techniques, from resource loading to images, fonts and JavaScript.
- 7 Learn Accessibility (web.dev): an evergreen course and reference on accessible design and development.
- 7 Accessibility on the web (MDN Learn): a module on accessible HTML, CSS, JavaScript, WAI-ARIA and testing with assistive technology.
- 6 Learn Images (web.dev): an in-depth course on image formats, compression and responsive images.
- 6 Learn Testing (web.dev): a course on testing web apps, from types of tests and where they run to component testing in practice, static analysis and assertions.
- 6 The Testing Trophy and Testing Classifications (Kent C. Dodds, 2021): the testing trophy, a guide to the return on investment of static analysis, unit, integration and end-to-end tests for JavaScript applications, and how its author classifies them.