What to build
apps/storefront/src/middleware/storefront.ts runs on every request and has three hot-path issues:
-
Duplicate Shop.findByDomain per request — line 64 (getHostname()) calls Shop.findByDomain(hostname, { sensitiveData: true }) unconditionally. Line 213 calls it again for cookie-less (first-time) visitors. That's two MongoDB round-trips for every new-user request that should be deduped (e.g. memoize within the request scope via react.cache() or pass the resolved shop down).
-
Sequential locale resolution — line 213-215 chains Shop.findByDomain → ShopifyApiClient → LocalesApi sequentially. The shop lookup gates the rest, but LocalesApi doesn't strictly depend on the Shopify client construction step. Parallelize where the dependency graph allows.
-
LOCALE_SLASH_TEST regex flag — line 139 declares the RegExp with the g flag immediately after a comment block (lines 132-137) that explicitly warns "Don't add the g flag here." Current .match() usage is incidentally safe, but the flag contradicts the documented invariant and is a latent footgun if anyone later switches to .test().
Acceptance criteria
Blocked by
None — can start immediately.
References
apps/storefront/src/middleware/storefront.ts:64,139,213-215
What to build
apps/storefront/src/middleware/storefront.tsruns on every request and has three hot-path issues:Duplicate
Shop.findByDomainper request — line 64 (getHostname()) callsShop.findByDomain(hostname, { sensitiveData: true })unconditionally. Line 213 calls it again for cookie-less (first-time) visitors. That's two MongoDB round-trips for every new-user request that should be deduped (e.g. memoize within the request scope viareact.cache()or pass the resolved shop down).Sequential locale resolution — line 213-215 chains
Shop.findByDomain → ShopifyApiClient → LocalesApisequentially. The shop lookup gates the rest, butLocalesApidoesn't strictly depend on the Shopify client construction step. Parallelize where the dependency graph allows.LOCALE_SLASH_TESTregex flag — line 139 declares the RegExp with thegflag immediately after a comment block (lines 132-137) that explicitly warns "Don't add thegflag here." Current.match()usage is incidentally safe, but the flag contradicts the documented invariant and is a latent footgun if anyone later switches to.test().Acceptance criteria
Shop.findByDomaincall per request, not two (verify with a middleware test that asserts the call count)Shop.findByDomainLOCALE_SLASH_TESTno longer has thegflag, matching its surrounding documentationstorefront.test.tsstill pass and gain coverage for the dedupBlocked by
None — can start immediately.
References
apps/storefront/src/middleware/storefront.ts:64,139,213-215