Skip to content

Commit 4ebd70b

Browse files
authored
Merge pull request #26 from stack127labs/perf/cloudflare-hover-prefetch
Host public pages on free Cloudflare static assets
2 parents 52e346c + 5ee6f8d commit 4ebd70b

38 files changed

Lines changed: 3998 additions & 83 deletions

‎.github/workflows/stack127-cache-maintenance.yml‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name: Stack127 Cache Maintenance
33
on:
44
schedule:
55
- cron: "*/5 * * * *"
6+
- cron: "17 * * * *"
67
workflow_dispatch:
78
inputs:
89
configure_cache_policy:
@@ -12,16 +13,24 @@ on:
1213

1314
permissions:
1415
contents: read
16+
actions: write
1517

1618
concurrency:
1719
group: stack127-cache-maintenance
1820
cancel-in-progress: false
1921

2022
jobs:
2123
process:
24+
# Repository variables are available before the production environment loads.
25+
if: >-
26+
github.event_name == 'workflow_dispatch' ||
27+
(vars.STACK127_FRONTEND_HOSTING == 'cloudflare' && github.event.schedule == '17 * * * *') ||
28+
(vars.STACK127_FRONTEND_HOSTING != 'cloudflare' && github.event.schedule == '*/5 * * * *')
2229
runs-on: ubuntu-latest
2330
environment: production
2431
timeout-minutes: 10
32+
env:
33+
STACK127_FRONTEND_HOSTING: ${{ vars.STACK127_FRONTEND_HOSTING || 'legacy' }}
2534
steps:
2635
- uses: actions/checkout@v4
2736
- uses: actions/setup-node@v4
@@ -30,26 +39,61 @@ jobs:
3039
cache: npm
3140
- run: npm ci
3241
- name: Check worker reliability
42+
if: env.STACK127_FRONTEND_HOSTING != 'cloudflare'
3343
id: reliability
3444
run: node scripts/stack127/cache/status.mjs
3545
env:
3646
DATABASE_URL: ${{ secrets.DATABASE_POOLER_URL || secrets.DATABASE_URL }}
3747
- name: Scheduled fallback during the first reliable day, or manual backup
38-
if: github.event_name == 'workflow_dispatch' || steps.reliability.outputs.backup_needed == 'true'
48+
if: env.STACK127_FRONTEND_HOSTING != 'cloudflare' && (github.event_name == 'workflow_dispatch' || steps.reliability.outputs.backup_needed == 'true')
3949
run: npm run stack127:cache:process
4050
env:
4151
DATABASE_URL: ${{ secrets.DATABASE_POOLER_URL || secrets.DATABASE_URL }}
4252
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
4353
CLOUDFLARE_ZONE_ID: ${{ vars.CLOUDFLARE_ZONE_ID || secrets.CLOUDFLARE_ZONE_ID }}
4454
STACK127_SITE_URL: https://stack127.com
55+
- name: Dispatch a static refresh only when events are pending
56+
if: env.STACK127_FRONTEND_HOSTING == 'cloudflare'
57+
env:
58+
GITHUB_TOKEN: ${{ github.token }}
59+
DATABASE_URL: ${{ secrets.DATABASE_POOLER_URL || secrets.DATABASE_URL }}
60+
run: |
61+
node --input-type=module <<'NODE'
62+
import { randomUUID } from 'node:crypto';
63+
import pg from 'pg';
64+
import { dispatchFrontendRefresh } from './scripts/stack127/frontend/content-queue.mjs';
65+
const client = new pg.Client({ connectionString: process.env.DATABASE_URL, connectionTimeoutMillis: 15000, statement_timeout: 15000 });
66+
const lease = randomUUID();
67+
let leased = false;
68+
let dispatched = false;
69+
try {
70+
await client.connect();
71+
const pending = await client.query('select exists(select 1 from public.stack127_revalidation_events where next_attempt_at <= now()) as pending');
72+
if (!pending.rows[0]?.pending) {
73+
console.log('No due static frontend events.');
74+
} else {
75+
leased = (await client.query(`update public.stack127_cache_worker_state set lease_id=$1, lease_until=now()+interval '150 seconds'
76+
where id and (lease_until is null or lease_until<now()) returning id`, [lease])).rowCount > 0;
77+
if (leased) {
78+
const result = await dispatchFrontendRefresh({ repo: process.env.GITHUB_REPOSITORY, token: process.env.GITHUB_TOKEN });
79+
dispatched = result.dispatched;
80+
console.log(JSON.stringify(result));
81+
} else console.log('The cache worker already holds the dispatch lease.');
82+
}
83+
} finally {
84+
try {
85+
if (leased && !dispatched) await client.query('update public.stack127_cache_worker_state set lease_id=null, lease_until=null where id and lease_id=$1', [lease]);
86+
} finally { await client.end(); }
87+
}
88+
NODE
4589
- name: Apply the initial cache policy when requested
46-
if: inputs.configure_cache_policy == true && steps.reliability.outputs.long_ttl_enabled == 'false'
90+
if: env.STACK127_FRONTEND_HOSTING != 'cloudflare' && inputs.configure_cache_policy == true && steps.reliability.outputs.long_ttl_enabled == 'false'
4791
run: node scripts/stack127/cache/configure-cloudflare.mjs --apply
4892
env:
4993
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
5094
CLOUDFLARE_ZONE_ID: ${{ vars.CLOUDFLARE_ZONE_ID || secrets.CLOUDFLARE_ZONE_ID }}
5195
- name: Enable longer cache times after seven reliable days
52-
if: steps.reliability.outputs.long_ttl_ready == 'true'
96+
if: env.STACK127_FRONTEND_HOSTING != 'cloudflare' && steps.reliability.outputs.long_ttl_ready == 'true'
5397
run: node scripts/stack127/cache/configure-cloudflare.mjs --long-ttl --apply
5498
env:
5599
DATABASE_URL: ${{ secrets.DATABASE_POOLER_URL || secrets.DATABASE_URL }}

‎.github/workflows/stack127-ci.yml‎

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,30 @@ jobs:
3131
- run: npm ci
3232
- run: npm run test:core
3333
- name: Check page publishing and recovery
34-
run: npm run test:focused -- tests/stack127/page-publishing.test.mjs tests/stack127/page-publishing-database.test.mjs
34+
run: npm run test:focused -- tests/stack127/page-publishing.test.mjs tests/stack127/page-publishing-database.test.mjs tests/stack127/frontend-content-queue.test.mjs tests/stack127/frontend-static.test.mjs tests/stack127/cache-process-hosting.test.mjs
3535

3636
verify-production-build:
3737
needs: verify
3838
if: github.event_name == 'workflow_dispatch' || github.event.pull_request.head.repo.full_name == github.repository
3939
runs-on: ubuntu-latest
4040
environment: production
41-
timeout-minutes: 10
41+
timeout-minutes: 35
42+
# CI and production uploads use the same isolated preview service.
43+
concurrency:
44+
group: stack127-production
45+
cancel-in-progress: false
46+
queue: max
47+
env:
48+
STACK127_RELEASE_SHA: ${{ github.sha }}
49+
STACK127_BUILD_SHA: ${{ github.sha }}
50+
STACK127_FRONTEND_HOSTING: ${{ vars.STACK127_FRONTEND_HOSTING || 'legacy' }}
51+
DATABASE_URL: ${{ secrets.DATABASE_POOLER_URL || secrets.DATABASE_URL }}
52+
SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
53+
SUPABASE_PUBLISHABLE_KEY: ${{ secrets.SUPABASE_PUBLISHABLE_KEY }}
54+
SUPABASE_SECRET_KEY: ${{ secrets.SUPABASE_SECRET_KEY }}
55+
GOOGLE_ANALYTICS_ID: ${{ vars.GOOGLE_ANALYTICS_ID || 'G-ZJNG8SCGE1' }}
56+
UMAMI_URL: ${{ vars.UMAMI_URL || 'https://umami.ravitejaknts.com' }}
57+
UMAMI_WEBSITE_ID: ${{ vars.UMAMI_WEBSITE_ID || 'ff47ea40-04e6-4a74-b4cc-e420d28433d3' }}
4258

4359
steps:
4460
- uses: actions/checkout@v4
@@ -48,11 +64,6 @@ jobs:
4864
cache: npm
4965
- run: npm ci
5066
- run: npm run build
51-
env:
52-
DATABASE_URL: ${{ secrets.DATABASE_POOLER_URL || secrets.DATABASE_URL }}
53-
SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
54-
SUPABASE_PUBLISHABLE_KEY: ${{ secrets.SUPABASE_PUBLISHABLE_KEY }}
55-
SUPABASE_SECRET_KEY: ${{ secrets.SUPABASE_SECRET_KEY }}
5667
- name: Save checked build manifests
5768
uses: actions/upload-artifact@v4
5869
with:
@@ -63,3 +74,36 @@ jobs:
6374
include-hidden-files: true
6475
if-no-files-found: error
6576
retention-days: 1
77+
- name: Check the static frontend export before merge
78+
run: node scripts/stack127/frontend/build.mjs --output "$RUNNER_TEMP/frontend-assets"
79+
- name: Save the checked frontend for review
80+
uses: actions/upload-artifact@v4
81+
with:
82+
name: stack127-frontend-assets-${{ github.sha }}
83+
path: ${{ runner.temp }}/frontend-assets
84+
if-no-files-found: error
85+
retention-days: 1
86+
- name: Check whether an isolated preview can be deployed
87+
id: preview
88+
env:
89+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
90+
CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID || secrets.CLOUDFLARE_ACCOUNT_ID }}
91+
CLOUDFLARE_ZONE_ID: ${{ vars.CLOUDFLARE_ZONE_ID || secrets.CLOUDFLARE_ZONE_ID }}
92+
shell: bash
93+
run: |
94+
if [ -n "$CLOUDFLARE_API_TOKEN" ] && [ -n "$CLOUDFLARE_ACCOUNT_ID" ] && [ -n "$CLOUDFLARE_ZONE_ID" ]; then
95+
echo 'available=true' >> "$GITHUB_OUTPUT"
96+
fi
97+
- name: Verify the isolated frontend preview
98+
if: steps.preview.outputs.available == 'true'
99+
env:
100+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
101+
CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID || secrets.CLOUDFLARE_ACCOUNT_ID }}
102+
CLOUDFLARE_ZONE_ID: ${{ vars.CLOUDFLARE_ZONE_ID || secrets.CLOUDFLARE_ZONE_ID }}
103+
run: |
104+
node --input-type=module <<'NODE'
105+
import { appendFile } from 'node:fs/promises';
106+
import { deployFrontend } from './scripts/stack127/frontend/deploy.mjs';
107+
const preview = await deployFrontend({ directory: `${process.env.RUNNER_TEMP}/frontend-assets`, preview: true });
108+
await appendFile(process.env.GITHUB_STEP_SUMMARY, `Frontend preview: ${preview.site}\n\nCommit: ${preview.sha}\n`);
109+
NODE
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
name: Stack127 frontend content refresh
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
force:
7+
description: "Rebuild current main even with an empty queue, and retry delayed events."
8+
type: boolean
9+
default: false
10+
dry_run:
11+
description: "Check current main, backend health, and the queue without changing them."
12+
type: boolean
13+
default: false
14+
15+
permissions:
16+
contents: read
17+
actions: read
18+
19+
concurrency:
20+
group: stack127-production
21+
cancel-in-progress: false
22+
queue: max
23+
24+
jobs:
25+
refresh:
26+
if: github.ref == 'refs/heads/main' && vars.STACK127_FRONTEND_HOSTING == 'cloudflare'
27+
runs-on: ubuntu-latest
28+
environment: production
29+
timeout-minutes: 35
30+
env:
31+
STACK127_FRONTEND_HOSTING: cloudflare
32+
STACK127_SITE_URL: https://stack127.com
33+
GITHUB_TOKEN: ${{ github.token }}
34+
DATABASE_URL: ${{ secrets.DATABASE_POOLER_URL || secrets.DATABASE_URL }}
35+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
36+
CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID || secrets.CLOUDFLARE_ACCOUNT_ID }}
37+
CLOUDFLARE_ZONE_ID: ${{ vars.CLOUDFLARE_ZONE_ID || secrets.CLOUDFLARE_ZONE_ID }}
38+
steps:
39+
- name: Checkout current main after the production lock
40+
uses: actions/checkout@v4
41+
with:
42+
ref: main
43+
fetch-depth: 1
44+
- uses: actions/setup-node@v4
45+
with:
46+
node-version: 24
47+
cache: npm
48+
- run: npm ci
49+
- name: Record checkout SHA
50+
shell: bash
51+
run: echo "STACK127_BUILD_SHA=$(git rev-parse HEAD)" >> "$GITHUB_ENV"
52+
- name: Preview the queue
53+
if: inputs.dry_run == true
54+
run: |
55+
flags=()
56+
if [ "$FORCE_REFRESH" = "true" ]; then flags+=(--force); fi
57+
node scripts/stack127/frontend/content-queue.mjs snapshot --sha "$STACK127_BUILD_SHA" --dry-run "${flags[@]}"
58+
env:
59+
FORCE_REFRESH: ${{ inputs.force }}
60+
- name: Capture event versions and source fingerprints
61+
if: inputs.dry_run != true
62+
id: snapshot
63+
shell: bash
64+
run: |
65+
flags=()
66+
if [ "$FORCE_REFRESH" = "true" ]; then flags+=(--force); fi
67+
node scripts/stack127/frontend/content-queue.mjs snapshot --sha "$STACK127_BUILD_SHA" --output "$RUNNER_TEMP/frontend-queue.json" "${flags[@]}"
68+
env:
69+
FORCE_REFRESH: ${{ inputs.force }}
70+
- name: Build the Next.js static export
71+
if: steps.snapshot.outputs.build_required == 'true'
72+
run: node scripts/stack127/frontend/build.mjs --output "$RUNNER_TEMP/frontend-assets"
73+
env:
74+
STACK127_RELEASE_SHA: ${{ env.STACK127_BUILD_SHA }}
75+
SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
76+
SUPABASE_PUBLISHABLE_KEY: ${{ secrets.SUPABASE_PUBLISHABLE_KEY }}
77+
SUPABASE_SECRET_KEY: ${{ secrets.SUPABASE_SECRET_KEY }}
78+
GOOGLE_ANALYTICS_ID: ${{ vars.GOOGLE_ANALYTICS_ID || 'G-ZJNG8SCGE1' }}
79+
UMAMI_URL: ${{ vars.UMAMI_URL || 'https://umami.ravitejaknts.com' }}
80+
UMAMI_WEBSITE_ID: ${{ vars.UMAMI_WEBSITE_ID || 'ff47ea40-04e6-4a74-b4cc-e420d28433d3' }}
81+
- name: Check main and backend again before deployment
82+
if: steps.snapshot.outputs.build_required == 'true'
83+
run: node scripts/stack127/frontend/content-queue.mjs check --sha "$STACK127_BUILD_SHA"
84+
- name: Preview the static frontend
85+
if: steps.snapshot.outputs.build_required == 'true'
86+
run: node scripts/stack127/frontend/deploy.mjs --directory "$RUNNER_TEMP/frontend-assets" --preview
87+
- name: Publish the static frontend to the existing route
88+
if: steps.snapshot.outputs.build_required == 'true'
89+
run: |
90+
node scripts/stack127/frontend/content-queue.mjs check --sha "$STACK127_BUILD_SHA"
91+
node scripts/stack127/frontend/deploy.mjs --directory "$RUNNER_TEMP/frontend-assets"
92+
- name: Verify public content, refresh APIs, and acknowledge captured versions
93+
if: steps.snapshot.outputs.build_required == 'true'
94+
run: node scripts/stack127/frontend/content-queue.mjs complete --snapshot "$RUNNER_TEMP/frontend-queue.json" --directory "$RUNNER_TEMP/frontend-assets"
95+
- name: Preserve failed event versions for retry
96+
if: failure() && inputs.dry_run != true && steps.snapshot.outcome == 'success'
97+
run: node scripts/stack127/frontend/content-queue.mjs fail --snapshot "$RUNNER_TEMP/frontend-queue.json" --error "Frontend refresh failed in GitHub run $GITHUB_RUN_ID."
98+
- name: Remove the private queue snapshot
99+
if: always()
100+
run: rm -f "$RUNNER_TEMP/frontend-queue.json"

‎.github/workflows/stack127-publish-pages.yml‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,20 @@ jobs:
4444
if: github.event.deployment.environment == 'stack127-page-publication' && github.event.deployment.task == 'page-publication'
4545
runs-on: ubuntu-latest
4646
environment: production
47-
timeout-minutes: 20
47+
timeout-minutes: 45
48+
env:
49+
STACK127_FRONTEND_HOSTING: ${{ vars.STACK127_FRONTEND_HOSTING || 'legacy' }}
50+
STACK127_SITE_URL: https://stack127.com
51+
DATABASE_URL: ${{ secrets.DATABASE_POOLER_URL || secrets.DATABASE_URL }}
52+
SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
53+
SUPABASE_PUBLISHABLE_KEY: ${{ secrets.SUPABASE_PUBLISHABLE_KEY }}
54+
SUPABASE_SECRET_KEY: ${{ secrets.SUPABASE_SECRET_KEY }}
55+
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
56+
CLOUDFLARE_ACCOUNT_ID: ${{ vars.CLOUDFLARE_ACCOUNT_ID || secrets.CLOUDFLARE_ACCOUNT_ID }}
57+
CLOUDFLARE_ZONE_ID: ${{ vars.CLOUDFLARE_ZONE_ID || secrets.CLOUDFLARE_ZONE_ID }}
58+
GOOGLE_ANALYTICS_ID: ${{ vars.GOOGLE_ANALYTICS_ID || 'G-ZJNG8SCGE1' }}
59+
UMAMI_URL: ${{ vars.UMAMI_URL || 'https://umami.ravitejaknts.com' }}
60+
UMAMI_WEBSITE_ID: ${{ vars.UMAMI_WEBSITE_ID || 'ff47ea40-04e6-4a74-b4cc-e420d28433d3' }}
4861
concurrency:
4962
group: stack127-production
5063
cancel-in-progress: false
@@ -69,18 +82,51 @@ jobs:
6982
SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
7083
run: |
7184
umask 077
85+
test -n "$DATABASE_URL"
7286
printf 'DATABASE_URL=%s\n' "$DATABASE_URL" > .env.database
7387
printf 'SUPABASE_URL=%s\n' "$SUPABASE_URL" >> .env.database
7488
- name: Publish and verify selected pages
7589
env:
7690
GH_TOKEN: ${{ github.token }}
91+
GITHUB_TOKEN: ${{ github.token }}
7792
run: |
7893
node --input-type=module <<'NODE'
7994
import { readFile } from 'node:fs/promises';
8095
import { publishingMain } from './scripts/stack127/publishing/cli.mjs';
8196
import { publicationEvent } from './scripts/stack127/publishing/github.mjs';
97+
import { refreshCommittedContent, checkCurrentRelease } from './scripts/stack127/frontend/content-queue.mjs';
98+
import { buildFrontend } from './scripts/stack127/frontend/build.mjs';
99+
import { deployFrontend, verifyFrontend } from './scripts/stack127/frontend/deploy.mjs';
82100
const publication = publicationEvent(JSON.parse(await readFile(process.env.GITHUB_EVENT_PATH, 'utf8')));
83-
await publishingMain(publication.plan.kind, ['apply', '--plan', `${process.env.RUNNER_TEMP}/page-plan.json`, '--environment', 'production', '--confirm', 'stack127-production', '--release-sha', publication.releaseSha]);
101+
process.env.STACK127_RELEASE_SHA = publication.releaseSha;
102+
const options = {
103+
sha: publication.releaseSha, directory: `${process.env.RUNNER_TEMP}/frontend-assets`,
104+
hosting: process.env.STACK127_FRONTEND_HOSTING, site: process.env.STACK127_SITE_URL,
105+
token: process.env.GITHUB_TOKEN, repo: process.env.GITHUB_REPOSITORY,
106+
zone: process.env.CLOUDFLARE_ZONE_ID, cloudflareToken: process.env.CLOUDFLARE_API_TOKEN,
107+
};
108+
if (options.hosting === 'cloudflare') {
109+
await checkCurrentRelease(options);
110+
await verifyFrontend(options.site, options.sha);
111+
}
112+
await publishingMain(publication.plan.kind, ['apply', '--plan', `${process.env.RUNNER_TEMP}/page-plan.json`, '--environment', 'production', '--confirm', 'stack127-production', '--release-sha', publication.releaseSha], {
113+
afterCommit: async ({ pool }) => {
114+
if (options.hosting !== 'cloudflare') return;
115+
const client = await pool.connect();
116+
try {
117+
await refreshCommittedContent(client, {
118+
...options,
119+
buildDeploy: async () => {
120+
await buildFrontend({ output: options.directory });
121+
await checkCurrentRelease(options);
122+
await deployFrontend({ directory: options.directory, preview: true });
123+
await checkCurrentRelease(options);
124+
await deployFrontend({ directory: options.directory });
125+
},
126+
});
127+
} finally { client.release(); }
128+
},
129+
});
84130
NODE
85131
- name: Record publication result
86132
if: always()

0 commit comments

Comments
 (0)