Skip to content

fix(event): resolve event image URLs (populate filename + prefix origin) #33

Closed

Description

@Ardnived

Summary

The event detail view throws (hits the ErrorBoundary) for any event that has
images. Root cause: getEvent populates the image relation with { url, thumbnailURL, alt }
but not filename — and SahajCloud's image url is a virtual field derived
from filename
, so it comes back null, which fails the url: z.string() parse.
A secondary bug: the resolved dev URL is relative (/api/images/file/…) and is
consumed raw, so even after the crash is fixed images 404 unless prefixed with the
SahajCloud origin.

Supersedes the earlier diagnosis in this issue (relax the schema). Relaxing
EventImageSchema.url would only swap the crash for silently-missing images —
it doesn't address why url is null. Confirmed against the SahajCloud source +
the running local backend (see Evidence).

Observed behavior

  1. Open an event that has ≥1 image, e.g. http://localhost:5173/russia/voronezh/13.
  2. getEvent (fetch.ts:288-320) fetches /events/:id
    (depth 1) with populate: { images: { url: true, thumbnailURL: true, alt: true } }
    (fetch.ts:313) and runs EventDocSchema.parse(response.data)
    (fetch.ts:318).
  3. The image's url is null, so EventImageSchema (event.ts:44, url: z.string())
    fails → ZodError. useSuspenseQuery in EventView.tsx
    re-throws it on render → error page.

Only events with images fail: images uses .default([]) (event.ts:112),
so an empty/absent array never runs per-image validation.

Root cause

On SahajCloud the images collection's url is a virtual: true field whose
afterRead hook builds the URL from data.filename and returns undefined when
filename is absent (SahajCloud/src/plugins/storage/urlFields.ts:116 virtualUrlField;
falsy-filename guard at :118). Our populate omits filename, so the hook has
nothing to build from and url resolves to null. Two further facts from the
backend:

  • thumbnailURL and sizes do not exist on this collection — imageSizes
    were removed in favour of Cloudflare Images flexible variants
    (SahajCloud/src/collections/Images/Images.ts). Requesting them is dead weight
    (always null).
  • URL shape is environment-dependent: absolute in prod
    (https://imagedelivery.net/<hash>/<filename>/public) and relative in dev
    (/api/images/file/<filename>, urlFields.ts:15 getLocalFallbackUrl). So any
    prefixing must be conditional — only relative URLs need the origin.

Evidence (running local backend, event 13)

# populate url + filename  → url resolves
{"id":2,"filename":"picture-9-39a496daa3-3.jpg","url":"/api/images/file/picture-9-39a496daa3-3.jpg"}

# populate url only (today's behavior) → url null
{"id":2,"url":null}

# the file serves fine at the backend origin (relative URL just needs the origin)
GET http://localhost:3000/api/images/file/picture-9-39a496daa3-3.jpg → 200 image/jpeg 764KB

Expected behavior

An event with images renders its detail view without crashing, and its images
actually display (carousel + lightbox) with URLs pointing at the SahajCloud
origin / CDN.

Proposed changes

  1. Select filename so the virtual url resolves. In getEvent
    (fetch.ts:313) change the images populate to
    { url: true, filename: true, alt: true } — and drop thumbnailURL (doesn't
    exist). This alone fixes the crash: url becomes a valid string.
  2. Origin-prefix relative image URLs so they render. Image URLs are consumed
    raw as <img src> in the carousel slides
    (EventDetails.tsx:106-114)
    and the OG/JSON-LD image (EventMetadata.tsx:22).
    Add a small helper (e.g. in src/lib/shape/) that resolves an image URL against
    the SahajCloud origin — new URL(url, import.meta.env.VITE_SAHAJCLOUD_URL).href
    leaves absolute prod URLs untouched and prefixes relative dev URLs. Note the
    client baseURL is ${VITE_SAHAJCLOUD_URL}/api (client.ts:44),
    and the URL already contains /api/…, so prefix with the origin
    (VITE_SAHAJCLOUD_URL), not the axios baseURL, to avoid a doubled /api.
  3. Optional hardening (not the fix): drop the non-existent thumbnailURL
    from EventImageSchema (event.ts:43-48); and,
    defensively, tolerate/skip an image whose url is still null (mirroring the
    firstDate_tz / SafeUrlSchema.catch(null) patterns already in that file) so
    a future null can't crash the whole event read.

Acceptance criteria

  • Visiting an event that has images (e.g. /russia/voronezh/13) renders the
    detail view without throwing / hitting the ErrorBoundary.
  • The event's images actually display in the carousel + lightbox (URL resolves
    to the SahajCloud origin in dev / the CDN URL in prod — no 404 against the
    widget's own origin).
  • getEvent selects filename and no longer requests the non-existent
    thumbnailURL.
  • The URL helper leaves already-absolute (https://imagedelivery.net/…) URLs
    unchanged and prefixes relative (/api/images/file/…) ones.
  • Unit test (node-only vitest) for the URL helper (absolute passthrough +
    relative prefixing) and for EventDocSchema parsing the real image shape
    (filename + relative url).
  • Lint + typecheck + test:run + build pass.

Files affected

  • src/config/api/fetch.ts — getEvent images populate (add filename, drop thumbnailURL)
  • src/lib/shape/ — new image-URL resolver helper (origin-prefix)
  • src/components/organisms/EventDetails/EventDetails.tsx — apply helper to slide src
  • src/components/molecules/EventMetadata/EventMetadata.tsx — apply helper to OG/JSON-LD image
  • src/types/event.ts — EventImageSchema (drop thumbnailURL; optional defensive url)
  • co-located tests for the helper + schema

References

  • SahajCloud source of truth:
    • src/plugins/storage/urlFields.ts:116 (virtualUrlField, url built from filename), :15 (getLocalFallbackUrl)
    • src/plugins/storage/cloudflareImagesAdapter.ts:30 (getCloudflareImagesUrl → imagedelivery.net/<hash>/<filename>/public)
    • src/collections/Images/Images.ts (no imageSizes/thumbnailURL), src/collections/Events/Events.ts:176 (images upload, hasMany)
    • client select-required gate: src/plugins/usage/hooks.ts:117
  • Confirmed live: event 13 image url is null without filename, /api/images/file/… with it (serves 200).

Activity

  1. changed the title [-]fix(event): tolerate null image URL so image events don't crash[/-] [+]fix(event): resolve event image URLs (populate filename + prefix origin)[/+] on Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Hold Until

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions