fix(pwa): avoid CSP-blocked stylesheet deferral

This commit is contained in:
4gray committed 2026-05-24 14:59:08 +03:00
1 parent 697eab6e73
commit bb4e386c6c
6 files changed
+113 -5

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
import { expect, test } from '@playwright/test';
test('@pwa-static PWA build applies the full stylesheet under CSP', async ({
page,
}) => {
const cspConsoleErrors: string[] = [];
page.on('console', (message) => {
if (
message.type() === 'error' &&
/content security policy|inline event handler|onload/i.test(
message.text()
)
) {
cspConsoleErrors.push(message.text());
}
});
await page.goto('/');
const stylesheetLinks = page.locator('link[rel="stylesheet"]');
await expect(stylesheetLinks.first()).toBeAttached();
const stylesheetMedia = await stylesheetLinks.evaluateAll((links) =>
links.map((link) => ({
href: link.getAttribute('href'),
media: link.getAttribute('media'),
}))
);
const appStylesheet = stylesheetMedia.find((link) =>
/^styles-.*\.css$/.test(link.href ?? '')
);
expect(appStylesheet).toBeDefined();
expect(appStylesheet?.media).not.toBe('print');
const materialIcon = page.locator('mat-icon.material-icons').first();
await expect(materialIcon).toBeVisible();
await expect
.poll(() =>
materialIcon.evaluate((icon) =>
getComputedStyle(icon).fontFamily.toLowerCase()
)
)
.toContain('material icons');
await expect
.poll(() =>
page.evaluate(() =>
document.fonts.check('24px "Material Icons"')
)
)
.toBe(true);
const ngswResponse = await page.request.get('/ngsw.json');
await expect(ngswResponse).toBeOK();
const ngswManifest = (await ngswResponse.json()) as {
assetGroups?: Array<{ urls?: string[] }>;
};
const cachedUrls = ngswManifest.assetGroups?.flatMap(
(assetGroup) => assetGroup.urls ?? []
);
expect(cachedUrls).toEqual(
expect.arrayContaining([expect.stringMatching(/^(?:\.\/|\/)media\//)])
);
expect(cspConsoleErrors).toEqual([]);
});