[#12] feat(loader): warm and invalidate CloudFront cache on publish#206
Open
jameswillis99 wants to merge 5 commits intomasterfrom
Open
[#12] feat(loader): warm and invalidate CloudFront cache on publish#206jameswillis99 wants to merge 5 commits intomasterfrom
jameswillis99 wants to merge 5 commits intomasterfrom
Conversation
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.
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.
What does this MR do?
Extends the
prefillCloudfrontworker to warm CloudFront's versioned cache and trigger per-project scopedCreateInvalidationon published loader paths after each project publish. This allows theloader_publishedCloudFront 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/publishedURL path at the edge, making cache entries targetable by project.Changes
platform/wab/package.json— add@aws-sdk/client-cloudfrontdependencyAppServer.ts— add/api/v1/loader/code/published/:projectIdsroute alongside the existing one; handles CloudFront-rewritten URLs (e.g./published/aaa,zzz?projectId=aaa&...) with the same handler — no behaviour change,req.query.projectIdis read as normalprefill-cloudfront.ts— after bundle generation andisPrefilled: true:Promise.allSettled, 30s timeout per request)CloudFrontClient.CreateInvalidationwith per-combination paths forcode/published(e.g./published/p1,p2,p3*) and per-project paths forrepr-v2/v3/html(which already have:projectIdin 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 throwsDesign decisions
Ordering is critical:
isPrefilledis 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 containingaaawithout touching other projects.repr/html already scoped:
repr-v2/published/:projectId,repr-v3/published/:projectId, andhtml/published/:projectId/:componentalready have the project ID in the path — no CloudFront Function needed for those.Non-fatal failures: Warming fetches use
Promise.allSettledwith 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_IDis unset — local dev and non-CDN environments are unaffected.Testing
Unit tests cover: correct warming fetch URLs per publishment,
isPrefilledset before first fetch, invalidation fired after all warm fetches, per-combinationcode/publishedpaths (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.