fix: ensure the region gets populated while site creation#2329
Open
khushboo5723 wants to merge 2 commits intomainfrom
Open
fix: ensure the region gets populated while site creation#2329khushboo5723 wants to merge 2 commits intomainfrom
khushboo5723 wants to merge 2 commits intomainfrom
Conversation
|
This PR will trigger a patch release when merged. |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The Tokowaka dashboard recently started rendering a
Regioncolumn for eachmonitored site (project-elmo-ui-data commit
cb98b3a, Apr 29). The column ispopulated from
Site.regionreturned byGET /sites, but ~84% of rows areempty: of the 7 code paths in
spacecat-api-servicethat callSite.create(),only 2 (
plg-onboarding.jsand the Slackonboardcommand) currently invokelocale detection. The remaining 5 paths create sites with
region: ""andlanguage: ""— and have done so since theregionattribute was added inspacecat-shared-data-accessPR #995 (Oct 2025).This PR plugs those 5 holes so new sites going through any creation flow get
their locale detected at creation time.
Changes
New helper:
src/support/locale.jsensureSiteLocale(site, baseUrl, log)is the shared glue arounddetectLocalefrom@adobe/spacecat-shared-utils:site.getLanguage()andsite.getRegion()are both already set(idempotent — won't overwrite caller-supplied values).
detectLocale({ baseUrl }).site.setLanguage(...)/site.setRegion(...)only for fields thatare currently empty AND for which detection returned a non-empty value.
site.save()if anything changed.enrichment is best-effort and must not block site creation.
Difference from the existing PLG/Slack-onboard pattern: the catch block
does NOT fall back to writing
'US'/'en'. When detection genuinely fails(unreachable host, bot-blocked, timeout) the site is left with empty region
rather than fake-classified as US. Empty values are honest and can be filled
by a future backfill; fake values silently corrupt regional metrics.
5 call-sites wired
src/controllers/sites.jsPOST /sitessrc/support/slack/commands/add-site.jsadd sitesrc/support/slack/actions/approve-site-candidate.jssrc/controllers/llmo/llmo-onboarding.jscreateOrFindSite(used by LLMO onboarding)src/controllers/llmo/llmo.jscreateOrUpdateStageEdgeConfig(stage-domain creation)Each is a single
await ensureSiteLocale(site, baseUrl, log);line rightafter
Site.create(...).Tests
test/support/locale.test.js: 19 cases covering every branch of thehelper at 100% coverage (verified via
c8 --include='src/support/locale.js').repo convention used in
cdn-detection.test.js,plg-onboarding.test.js,etc.). Each updated test asserts the helper is invoked with the freshly
created site and the correct base URL.
Out of scope (follow-ups)
detectLocale's internal'US'/'en'fallback (inspacecat-shared-utils/src/locale-detect/locale-detect.js:61-62,pre-existing since PR fix(deps): update external fixes #1006). When fetch succeeds but no indicator
(TLD/path/hreflang/htmlLang/headers/metaTag/content) fires,
detectLocalereturns
{region:'US', language:'en'}. Our helper trusts that result, soplain
.comsites with no localization markup still get classified as US.Proper fix is to add a
{ allowFallback: false }option upstream so callerscan opt out — separate PR to
spacecat-shared.created from now on. A one-off script that iterates
Site.all(), callsensureSiteLocaleon each empty-region site, and saves will be a separatePR once we confirm the new write path works in stage.
plg-onboarding.jsandsupport/utils.jsto use the newhelper instead of their inline duplicates of the same pattern. Deferred
to keep this PR scoped; both already have the (problematic)
'US'/'en'catch fallback that we'd want to remove in lockstep.
Test plan
npx mocha --spec='test/support/locale.test.js' --spec='test/controllers/sites.test.js' --spec='test/support/slack/commands/add-site.test.js' --spec='test/support/slack/actions/approve-site-candidate.test.js' --spec='test/controllers/llmo/llmo-onboarding.test.js' --spec='test/controllers/llmo/llmo.test.js'(the 14 pre-existing failures in the full repo suite — 9× detectedCdn
Joi, 1× LlmoController before-all hook, 4× PLG timeouts — are
unrelated to this change and reproduce on
main)POST /sitesagainst stage):-
https://www.bmw.de→region: "DE"(TLD signal)-
https://www.toyota.co.jp→region: "JP"-
https://www.lemonde.fr→region: "FR"-
https://this-host-does-not-exist-xyz.invalid→region: ""(no fallback to US — this is the key behavior change)@spacecat add site https://www.lemonde.fr, then@spacecat get site https://www.lemonde.fr→ confirmregion: "FR".modify the saved record.
{baseURL: ..., region: "GB"}isnot overwritten by detection.
llmo-usage-data-YYYY-MM-DD.jsonin
project-elmo-ui-data/usage-tracking/shows reduced empty-regionrate for sites created in the last 24h.