Adds the Lifestyle fotografie section to the homepage
Adds the first block below the hero: heading, intro and a staggered gallery of four live photos. Brings in the shared section building blocks (HomeSection, SectionHeading, Reveal) and DeferredImage, which holds back below-the-fold photos until the page has loaded so they do not compete with the hero for bandwidth. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import { act, screen } from '@testing-library/react';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { FakeIntersectionObserver } from '@/test/fakeIntersectionObserver';
|
||||
import { renderWithProviders } from '@/test/renderWithProviders';
|
||||
import DeferredImage from './DeferredImage';
|
||||
|
||||
const image = {
|
||||
src: '/foto.jpg',
|
||||
alt: 'Een foto',
|
||||
width: 800,
|
||||
height: 1200,
|
||||
};
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
FakeIntersectionObserver.reset();
|
||||
});
|
||||
|
||||
describe('DeferredImage', () => {
|
||||
it('loads straight away without IntersectionObserver once the page has loaded', async () => {
|
||||
await renderWithProviders(<DeferredImage image={image} />);
|
||||
const img = screen.getByRole('img', { name: 'Een foto' });
|
||||
expect(img).toHaveAttribute('src', '/foto.jpg');
|
||||
expect(img).toHaveAttribute('width', '800');
|
||||
expect(img).toHaveAttribute('height', '1200');
|
||||
expect(img).toHaveAttribute('loading', 'lazy');
|
||||
});
|
||||
|
||||
it('waits until the image comes near the viewport', async () => {
|
||||
vi.stubGlobal('IntersectionObserver', FakeIntersectionObserver);
|
||||
await renderWithProviders(<DeferredImage image={image} />);
|
||||
const img = screen.getByRole('img', { name: 'Een foto' });
|
||||
const [observer] = FakeIntersectionObserver.instances;
|
||||
|
||||
expect(img).not.toHaveAttribute('src');
|
||||
expect(observer.options?.rootMargin).toBe('600px 0px');
|
||||
|
||||
act(() => observer.trigger(true));
|
||||
expect(img).toHaveAttribute('src', '/foto.jpg');
|
||||
});
|
||||
|
||||
it('waits for the page load event, even when already in view', async () => {
|
||||
vi.spyOn(document, 'readyState', 'get').mockReturnValue('interactive');
|
||||
await renderWithProviders(<DeferredImage image={image} />);
|
||||
const img = screen.getByRole('img', { name: 'Een foto' });
|
||||
expect(img).not.toHaveAttribute('src');
|
||||
|
||||
act(() => {
|
||||
window.dispatchEvent(new Event('load'));
|
||||
});
|
||||
expect(img).toHaveAttribute('src', '/foto.jpg');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,37 @@
|
||||
import { Image, type ImageProps } from '@chakra-ui/react';
|
||||
import usePageLoaded from '@/hooks/usePageLoaded';
|
||||
import useInView from '@/hooks/useInView';
|
||||
import type { ContentImage } from '@/content/types';
|
||||
|
||||
// How far below the viewport an image starts loading
|
||||
const LOAD_MARGIN = '600px 0px';
|
||||
|
||||
type DeferredImageProps = Omit<ImageProps, 'src' | 'alt'> & {
|
||||
image: ContentImage;
|
||||
};
|
||||
|
||||
// A below-the-fold image that only starts downloading once the page has
|
||||
// finished loading and the image is near the viewport. Native
|
||||
// loading="lazy" is not enough: Chrome fetches lazy images up to ±1250px
|
||||
// below the screen straight away, so photos right under the hero compete
|
||||
// with it for bandwidth and delay the largest contentful paint.
|
||||
// Width and height reserve the space, so nothing shifts when it loads.
|
||||
export default function DeferredImage({ image, ...rest }: DeferredImageProps) {
|
||||
const pageLoaded = usePageLoaded();
|
||||
const { ref, inView } = useInView<HTMLImageElement>({
|
||||
rootMargin: LOAD_MARGIN,
|
||||
});
|
||||
|
||||
return (
|
||||
<Image
|
||||
ref={ref}
|
||||
src={pageLoaded && inView ? image.src : undefined}
|
||||
alt={image.alt}
|
||||
htmlWidth={image.width}
|
||||
htmlHeight={image.height}
|
||||
loading="lazy"
|
||||
decoding="async"
|
||||
{...rest}
|
||||
/>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user