This commit is contained in:
parent
eceb31a9ad
commit
e9b6035c58
48 changed files with 354 additions and 340 deletions
|
|
@ -4,7 +4,11 @@ import path from 'node:path';
|
|||
import { chromium } from 'playwright';
|
||||
|
||||
const dist = path.resolve('dist');
|
||||
const widths = [320, 390, 430, 768, 1024, 1440, 1920];
|
||||
const INDEX_FILE = 'index.html';
|
||||
const MAX_NAV_RETRIES = 3;
|
||||
// Common device widths: iPhone SE / Galaxy S / iPhone 14 / iPad portrait /
|
||||
// iPad landscape / common laptop / full HD desktop.
|
||||
const VIEWPORT_WIDTHS = [320, 390, 430, 768, 1024, 1440, 1920];
|
||||
|
||||
const MIME = {
|
||||
'.html': 'text/html; charset=utf-8',
|
||||
|
|
@ -50,10 +54,13 @@ async function discoverRoutes() {
|
|||
if (!file.endsWith('.html')) continue;
|
||||
const rel = path.relative(dist, file).replaceAll(path.sep, '/');
|
||||
if (rel === '404.html') continue;
|
||||
if (rel.endsWith('/index.html')) {
|
||||
routes.add('/' + rel.slice(0, -'index.html'.length));
|
||||
} else if (rel === 'index.html') {
|
||||
// /writing/* are meta-refresh redirect stubs to /articles/*, not real
|
||||
// pages — measuring them would just remeasure /articles/.
|
||||
if (rel.startsWith('writing/')) continue;
|
||||
if (rel === INDEX_FILE) {
|
||||
routes.add('/');
|
||||
} else if (rel.endsWith(`/${INDEX_FILE}`)) {
|
||||
routes.add('/' + rel.slice(0, -INDEX_FILE.length));
|
||||
} else {
|
||||
routes.add('/' + rel.replace(/\.html$/, '/'));
|
||||
}
|
||||
|
|
@ -112,7 +119,7 @@ const browser = await chromium.launch({ headless: true });
|
|||
const failures = [];
|
||||
|
||||
async function measureViewport(page) {
|
||||
for (let attempt = 0; attempt < 3; attempt += 1) {
|
||||
for (let attempt = 0; attempt < MAX_NAV_RETRIES; attempt += 1) {
|
||||
try {
|
||||
await page.waitForLoadState('load');
|
||||
return await page.evaluate(() => ({
|
||||
|
|
@ -121,7 +128,8 @@ async function measureViewport(page) {
|
|||
}));
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
if (attempt === 2 || !/Execution context was destroyed|navigation/i.test(message)) {
|
||||
const isLast = attempt === MAX_NAV_RETRIES - 1;
|
||||
if (isLast || !/Execution context was destroyed|navigation/i.test(message)) {
|
||||
throw error;
|
||||
}
|
||||
await page.waitForLoadState('load').catch(() => {});
|
||||
|
|
@ -130,7 +138,7 @@ async function measureViewport(page) {
|
|||
}
|
||||
|
||||
try {
|
||||
for (const width of widths) {
|
||||
for (const width of VIEWPORT_WIDTHS) {
|
||||
const page = await browser.newPage({
|
||||
viewport: { width, height: 900 },
|
||||
javaScriptEnabled: false,
|
||||
|
|
@ -138,11 +146,6 @@ try {
|
|||
|
||||
for (const route of routes) {
|
||||
await page.goto(`http://127.0.0.1:${port}${route}`, { waitUntil: 'load' });
|
||||
if (route.startsWith('/writing/')) {
|
||||
await page
|
||||
.waitForURL((url) => url.pathname.startsWith('/articles/'), { timeout: 1000 })
|
||||
.catch(() => {});
|
||||
}
|
||||
const result = await measureViewport(page);
|
||||
|
||||
if (result.scrollWidth > result.clientWidth + 1) {
|
||||
|
|
@ -165,5 +168,5 @@ if (failures.length > 0) {
|
|||
}
|
||||
|
||||
console.log(
|
||||
`No horizontal overflow detected at ${widths.join(', ')}px across ${routes.length} routes.`
|
||||
`No horizontal overflow detected at ${VIEWPORT_WIDTHS.join(', ')}px across ${routes.length} routes.`
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue