Adds the fallbacks for visitors without JavaScript

The mobile menu works through CSS, and controls that need JavaScript are hidden
without it. The contact photo gets its own preload, and the tests cover the new
Reveal behaviour, the first row and the noscript markup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-31 23:09:33 +01:00
co-authored by Claude Opus 5.5
parent 44125155bc
commit 01fcfeefea
38 changed files with 1510 additions and 67 deletions
@@ -1,7 +1,11 @@
import { ChakraProvider } from '@chakra-ui/react';
import { act, screen } from '@testing-library/react';
import { prerender } from 'react-dom/static';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { FakeIntersectionObserver } from '@/test/fakeIntersectionObserver';
import { hydratePage, serverHtml } from '@/test/hydrate';
import { renderWithProviders } from '@/test/renderWithProviders';
import system from '@/theme';
import DeferredImage, { DEFERRED_LOAD_MARGIN } from './DeferredImage';
const image = {
@@ -73,3 +77,80 @@ describe('DeferredImage', () => {
expect(FakeIntersectionObserver.instances).toHaveLength(0);
});
});
describe('DeferredImage without JavaScript', () => {
const imgTags = (html: string) => html.match(/<img\b[^>]*>/g) ?? [];
const attribute = (tag: string, name: string) =>
new RegExp(` ${name}="([^"]*)"`).exec(tag)?.[1];
const hasAttribute = (tag: string, name: string) =>
new RegExp(` ${name}[ =/>]`).test(tag);
it('renders the empty image and, in a noscript, a twin with the photo', async () => {
const html = await serverHtml(
<DeferredImage image={image} id="foto" data-testid="foto" />,
);
const [first = '', twin = '', ...others] = imgTags(html);
expect(others).toEqual([]);
expect(html.match(/<noscript>/g)).toHaveLength(1);
expect(html).toMatch(/<noscript><img\b[^>]*src="\/foto\.jpg"/);
expect(hasAttribute(first, 'data-deferred')).toBe(true);
expect(hasAttribute(first, 'src')).toBe(false);
expect(attribute(first, 'loading')).toBe('lazy');
expect(attribute(first, 'id')).toBe('foto');
expect(attribute(first, 'data-testid')).toBe('foto');
expect(attribute(twin, 'src')).toBe('/foto.jpg');
expect(attribute(twin, 'alt')).toBe('Een foto');
expect(attribute(twin, 'width')).toBe('800');
expect(attribute(twin, 'height')).toBe('1200');
expect(hasAttribute(twin, 'loading')).toBe(false);
expect(hasAttribute(twin, 'id')).toBe(false);
expect(hasAttribute(twin, 'data-testid')).toBe(false);
expect(hasAttribute(twin, 'data-deferred')).toBe(false);
});
it('gives the twin the class of the first image, so it has the same box', async () => {
const html = await serverHtml(
<DeferredImage image={image} w="100%" objectFit="cover" />,
);
const [first = '', twin = ''] = imgTags(html);
expect(attribute(first, 'class')).toBeTruthy();
expect(attribute(twin, 'class')).toBe(attribute(first, 'class'));
});
it('adds no preload for the twin when it is rendered in a document', async () => {
const { prelude } = await prerender(
<html lang="nl">
<head />
<body>
<ChakraProvider value={system}>
<DeferredImage image={image} />
<img src="/gewoon.jpg" alt="" />
</ChakraProvider>
</body>
</html>,
);
const html = await new Response(prelude).text();
const preloads = html.match(/<link\b[^>]*rel="preload"[^>]*>/g) ?? [];
// React hoists the plain photo, which shows the check can see a preload
expect(preloads).toHaveLength(1);
expect(preloads[0]).toContain('href="/gewoon.jpg"');
expect(html).toMatch(/<noscript><img\b[^>]*src="\/foto\.jpg"/);
});
it('hydrates the pair without a mismatch and renders no twin in the browser', async () => {
const { container, errors } = await hydratePage(
<DeferredImage image={image} />,
);
expect(errors).toEqual([]);
expect(container.querySelectorAll('img')).toHaveLength(1);
});
it('renders an empty noscript when the browser renders it', async () => {
const { container } = await renderWithProviders(
<DeferredImage image={image} />,
);
expect(container.querySelector('noscript')).toBeEmptyDOMElement();
});
});