Skip to content

fix(upload): use the small format as the 2x candidate in the media library grid - #28007

Merged
Adzouz merged 2 commits into
strapi:developfrom
santichausis:fix/media-library-grid-hidpi-preview
Nov 17, 2026
Merged

Adzouz merged 2 commits into
strapi:developfrom
santichausis:fix/media-library-grid-hidpi-preview

Conversation

@santichausis

@santichausis santichausis commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

What does it do?

In the new Media Library grid (AssetPreview in AssetsGrid.tsx), the image card now sets srcSet with the small format as the 2x candidate. src is unchanged, so 1x screens keep loading the light thumbnail.

srcSet is only added when both thumbnail and small exist:

  • small is only generated when responsive formats are enabled and the original is larger than the breakpoint.
  • Without a thumbnail, src is already the original, which is larger than small.

The existing cache-buster is applied to the small URL as well, and skipped for signed URLs, same as for src.

small is also declared (optional) in the thumbnail-only variant of File['formats'] so the grid can read it without a cast.

Why is it needed?

thumbnail is generated in a fixed 245x156 box (fit: 'inside'), so a portrait image ends up around 117x156. The card is ~250 CSS px wide, ~500 device px on a HiDPI screen, so the preview is upscaled 3-4x and looks blurry, even though small already exists for the same asset.

As agreed in the issue, this uses a srcset instead of switching to small unconditionally, and leaves the legacy components untouched.

How to test it?

  1. Run a project with default upload settings (responsive formats enabled).
  2. Upload a portrait image larger than 500px, e.g. 1500x2000.
  3. Open the Media Library grid on a HiDPI screen (or emulate DPR 2 in devtools): the card now loads the small_ file; at DPR 1 it loads the thumbnail_ file.
  4. Upload a small image (< 500px) or disable responsive formats: no srcset is rendered and the card behaves as before.

Unit tests added in AssetsGrid.test.tsx (srcset present, absent without small, absent without thumbnail, cache-buster / signed URL handling).

Related issue(s)/PR(s)

Fix #27986


Fixes CPR-604

…brary grid

The grid always rendered formats.thumbnail, a fixed 245x156 box, so previews were upscaled and blurry on HiDPI screens. Add a srcset with the small format as the 2x candidate. 1x screens keep the thumbnail. Nothing is added when small was not generated or there is no thumbnail.

@Adzouz Adzouz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @santichausis, thanks for the PR 🙏 I tested and it looks good on my side ✅
One nit but it's not part of this PR so I'll approve it

// Offer the `small` format as the 2x candidate; 1x screens keep the light
// thumbnail. `small` only exists for images larger than its breakpoint (and
// when responsive formats are enabled), so it is optional.
const rawHiDpiURL = formats?.thumbnail?.url

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking, fine as a follow-up: portrait images are still soft on 1x screens. StyledImage uses object-fit: cover, so a ~117px-wide portrait thumbnail is stretched to fill the ~250px card, about 2x upscale even at DPR 1.

Width descriptors would cover that case too, because the browser would pick small whenever the thumbnail is narrower than the card:

srcSet={`${thumbURL} ${formats.thumbnail.width}w, ${smallURL} ${formats.small.width}w`}
sizes="250px"

This needs width on the formats (the thumbnail-only type variant doesn't have it) and a sizes value that matches the grid column.

@Adzouz Adzouz self-assigned this Nov 17, 2026
@Adzouz Adzouz added source: core:upload Source is core/upload package pr: fix This PR is fixing a bug needs-qa generates AI QA checklist qa-done Applied when a PR passes QA community Changes and fixes created by community members labels Nov 17, 2026
@Adzouz Adzouz added this to the 5.62.1 milestone Nov 17, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been added to our community triage board as CPR-604.

@github-actions

Copy link
Copy Markdown
Contributor

AI-generated QA checklist

Scope / Impact

  • Package: @strapi/upload (admin only). 3 files, +80/-0 — no server, DB, or API changes.
  • AssetPreview in packages/core/upload/admin/src/pages/Assets/components/AssetsGrid.tsx:370 now renders srcSet="<small_ url> 2x" alongside the existing src (thumbnail_, falling back to the original). The 2x candidate is emitted only when a thumbnail format exists; otherwise src is already the original.
  • The existing updatedAt cache-buster (?v=<ts>, CMS-1237) is applied to the small URL too, and skipped for signed URLs (isUrlSigned) so the signature is not invalidated. crossOrigin handling is unchanged.
  • packages/core/upload/shared/contracts/files.ts:78 adds an optional small?: { url: string } to the thumbnail-only File['formats'] variant — type-only, but it widens a shared contract consumed by admin code.
  • Affected surfaces: AssetsGrid is rendered by the Media Library grid view (AssetsView → AssetsPage) and by AssetPickerDialog (the Content Manager media field picker, both the browse grid and the selected-assets grid). All of these are the new Media Library, which is the default; the useLegacyMediaLibrary feature flag path (packages/core/upload/admin/src/legacy/) is untouched.
  • Main risk: extra bandwidth on HiDPI screens (browsers fetch the small_ candidate instead of thumbnail_), and broken/404 or unauthorized previews if formats.small.url is handled differently from the thumbnail by a provider (signed/private buckets, CDN, custom breakpoints config).

Manual QA

  • Default upload settings (responsive friendly formats on). Upload a portrait image clearly larger than the small breakpoint (e.g. 1500x2000). In the Media Library grid view at DPR 2 (devtools device toolbar or a HiDPI display), confirm the Network tab shows small_<hash> being fetched and the card preview is visibly sharper than before.
  • Same asset at DPR 1: confirm only thumbnail_<hash> is fetched — no small_ request (this is the "1x screens keep the light thumbnail" claim in the description).
  • Inspect the <img> in the grid: src is the thumbnail_ URL, srcset is <small_ URL> 2x, and both carry the same ?v=<updatedAt ms> cache-buster.
  • Upload an image smaller than the small breakpoint (e.g. 300x200) or turn responsive friendly formats off in Media Library settings: confirm no srcset attribute is rendered and the card behaves exactly as before the PR.
  • Open the Content Manager media field picker (AssetPickerDialog) — browse grid and the selected-assets strip — and confirm previews still render, are sharper at DPR 2, and nothing is broken/blank.
  • Regression — Replace media: replace an existing image with a different one and confirm the grid card updates immediately at both DPR 1 and DPR 2 (the cache-buster must bust the small_ candidate, not just the thumbnail).
  • Regression — list/table view, asset details drawer/preview, crop editor, and the upload dialog previews are unchanged.
  • Regression with useLegacyMediaLibrary enabled: legacy Media Library grid is untouched and renders no srcset.

Edge Cases

  • Private/signed provider (e.g. S3 with isPrivate() === true): confirm the srcset URL keeps its signature (no ?v= appended), the image loads, and crossOrigin="anonymous" does not cause a CORS failure for the 2x candidate. signFileUrls signs each format, so formats.small.url should be signed too — verify it actually is in the API response.
  • Public remote provider (unsigned S3/Cloudinary, no bucket CORS rule): confirm the 2x candidate still loads without crossOrigin (see Content Manager image preview fails with CORS error in Chrome/Safari for private S3 buckets (signed URLs) #26581 regression).
  • Custom plugin::upload.breakpoints config that renames or removes the small key (e.g. only large/medium): confirm no srcset and no broken image.
  • Non-image mime types (PDF, video, audio) and images without formats (SVG, tiny PNG): icon/original preview path unchanged, no srcset.
  • Assets with no updatedAt (legacy rows): the cache-buster is skipped for both src and srcset.
  • Asset whose original is only slightly above the breakpoint (e.g. 520x700): the small candidate is still smaller than 2x the ~250px card — confirm it is at least not worse than the current thumbnail.
  • Large library / slow network at DPR 2: scroll the grid and sanity-check that the heavier small_ files do not cause a noticeable loading regression (this is the main cost of the change).
  • Browser coverage for srcset 2x selection: Chrome, Firefox, Safari/WebKit — Safari in particular is conservative about re-picking candidates on DPR change.

Test Setup

  • A dev app (e.g. examples/getstarted) with the local upload provider for the happy path, plus one run against a private/signed provider (S3 or equivalent) to cover the signed-URL branch.
  • Media Library Settings → Responsive friendly upload toggled on for the main scenario and off for the negative case.
  • Fixtures: one portrait image > 500px (e.g. 1500x2000), one landscape > 500px, one image < 500px, one SVG, one non-image file (PDF).
  • A content type with a single media field and a multiple-media field, to exercise AssetPickerDialog.
  • Devtools device emulation at DPR 1 and DPR 2 (or an actual HiDPI display).
  • Feature flag useLegacyMediaLibrary off (default) for the main pass, on for the legacy regression check.
  • No special role/permission setup; any user with Media Library read access is sufficient.

Possible QA Automation

Candidate Test Layer Priority Signal
srcset present/absent plus cache-buster and signed-URL behaviour in AssetPreview Frontend (already added in AssetsGrid.test.tsx) Recommended 4 unit tests ship with the PR and cover the branch logic
AssetPickerDialog grid renders the same srcset (shared component, not covered by the new tests) Frontend Recommended AssetsGrid is reused by the picker but only the Media Library page is asserted
small added to the thumbnail-only File['formats'] contract still type-checks consumers Unit / yarn test:ts Optional Contract change is additive and optional, low risk
E2E: Media Library grid image src/srcset attributes after upload (tests/e2e/tests/upload domain) E2E Optional Attribute assertion is cheap; actual DPR-2 pixel sharpness is not practical to assert
API: upload response includes formats.small when responsive formats are on and the original exceeds the breakpoint API Integration Optional Server behaviour is pre-existing and unchanged by this PR

Assumptions / Gaps

  • The diff does not change how formats.small is produced or signed; the checklist assumes the server already returns a usable (and, for private providers, signed) small.url whenever thumbnail exists and the original exceeds the 500px small breakpoint. Worth confirming on a private provider since that path is the least covered.
  • breakpoints are configurable (plugin::upload.breakpoints) and the code hardcodes the small key, so custom breakpoint sets simply yield no srcset. That is a silent no-op rather than a bug, but it is not covered by the added tests.
  • No visual-regression or performance baseline is included, so the "sharper preview" and "not slower" claims rest on manual observation.
  • The PR already carries a qa-done label; this checklist is generated for the record and may overlap with QA that has already been performed.

QA Resolution

Add qa-done when completed, or qa-skipped if intentionally skipped.

@draft-release draft-release Bot modified the milestones: 5.63.0, 5.63.1 Nov 17, 2026
@Adzouz
Adzouz merged commit 697573d into strapi:develop Nov 17, 2026
109 of 111 checks passed
@draft-release draft-release Bot mentioned this pull request Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community Changes and fixes created by community members needs-qa generates AI QA checklist pr: fix This PR is fixing a bug qa-done Applied when a PR passes QA source: core:upload Source is core/upload package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Media Library grid shows blurry previews: admin always uses the 245×156 thumbnail format, even on HiDPI screens

2 participants