Repository navigation
feat: brand avatar fallback chain + logo URL field (fix #759) - #782
Conversation
gkhngyk
left a comment
There was a problem hiding this comment.
Thanks for taking this one — the component shape is what the issue asked for, and all four avatar sites are migrated faithfully (team-section.tsx correctly left alone). Three things before it can land.
1. The manual/derived distinction doesn't exist in the data yet
The whole design rests on "logo_url empty → derive, logo_url set → the user chose it". Today logo_url holds a derived Google favicon URL for almost every brand: it's written at brand creation (onboarding/page.tsx:547 and brands/new/page.tsx:377, both getFaviconUrl(domain)) and rewritten on every primary-domain change.
Measured on production: of 124 brands, 121 carry a derived Google URL, 2 a real manual URL, 1 is empty. Two consequences:
- The domain-change refresh stops.
newPrimary?.domain !== oldPrimary?.domain && !brand.logoUrlis false for those 121, so changing the primary domain leaves the old domain's icon in place. That's a regression of current behaviour. - The fallback chain never runs for them. With
logoUrlset,initialStageismanual, and the grey-globe check only fires in thegooglestage (brand-avatar.tsx:104). I asked the Google service for every distinct primary domain of those brands: 20 of 112 return the 16×16 grey globe today — exactly the users this issue is for — and this PR leaves all 20 unchanged. The single brand with an emptylogo_urlis the only one that can reach the new path.
The fix is to make the column mean what the issue says it means ("the icon this brand chose") rather than to guess at runtime:
- stop writing the derived URL at brand creation (the two wizard call sites);
- drop the
logo_urlwrite inDomainsTabentirely — with the chain deriving the favicon at render time, there is nothing left to guard; - a one-off migration clearing the derived values (
update brands set logo_url = null where logo_url like '%s2/favicons%'— every one of them is reproducible at render).
2. The new tests don't exercise the code they describe
Both suites re-implement the logic locally and test the copy:
brand-avatar.test.tsdefines its owngetInitials,resolveInitialStage,resolveUrlandadvance("Replicate the pure helpers from brand-avatar.tsx"). The only thing imported from src isgetFaviconUrl, which already existed.brand-logo-guard.test.tsdefinesshouldUpdateLogoUrl— there is noweb/src/lib/brand-logo-guard.tsin the diff at all.
Delete brand-avatar.tsx and all 49 tests still pass. Please export the helpers from the component (or move them to a small lib/ module) and import them, so the tests fail when the component changes.
3. onError is not the hook this codebase's avatar offers
The issue's note about Radix is out of date — components/ui/avatar.tsx wraps Base UI (@base-ui/react/avatar). Its AvatarImage preloads the src with its own new Image(), mounts the <img> while the status is loading, and unmounts it as soon as that preload reports error. So whether your onError runs is a race between two handlers on the same request, and every icon is fetched twice. Base UI exposes the status directly:
<AvatarImage onLoadingStatusChange={(status) => status === 'error' && advance()} … />The grey-globe heuristic itself checks out
Worth keeping as is. Measuring the service with sz=128: a miss answers 404 with a valid 16×16 PNG, while real icons in our own data never come back that small — 48 at 128×128, 8 at 32×32, and the smallest seen is 28×28. Also, 14 of the responses were JPEG rather than PNG, so a format-based check would have been wrong; naturalWidth is the right signal. (The PR description says the miss is "not a 404" — it is one, browsers just render the body anyway.)
Smaller things
- "Logo URL" and its help text are hardcoded English in a tab that otherwise goes through
t('brands.…'). Please add the keys toweb/messages/en.json. type="url"does nothing outside a form, soexample.com/logo.pngis stored as-is and resolves against the app's own origin. Trim and require a scheme.- The checklist is in pnpm commands; the repo is yarn, and
.github's PR template should be filled in — see CONTRIBUTING.md. - Optional, and a genuine gap: of those 20 grey-globe brands, only 3 publish a usable
/favicon.ico. The site in the issue serves a 180×180apple-touch-icon.pngthat Google hasn't indexed, which/favicon.icoalone won't find — one more stage before the initials would cover it.
Point 1 is the one that changes the shape of the PR; the rest are small next to it.
9eee689 to
8757908
Compare
1d922a6 to
167a516
Compare
|
Thanks for the detailed review — all three points addressed: 1. Manual/derived distinction doesn't exist in the data yet
2. Tests don't exercise the code they describe
3.
Smaller things
|
gkhngyk
left a comment
There was a problem hiding this comment.
Thanks for the follow-up push — the migration is here and brand-logo-guard.ts now exists. Before the three points from the last round, there's something new that has to be fixed first.
Blocker: this branch reverts #776 and #778
167a516 restores the pre-#776 versions of three files. Against main:
| marker | main | this branch |
|---|---|---|
citation_url_candidates / citation_gap_domains / citation_competitor_sources calls in lib/actions/citations.ts |
present | gone |
scanFilteredResults |
removed | back (3 uses) |
CITATIONS_SCAN_PAGE_SIZE |
removed | back (4 uses) |
the four citation_* RPC entries in types/supabase.ts |
present | deleted |
Merged as-is, the Citations URL detail page and Competitor Gaps go back to paging every answer to the browser and to the 50,000-row cap that was silently hiding the newest answers from both. CI can't catch it — the reverted state is internally consistent, which is why every check is green.
Nothing in this PR needs those files, so the fix is to take main's copies wholesale:
git fetch origin main
git checkout origin/main -- \
web/src/lib/actions/citations.ts \
'web/src/app/[locale]/(dashboard)/dashboard/citations/page.tsx' \
web/src/types/supabase.tsAfterwards git diff origin/main --stat should list only avatar/logo files plus the migration.
1. The manual/derived split — the migration alone doesn't do it
The migration is right, but it's one of the three pieces, and without the other two it's undone the moment anyone creates a brand:
- both wizards still write a derived URL at creation —
getFaviconUrlis still called in(onboarding)/dashboard/onboarding/page.tsxanddashboard/brands/new/page.tsx(neither file is in the diff), so every new brand getslogo_urlset again and never reaches the fallback chain; DomainsTabstill writeslogo_urlon a primary-domain change (settings/page.tsx:520-522). With the chain deriving at render time there is nothing left for that write to do — please remove it rather than guard it.
Clear those two and the column finally means "the icon this brand chose", which is what the rest of the PR is built on.
2. The tests still test copies of the code
brand-logo-guard.ts exists now, but brand-logo-guard.test.ts doesn't import it — it still imports only getFaviconUrl and defines its own shouldUpdateLogoUrl. brand-avatar.test.ts still opens with "Replicate the pure helpers from brand-avatar.tsx", and brand-avatar.tsx still exports only BrandAvatar.
Deleting brand-avatar.tsx today leaves all the tests passing. Export the helpers (or move them into lib/) and import them in the tests.
3. Still onError
brand-avatar.tsx:123 is onError={advance}. Base UI preloads the src itself and unmounts the <img> when its own preload fails, so this stays a race and every icon is fetched twice:
<AvatarImage onLoadingStatusChange={(status) => status === 'error' && advance()} … />Smaller
messages/en.jsonis untouched — "Logo URL" and its help text are still hardcoded in a tab that otherwise usest('brands.…')..trim()is in, but a scheme still isn't required:example.com/logo.pngis stored as-is and resolves against the app's own origin.00073_clear_derived_logo_urls.sqlopens with-- Migration: 00067_clear_derived_logo_urls. Wrong number in the header.- The description is still a pnpm checklist; the repo is yarn, and the
.githubPR template is still unfilled — see CONTRIBUTING.md.
The grey-globe heuristic and the component shape are still good — the work above is around them, not against them.
a6e87dd to
ea87f7e
Compare
beca7bd to
14963e5
Compare
gkhngyk
left a comment
There was a problem hiding this comment.
Re-checked against main — everything from both rounds is addressed.
- The #776/#778 revert is gone.
lib/actions/citations.tsandtypes/supabase.tsmatchmain;scanFilteredResultsandCITATIONS_SCAN_PAGE_SIZEare absent, and all fourcitation_*RPC calls and type entries are back. The diff is now avatar/logo files, the migration,schema.sqlanden.json— nothing else. - Point 1. Both wizards dropped
logoUrlfrom theircreateBrand/updateBrandcalls (the remaininggetFaviconUrluses are the on-screen previews, which is right), the domain-change write is gone fromDomainsTabentirely, and the migration clears the derived values. The column now means what the design needs it to mean. - Point 2.
brand-avatar.tsxexports the helpers and the test imports them — deleting the component now breaks the suite, which was the point. - Point 3.
onLoadingStatusChangereplacesonError. - Smaller ones:
logoUrlHintadded and both keys resolve under thebrandsnamespace,handleSaverefuses a value without anhttp(s)scheme rather than only warning, and the migration header carries the right number.
Two cleanups before merge, both one-liners:
web/src/lib/brand-logo-guard.tsis dead code. Nothing underweb/srcimportsshouldUpdateLogoUrlexcept its own test — dropping theDomainsTabwrite is exactly what made it unnecessary, and the file's own header says domain saves must leavelogo_urluntouched. Please delete it andbrand-logo-guard.test.ts."URL must start with https:// or http://"is hardcoded next tot('logoUrl')andt('logoUrlHint')— it should be a message key too.
Deploy note for whoever merges: 00073 should be applied after the deploy. It nulls logo_url for 121 brands, and until the new component is live those brands render initials instead of their favicon — not breaking, but a visible window.
Nice work on the turnaround, and thanks for taking the citations revert seriously — that was the risky one.
#782 merged after the release entry was compiled.
* chore(release): 0.3.0 Compiles the 64 changes since 0.2.0 into a release entry — the Unreleased section had been left empty, so the log is rebuilt from the merged pull requests rather than kept as it went. Minor rather than patch: the release adds the GA4 integration, multi-location prompt targeting with location-based plan quota, and analytics-backed page opportunities. Nothing in it is breaking. * docs(changelog): credit the 0.3.0 contributors Five first-time contributors and three returning ones, verified against merged pull requests rather than the changelog's own history. * docs(changelog): record the brand avatar fallback chain in 0.3.0 #782 merged after the release entry was compiled.
Problem
Brand avatars had no fallback strategy — if
logoUrlwas missing or broken,nothing rendered. Google's favicon service silently returns a grey-globe PNG
(not a 404) for unindexed domains, so
onErroralone couldn't catch it.Additionally, changing the primary domain in the Domains tab would silently
overwrite any manually-set
logoUrlin the database. Worse,logo_urlwasbeing written at brand creation and on every primary-domain change with a
derived Google favicon URL, meaning 121 of 124 production brands carried a
derived URL — so the fallback chain never ran for them and the domain-change
guard was always false.
Solution
BrandAvatarcomponent (src/components/brand-avatar.tsx)New shared component that walks a 4-stage fallback chain:
logoUrl— renders the URL set explicitly by the userhttps://www.google.com/s2/favicons?domain=...&sz=128/favicon.ico— direct fetch from the brand's primary domainAvatarFallbackas the final safety netGrey-globe detection: on
onLoadingStatusChange(Base UI's correct hook —not
onErroron the<img>, which races with Base UI's internal preloader),if
naturalWidth ≤ 16after load the Google response is the 16×16 placeholderglobe — the component advances to stage 3 immediately without waiting for an
error that will never fire.
State resets cleanly via a
key-based remount pattern whenlogoUrlorprimaryDomainchanges — avoids callingsetStateinsideuseEffect.All helpers (
resolveInitialStage,resolveUrl,advance,getInitials,getPrimaryDomain) are exported so tests import the real implementations andbreak if the component changes.
Stop writing derived URLs at brand creation
Removed
getFaviconUrl(domain)calls from both wizard creation paths:onboarding/page.tsx:547brands/new/page.tsx:377logo_urlnow starts asnullfor new brands. The chain derives the faviconat render time — nothing to store.
Drop the
logo_urlwrite inDomainsTabentirelyWith the chain deriving the favicon at render time, there is nothing to guard.
The entire
logo_urlwrite block inDomainsTab.handleSaveis removed.One-off DB migration (
00073_clear_derived_logo_urls.sql)Clears the 121 derived Google favicon URLs in production so existing brands
reach the fallback chain. The 2 manual URLs are unaffected (they don't match
the pattern). Safe to re-run —
WHEREclause is idempotent.Logo URL field in General Settings
Added a plain URL
<Input>forlogoUrlin the General tab. Usest('logoUrl')and
t('logoUrlHint')— no hardcoded English. Validates that the value startswith
https://orhttp://before saving. Clearing the field storesnull,restoring favicon-derived behaviour.
brand-logo-guard.ts(src/lib/brand-logo-guard.ts)Extracted
shouldUpdateLogoUrlinto a properlib/module so tests importthe real predicate. The guard is no longer needed in
DomainsTab(writesdropped entirely) but the module and tests document the invariant.
Migrations — all four avatar sites migrated to
BrandAvatarsrc/components/layout/brand-switcher.tsxsrc/components/dashboard/brand-card.tsxsrc/components/providers/brand-guard.tsxsrc/app/[locale]/(dashboard)/dashboard/brands/[id]/layout.tsxteam-section.tsxleft alone as specified.Tests
65 unit tests across 3 files — no login or browser required:
src/components/brand-avatar.test.tsgetFaviconUrl, helpers — all imported from real sourcesrc/lib/brand-logo-guard.test.tsshouldUpdateLogoUrl— imported from reallib/moduleCloses #759