Skip to content

[#12] feat(loader): warm and invalidate CloudFront cache on publish#206

Open
jameswillis99 wants to merge 5 commits intomasterfrom
feat/cloudfront-invalidation-on-publish
Open

[#12] feat(loader): warm and invalidate CloudFront cache on publish#206
jameswillis99 wants to merge 5 commits intomasterfrom
feat/cloudfront-invalidation-on-publish

Conversation

@jameswillis99
Copy link
Copy Markdown
Collaborator

@jameswillis99 jameswillis99 commented Apr 7, 2026

What does this MR do?

Extends the prefillCloudfront worker to warm CloudFront's versioned cache and trigger per-project scoped CreateInvalidation on published loader paths after each project publish. This allows the loader_published CloudFront cache TTL to be safely raised from 30s to 300s — reducing origin load and storefront latency without serving stale content.

Per-project scoping is achieved via a CloudFront Function that embeds sorted project IDs into the code/published URL path at the edge, making cache entries targetable by project.

Changes

  • platform/wab/package.json — add @aws-sdk/client-cloudfront dependency
  • AppServer.ts — add /api/v1/loader/code/published/:projectIds route alongside the existing one; handles CloudFront-rewritten URLs (e.g. /published/aaa,zzz?projectId=aaa&...) with the same handler — no behaviour change, req.query.projectId is read as normal
  • prefill-cloudfront.ts — after bundle generation and isPrefilled: true:
    1. GET each versioned CDN URL to warm CloudFront's versioned cache (Promise.allSettled, 30s timeout per request)
    2. Call CloudFrontClient.CreateInvalidation with per-combination paths for code/published (e.g. /published/p1,p2,p3*) and per-project paths for repr-v2/v3/html (which already have :projectId in the route)
  • prefill-cloudfront.spec.ts — tests for ordering (isPrefilled before warming, warming before invalidation), warming URL construction, per-project invalidation paths, env var guard, warming failure handling, and resilience when CloudFront API throws

Design decisions

Ordering is critical: isPrefilled is set before CDN warming so origin serves pre-computed S3 bundles during warm requests. Warming precedes invalidation so clients following the published→versioned redirect always get a cache hit.

Per-project invalidation for code/published: CloudFront invalidations match on URI path only — query strings are ignored. A CloudFront Function rewrites /published?projectId=aaa&projectId=zzz/published/aaa,zzz?projectId=aaa&... at the edge. Invalidation then targets /published/aaa* which matches all combinations containing aaa without touching other projects.

repr/html already scoped: repr-v2/published/:projectId, repr-v3/published/:projectId, and html/published/:projectId/:component already have the project ID in the path — no CloudFront Function needed for those.

Non-fatal failures: Warming fetches use Promise.allSettled with a 30s timeout; failures are logged with a count but don't block the publish flow. Invalidation errors are also non-fatal.

Environment guard: Everything is skipped when CLOUDFRONT_DISTRIBUTION_ID is unset — local dev and non-CDN environments are unaffected.

Testing

Unit tests cover: correct warming fetch URLs per publishment, isPrefilled set before first fetch, invalidation fired after all warm fetches, per-combination code/published paths (e.g. /published/p1,p2,p3*), per-project repr/html paths, warning logged on warming failures, no-op when env var absent, graceful handling of CloudFront API errors.

After generating bundles, warm the CloudFront versioned cache via GET
requests before triggering a CreateInvalidation on published paths.
This allows the loader_published cache TTL to be extended from 30s
to 300s without serving stale content after a project publish.

Relates-to: https://gitlab.elasticpath.com/commerce-cloud/ncl-projects/paragon/plasmic-terraservices/-/issues/12
Add /api/v1/loader/code/published/:projectIds route to handle
CloudFront-rewritten URLs (sorted project IDs embedded in path by the
companion CF Function). Update invalidation paths to be per-combination
for code/published and per-project for repr/html endpoints.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants