This API route performs accessibility scanning on websites using Playwright and axe-core.
{
"url": "https://example.com"
}{
"url": "https://example.com",
"timestamp": "2024-01-01T00:00:00.000Z",
"totalIssues": 5,
"issues": [
{
"selector": "body > div > img",
"ruleId": "image-alt",
"wcag": ["wcag2a", "wcag111"],
"severity": "critical",
"message": "Images must have alternate text",
"source": "axe"
}
],
"summary": {
"axe": 5,
"pa11y": 0
}
}- Playwright Integration: Uses Playwright Chromium for reliable browser automation
- Axe-Core: Comprehensive accessibility testing with configurable rules
- WCAG Compliance: Maps issues to WCAG guidelines
const response = await fetch('/api/scan', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
url: 'https://your-website.com'
})
});
const result = await response.json();
console.log('Accessibility issues:', result.issues);Run the development server:
npm run devThen test the API:
node test-scan.jsThe API includes several configurable axe-core rules:
color-contrast: Color contrast requirementsdocument-title: Page title requirementshtml-has-lang: HTML language attributeimage-alt: Image alt text requirementslink-name: Link accessibilitylist&listitem: List structurepage-has-heading-one: Page heading structureregion: Page regionsskip-link: Skip navigation links
You can modify these rules in the route.ts file under the axe.run() configuration.