Skip to content

Configure Fastly during deploy via generated route snippets#374

Open
cwillisf wants to merge 5 commits into
developfrom
refactor/fastly-config-generated-snippets
Open

Configure Fastly during deploy via generated route snippets#374
cwillisf wants to merge 5 commits into
developfrom
refactor/fastly-config-generated-snippets

Conversation

@cwillisf

@cwillisf cwillisf commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Proposed Changes

  • Render src/routes.json into a small, fixed set of VCL snippets (an init snippet with redirects/sections lookup tables, a recv snippet, and an error snippet) via a new bin/lib/routes-to-vcl.js, instead of creating one Fastly condition + response object + header per route. The snippets are overwritten by name on every run.
  • Redirects and section rewrites become table.lookups; a single reused internal status code (700) drives every 301, rather than burning one synthetic status per redirect.
  • Compile-check the generated VCL (validateServiceVersion) before anything activates.
  • Stop managing the origin Host header in code. The backend Override host in the service config owns it, and cloning a version preserves it. With CloudFront fronting the origin, the previous Host = S3 bucket name was also the wrong value (CloudFront routes on its distribution domain / alternate domain names, not a bucket name).
  • Add a configure-fastly job to the deploy workflow, scoped to the resolved GitHub Environment so staging and production each use their own Fastly service. Activation is gated per-environment by the FASTLY_ACTIVATE_CHANGES variable (set it to activate + purge, leave it unset to configure + validate only).

Reason for Changes

The previous configure-fastly was additive: each run created fresh conditions, response objects, and headers per route, and consumed a synthetic status code per redirect. Re-running it — as a deploy step would — accumulated objects and eventually exhausted status codes, which is why it was never wired into the deploy. Rendering routes to a handful of overwrite-by-name snippets makes the operation idempotent, so it is safe to run on every deploy. This PR both makes that change and wires the step in.

Test Coverage

  • New unit test test/routes-to-vcl.test.js (wired into npm test / npm run test:unit) asserts the generated snippet set: the lookup-table contents, the recv lookups plus root handling and the redirect sentinel, the error snippet's 301 conversion, and that a non-literal redirect pattern is rejected rather than silently mishandled.
  • Runtime validation: configure-fastly compile-checks the cloned version before activating.
  • Not yet exercised against a live Fastly service. Merging to develop runs the job against the staging service; the legacy per-route objects (and any lingering Bucket name header) should be cleared from the staging service first, so the run tests the snippet path alone.

cwillisf added 3 commits July 16, 2026 12:30
configure-fastly.js created a Fastly condition, response object, and header
per route. That accumulates objects on every change and spends a synthetic
status code per redirect (the live service was already carrying 900-906).

Render routes.json into three snippets instead: two lookup tables plus the
recv/error logic, overwritten by name on every run, with one reused internal
status (700) driving every 301. Redirects and section rewrites become table
lookups, so the route config stops accumulating.

- routes-to-vcl.js: pure routes.json to snippet-spec generator, with a test
  wired into npm test / test:unit
- fastly-extended.js: add setSnippet (delete-then-create, since fastly-js
  updateSnippet sends no body) and validateVersion; drop the now-unused
  condition and response-object helpers
- configure-fastly.js: build and write the snippets, validate the version
  before activating, drop the per-route machinery and unused lodash.defaults
The origin Host header is owned by the backend's Override host setting in the Fastly service config, which cloning a version preserves. Setting it again from configure-fastly duplicated that responsibility and, with a CloudFront-fronted origin, set the wrong value (an S3 bucket name rather than a host CloudFront routes on). Drop setBucketNameHeader and the now-unused header helper; the script manages only the route snippets.
Run the Fastly configuration script as its own job in the deploy workflow, scoped to the resolved GitHub Environment so staging and production each use their own Fastly service and activation setting. Now that route config renders to a few overwritten snippets instead of accumulating per-route objects, running it on every deploy no longer piles up Fastly objects.

Activation is gated per environment by the FASTLY_ACTIVATE_CHANGES variable: set it in an environment to activate and purge after configuring, leave it unset to configure and validate only.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR makes Fastly route configuration deploy-safe and idempotent by generating a small fixed set of VCL snippets from src/routes.json (instead of creating per-route Fastly objects), then wiring the configuration step into the deploy workflow with optional per-environment activation.

Changes:

  • Add bin/lib/routes-to-vcl.js to render routes.json into init/recv/error VCL snippets, plus a unit test validating the generated VCL.
  • Update Fastly tooling to manage VCL snippets (delete+create) and validate generated VCL before activation.
  • Add a configure-fastly GitHub Actions job and adjust npm scripts/deps (new test:unit, drop lodash.defaults, remove S3_BUCKET_NAME docs).

Reviewed changes

Copilot reviewed 4 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
bin/lib/routes-to-vcl.js New routes.json → VCL snippet generator (tables + recv/error logic).
bin/lib/fastly-extended.js Switch helper surface to snippet management + version validation.
bin/configure-fastly.js Configure snippets on a working version and validate VCL before optional activation/purge.
test/routes-to-vcl.test.js New unit test for snippet generation and redirect-pattern validation.
.github/workflows/deploy.yml Add configure-fastly job scoped to the resolved Environment.
package.json / package-lock.json Add unit-test script; remove unused dependency.
README.md Remove S3_BUCKET_NAME from env var docs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread .github/workflows/deploy.yml
Comment thread .github/workflows/deploy.yml Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 9 changed files in this pull request and generated 1 comment.

Comment thread src/routes.json
cwillisf added 2 commits July 16, 2026 15:22
The learn section was renamed to explore, but the live config still
routed /learn to a now-missing learn page. Redirect /learn and its
sub-paths to the matching /explore path (preserving the tail), plus an
exact /learn.html redirect, so existing links keep working.

Add a "prefix" redirect kind to the generator, rendered as a regex
rewrite in recv (a table lookup can only match an exact path).
The configure-fastly job ran in parallel with the S3 upload, so with
activation on it could flip Fastly to new routing before the new content
was uploaded -- and this PR adds new routes. Depend on the deploy job so
content lands first, and so a failed upload skips the Fastly step.

FASTLY_ACTIVATE_CHANGES was a presence check, so the string "false" (or
any non-empty value) still activated. Require exactly "true".
@cwillisf
cwillisf force-pushed the refactor/fastly-config-generated-snippets branch from 97d811c to cbfa1f6 Compare July 16, 2026 22:24
@cwillisf
cwillisf requested a review from Copilot July 16, 2026 22:26

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 9 changed files in this pull request and generated no new comments.

@cwillisf
cwillisf requested a review from colbygk July 16, 2026 22:34
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