Implement BS-008 privacy draft and public prerendering
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
const { chromium } = require('playwright');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const AxeBuilder = require('@axe-core/playwright').default;
|
||||
|
||||
const root = path.resolve(__dirname, '../dist');
|
||||
|
||||
(async () => {
|
||||
const browser = await chromium.launch({ headless: true });
|
||||
try {
|
||||
const widths = process.env.BS008_FOCUS ? [390] : [1440, 390, 320];
|
||||
const paths = process.env.BS008_FOCUS ? ['/privacy'] : ['/', '/services', '/projects', '/projects/roulettebuddy', '/about', '/contact', '/privacy', '/login', '/projects/example'];
|
||||
for (const width of widths) for (const pathname of paths) {
|
||||
const candidate = pathname === '/' ? path.join(root, 'index.html') : path.join(root, pathname.slice(1), 'index.html');
|
||||
const staticFile = fs.existsSync(candidate) ? candidate : path.join(root, '404.html');
|
||||
const staticHtml = fs.readFileSync(staticFile, 'utf8');
|
||||
const staticHeading = /<h1\b/.test(staticHtml);
|
||||
const context = await browser.newContext({ viewport: { width, height: 900 } });
|
||||
if (pathname === '/privacy' && width === 390) await context.addInitScript(() => localStorage.setItem('bortstack-theme', 'light'));
|
||||
const page = await context.newPage();
|
||||
const errors = [];
|
||||
page.on('pageerror', error => errors.push(error.message));
|
||||
page.on('console', message => { if (message.type() === 'error') errors.push(message.text()); });
|
||||
await page.route('**/*', route => {
|
||||
const url = new URL(route.request().url());
|
||||
if (url.pathname === '/api/contact/config') return route.fulfill({ json: { email: null, available: false } });
|
||||
const requested = url.pathname === '/' || url.pathname === pathname ? staticFile : path.join(root, url.pathname);
|
||||
if (!requested.startsWith(root)) return route.abort();
|
||||
const file = fs.existsSync(requested) ? requested : path.join(root, 'index.html');
|
||||
const contentType = file.endsWith('.js') ? 'application/javascript' : file.endsWith('.css') ? 'text/css' : file.endsWith('.html') ? 'text/html' : 'application/octet-stream';
|
||||
return route.fulfill({ status: file === staticFile && staticFile.endsWith('404.html') ? 404 : 200, body: fs.readFileSync(file), contentType });
|
||||
});
|
||||
const response = await page.goto(`http://local.test${pathname}`);
|
||||
await page.getByRole('main').getByRole('heading', { level: 1 }).first().waitFor();
|
||||
const result = {
|
||||
width,
|
||||
pathname,
|
||||
staticHeading,
|
||||
status: response.status(),
|
||||
theme: await page.locator('html').getAttribute('data-bs-theme'),
|
||||
title: await page.title(),
|
||||
robots: await page.locator('meta[name="robots"]').getAttribute('content'),
|
||||
main: await page.getByRole('main').count(),
|
||||
overflow: await page.evaluate(() => document.documentElement.scrollWidth > document.documentElement.clientWidth),
|
||||
axeViolations: (await new AxeBuilder({ page }).analyze()).violations.map(violation => ({ id: violation.id, nodes: violation.nodes.map(node => ({ target: node.target, summary: node.failureSummary })) })),
|
||||
errors,
|
||||
};
|
||||
console.log(JSON.stringify(result));
|
||||
const expectedRobots = pathname === '/privacy' || staticFile.endsWith('404.html') ? 'noindex, nofollow' : 'index, follow';
|
||||
const isMissing = staticFile.endsWith('404.html');
|
||||
const unexpectedErrors = errors.filter(error => !(isMissing && error === 'Failed to load resource: the server responded with a status of 404 (Not Found)'));
|
||||
const expectedTheme = pathname === '/privacy' && width === 390 ? 'light' : 'dark';
|
||||
if (!staticHeading || !staticHtml.includes(`content="${expectedRobots}"`) || result.status !== (isMissing ? 404 : 200) || result.theme !== expectedTheme || result.robots !== expectedRobots || result.main !== 1 || result.overflow || result.axeViolations.length || unexpectedErrors.length) process.exitCode = 1;
|
||||
await page.close();
|
||||
await context.close();
|
||||
}
|
||||
} finally {
|
||||
await browser.close();
|
||||
}
|
||||
})().catch(error => { console.error(error); process.exitCode = 1; });
|
||||
Reference in New Issue
Block a user