Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(empty-states): load the beside hedgehog eagerly (#102231)

Georges-Antoine Assia5071e80eaf6
diff --git a/common/storybook/.storybook/test-runner.ts b/common/storybook/.storybook/test-runner.ts
index fd96e5fa75a6..e081729bee37 100644
--- a/common/storybook/.storybook/test-runner.ts
+++ b/common/storybook/.storybook/test-runner.ts
@@ -541,11 +541,25 @@ async function takeSnapshotWithTheme(
// check if all images have width, unless purposefully skipped
if (!allowImagesWithoutWidth) {
await page.waitForFunction(() => {
- const allImages = Array.from(document.images)
- const areAllImagesLoaded = allImages.every(
+ // Declared inside the callback because this whole body is serialized into the browser.
+ function isImageAccountedFor(i: HTMLImageElement): boolean {
+ if (i.naturalWidth) {
+ return true
+ }
// ProseMirror-separator isn't an actual image of any sort, so we ignore those
- (i: HTMLImageElement) => !!i.naturalWidth || i.classList.contains('ProseMirror-separator')
- )
+ if (i.classList.contains('ProseMirror-separator')) {
+ return true
+ }
+ // A `loading="lazy"` image with no layout box has nothing to intersect, so the
+ // browser can leave it unfetched and this wait can only time out. It is safe to
+ // skip because such an image cannot appear in the screenshot either.
+ // getClientRects() is empty only for display:none, so this still waits for a
+ // visible image that is downloading.
+ return i.getClientRects().length === 0
+ }
+
+ const allImages = Array.from(document.images)
+ const areAllImagesLoaded = allImages.every(isImageAccountedFor)
if (areAllImagesLoaded) {
// Hide gifs to prevent their animations causing flakiness
for (const image of allImages) {
diff --git a/frontend/src/lib/brand/hoggies.tsx b/frontend/src/lib/brand/hoggies.tsx
index a1f7292d55b9..60e10f9aa55f 100644
--- a/frontend/src/lib/brand/hoggies.tsx
+++ b/frontend/src/lib/brand/hoggies.tsx
@@ -26,6 +26,15 @@ export interface HoggiePngModule {
aspectRatio: number
}
+export type HoggiePngProps = AssetSvgProps & {
+ /**
+ * Pass `eager` when the element can be `display: none` at some widths: a lazy image with no
+ * layout box has nothing to intersect, so the browser can leave it unfetched until a
+ * responsive query reveals it. Defaults to `lazy`.
+ */
+ loading?: 'lazy' | 'eager'
+}
+
/**
* A hoggie illustration as an `<img>` backed by the package's PNG export. Drop-in for the
* SVG barrel component: accepts the same `className`/`style`/`size`/`title` props, reserves
@@ -34,8 +43,17 @@ export interface HoggiePngModule {
* `aspect-ratio` and `object-fit` are inline styles, so utility classes can't override
* them - use the `style` prop for that.
*/
-export function pngHoggie({ src, aspectRatio }: HoggiePngModule): ComponentType<AssetSvgProps> {
- function HoggiePng({ className, style, size, title, width, height, ...rest }: AssetSvgProps): JSX.Element {
+export function pngHoggie({ src, aspectRatio }: HoggiePngModule): ComponentType<HoggiePngProps> {
+ function HoggiePng({
+ className,
+ style,
+ size,
+ title,
+ width,
+ height,
+ loading = 'lazy',
+ ...rest
+ }: HoggiePngProps): JSX.Element {
// Mirror the SVG components' sizing: `size` (any CSS length) wins, then explicit
// width/height, then fill the container - the last only when no className is given,
// because inline style beats utility classes (the SVG version used a width
@@ -56,11 +74,11 @@ export function pngHoggie({ src, aspectRatio }: HoggiePngModule): ComponentType<
width={width}
height={height}
style={{ aspectRatio: String(aspectRatio), objectFit: 'contain', ...sizing, ...style }}
- // Decorative illustrations: don't fetch until near the viewport, don't block
- // paint on decode, and never compete with real content for bandwidth.
+ // Decorative illustrations: don't block paint on decode, and never compete with
+ // real content for bandwidth. By default don't fetch until near the viewport.
// (lowercase fetchpriority: React 18 only forwards the attribute un-camelized;
// aspect-ratio above reserves layout, so lazy loading causes no shift.)
- loading="lazy"
+ loading={loading}
decoding="async"
{...{ fetchpriority: 'low' }}
// Callers type against the SVG prop surface; the shared subset (aria-*, data-*,
diff --git a/frontend/src/lib/components/ProductEmptyState/ProductEmptyState.test.tsx b/frontend/src/lib/components/ProductEmptyState/ProductEmptyState.test.tsx
index 84b2201841e2..382b4b190522 100644
--- a/frontend/src/lib/components/ProductEmptyState/ProductEmptyState.test.tsx
+++ b/frontend/src/lib/components/ProductEmptyState/ProductEmptyState.test.tsx
@@ -1,6 +1,7 @@
import { cleanup, render, screen } from '@testing-library/react'
import { expectLogic } from 'kea-test-utils'
+import { pngHoggie } from 'lib/brand/hoggies'
import { FEATURE_FLAGS } from 'lib/constants'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { preflightLogic } from 'scenes/PreflightCheck/preflightLogic'
@@ -13,6 +14,8 @@ import { AccessControlLevel, AccessControlResourceType, AppContext } from '~/typ
import { ProductEmptyState } from './ProductEmptyState'
import type { ProductEmptyStateConfig } from './types'
+const Hedgehog = pngHoggie({ src: 'hedgehog.png', aspectRatio: 1 })
+
const config: ProductEmptyStateConfig = {
productKey: ProductKey.EXPERIMENTS,
productName: 'Experiments',
@@ -161,4 +164,21 @@ describe('ProductEmptyState', () => {
expect(!!screen.queryByText('Base hint')).toBe(!applied)
expect(screen.getByText('Headline')).toBeTruthy()
})
+
+ // `beside` renders a pair and a container query hides one of them at any width. A lazy
+ // image with no layout box has nothing to intersect, so the browser can leave it unfetched
+ // until a resize reveals it. The Storybook image gate skips hidden images.
+ it.each([
+ ['above' as const, ['lazy']],
+ ['beside' as const, ['eager', 'eager']],
+ ])('renders a %s hedgehog loading as %s', (hedgehogPlacement, expected) => {
+ const { container } = render(
+ <ProductEmptyState config={{ ...config, hedgehog: Hedgehog, hedgehogPlacement }} mode="needs-setup" />
+ )
+
+ const loading = Array.from(container.querySelectorAll('img[src="hedgehog.png"]')).map((image) =>
+ image.getAttribute('loading')
+ )
+ expect(loading).toEqual(expected)
+ })
})
diff --git a/frontend/src/lib/components/ProductEmptyState/ProductEmptyState.tsx b/frontend/src/lib/components/ProductEmptyState/ProductEmptyState.tsx
index 36da8cb98096..a9f00972b56c 100644
--- a/frontend/src/lib/components/ProductEmptyState/ProductEmptyState.tsx
+++ b/frontend/src/lib/components/ProductEmptyState/ProductEmptyState.tsx
@@ -235,7 +235,12 @@ export function ProductEmptyState({ config: baseConfig, mode, preview = false }:
// At this width the illustration still leaves the copy 32rem, about what
// an `above` empty state gives it. Below it the column drops the wide
// illustration and falls back to the small one above the product name.
- <Hedgehog className="hidden w-72 shrink-0 @min-[52rem]/product-empty-state-copy:block" />
+ // `beside` renders this pair, and the container query puts exactly one of
+ // the two in `display: none`, so both must load eagerly.
+ <Hedgehog
+ className="hidden w-72 shrink-0 @min-[52rem]/product-empty-state-copy:block"
+ loading="eager"
+ />
) : null}
<div className="flex min-w-0 max-w-[36rem] flex-col justify-center gap-4">
<div className="flex flex-col items-start gap-3">
@@ -247,6 +252,7 @@ export function ProductEmptyState({ config: baseConfig, mode, preview = false }:
// more width than an `above` hedgehog to stay legible here.
hedgehogBeside ? 'w-48 @min-[52rem]/product-empty-state-copy:hidden' : 'w-32'
)}
+ loading={hedgehogBeside ? 'eager' : 'lazy'}
/>
) : null}
<div className="inline-flex items-center gap-2.5 text-4xl font-bold [&_svg]:text-[2.25rem]">
diff --git a/frontend/src/lib/components/ProductEmptyState/types.ts b/frontend/src/lib/components/ProductEmptyState/types.ts
index 42255f919b38..554c939232d8 100644
--- a/frontend/src/lib/components/ProductEmptyState/types.ts
+++ b/frontend/src/lib/components/ProductEmptyState/types.ts
@@ -1,6 +1,7 @@
import type { LogicWrapper } from 'kea'
-import type { ComponentType, CSSProperties, ReactNode } from 'react'
+import type { ComponentType, ReactNode } from 'react'
+import type { HoggiePngProps } from 'lib/brand/hoggies'
import type { RestrictionScope } from 'lib/components/RestrictedArea'
import type { FeatureFlagKey, TeamMembershipLevel } from 'lib/constants'
@@ -113,7 +114,7 @@ export interface ProductEmptyStateConfig {
/** Dark-mode accent override; falls back to `accentColor` */
accentColorDark?: string
/** A `pngHoggie(...)`-wrapped hedgehog, rendered above the product name */
- hedgehog?: ComponentType<{ className?: string; style?: CSSProperties }>
+ hedgehog?: ComponentType<Pick<HoggiePngProps, 'className' | 'style' | 'loading'>>
/**
* Where the hedgehog sits: `above` (default) is a small illustration above the
* product name; `beside` renders it large next to the text and install command,