edge functions example for redirect maps on edge delivery#2
Open
krystiannowak wants to merge 1 commit into
Open
edge functions example for redirect maps on edge delivery#2krystiannowak wants to merge 1 commit into
krystiannowak wants to merge 1 commit into
Conversation
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.
Add edge-delivery-redirect-maps example — URL redirect maps at the edge for Edge Delivery
Description
Adds a new
edge-delivery-redirect-mapsexample: an edge-native way to serve URL redirect maps for an AEM Edge Delivery site. On each request the function checks the path against one or more published redirect sheets and answers a matched redirect (301/302) straight from the CDN POP; anything without a match passes through to the Edge Delivery origin. Edge Delivery uses a constrained, canonical URL space, so redirecting legacy/alternate URLs to canonical ones is a common need — and redirect sets get large (migrations, multiple brands/campaigns). Edge Delivery already authors redirects as a spreadsheet published as JSON, and this example consumes that same JSON directly at the edge.It is the Edge Delivery counterpart to
publish-delivery-redirect-maps, and builds directly on the existingedge-delivery-transformerexample to keep the examples consistent and reduce variance — reusing its file layout, its sharedsrc/libhelpers, and the hardcoded origin pattern.It is deliberately much simpler than the AEMaaCS publish version, because Edge Delivery lets us drop three pieces:
x-edgefunction-requestsentinel.The example focuses on the aspects specific to running redirect maps on Edge Functions:
{ "data": [ { "Source", "Destination" }, … ] }, or a bare array), so a real project can point it straight at an existing published redirect sheet.REDIRECT_MAPSis a list of{ sourcePath, redirectCode? }checked in order (first match wins), so independently-authored maps (e.g. a permanent migration map and a temporary campaigns map) compose with explicit precedence and can each use their own301/302.Mapread, a cold instance is a cheap POP-cached refill + parse, and only a fully cold path reaches origin.publish-delivery-redirect-mapsas the heavier-duty alternative for very large sets.To stay small and aligned with the other examples, it hardcodes the origin and the map definitions. Relative
Destinations are emitted as relativeLocations (resolved by the browser against the requested host), so — unlike the AEMaaCS version — no customer domain is hardcoded; absoluteDestinations pass through for cross-host redirects. This is a reference example, not a turnkey production project.Changes
examples/edge-delivery-redirect-maps/— new example:src/index.js— entry point; calls the redirect handler and logs the requestsrc/redirect.js— checks each map in order →301/302on a hit; transparent pass-through to the Edge Delivery origin on a miss (no loopback)src/lib/redirects.js— two-layer cache: readthrough edge cache of the JSON + per-instance parsed-map memo, with a shared TTLsrc/lib/parse.js— parses an Edge Delivery / Helix published sheet into a lookupMapsrc/lib/location.js— resolves aDestinationto aLocation(relative stays relative; absolute passes through)src/config.js— the origin, the orderedREDIRECT_MAPS({ sourcePath, redirectCode? }), the default status, and the cache TTL (the single edit point)src/lib/log.js,src/lib/response.js— shared helpers (fromedge-delivery-transformer)config/edgeFunctions.yaml— declares the serviceconfig/cdn.yaml— origin-selector rule routing page-level GET/HEAD traffic to the function (no loopback condition)test/— unit tests (parse, location, response) and a sample published sheetREADME.md,fastly.toml,package.json,package-lock.json,Makefile,renovate.json,LICENSE,CODE_OF_CONDUCT.md,.github/CONTRIBUTING.md,.gitignore,.npmrcREADME.md(repository root) — added the example to the Examples listNotes
edge-delivery-transformer(layout + shared helpers + hardcoded origin) so the two Edge Delivery examples read consistently.Destinations produce relativeLocations, so no customer domain is hardcoded; absoluteDestinations cover cross-host redirects.@fastly/js-compute(the AEMaaCS version's@noble/hashesis not needed here, as there is no KV change-detection).npm testcovers the pure-logic units (sheet parsing,Locationresolution, responses); the cache/fetch and handler modules load only at build/runtime.publish-delivery-redirect-maps(KV-sharded, off-path maintenance) is the heavier-duty option.