belt 0.6.9 → 0.6.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +49 -0
- data/lib/belt/cli/frontend_deploy_command.rb +25 -6
- data/lib/belt/cli/frontend_env_command.rb +35 -0
- data/lib/belt/cli/frontend_prerender.rb +94 -0
- data/lib/belt/cli/frontend_routes.rb +129 -0
- data/lib/belt/cli/frontend_setup_command.rb +8 -0
- data/lib/belt/cli.rb +2 -0
- data/lib/belt/docs/frontend.md +85 -12
- data/lib/belt/frontend/prerender.mjs +290 -0
- data/lib/belt/frontend/router.js +112 -0
- data/lib/belt/version.rb +1 -1
- data/lib/templates/frontend/react/package.json.erb +1 -1
- data/lib/templates/frontend/react/src/App.jsx +2 -0
- data/lib/templates/frontend/react/src/entry-server.jsx +2 -1
- data/lib/templates/frontend/react/src/pages/NotFound.jsx +18 -0
- data/lib/templates/frontend/react/src/seo.js.erb +7 -2
- data/lib/templates/module/frontend.tf.erb +43 -1
- data/lib/templates/new_app/AGENTS.md.erb +7 -2
- metadata +6 -2
- data/lib/templates/frontend/react/scripts/prerender.js +0 -172
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: dd41febdeffceff192d2ce0c71193a0d44f6bc9bf573361e2473aed29f75c845
|
|
4
|
+
data.tar.gz: 48c1cce20c99bdf8e1aac26af043a6b2fc16438007d6840e18fa8a8d17607b6e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7d44de9ce0d9f79fa3247cb32d7eab77859bc5da28167b1d3a9efd34e391c024f130d5d7486a5b053b9a0d4d425a6183657e2b1009ab13a48f9c1dc0284edeed
|
|
7
|
+
data.tar.gz: f757091a23142d205428a6161afe26033520229623950f8455b336ffd99135d29efc64fd40b1ef76f67035644780c21fe9ce844e56b3a6c69a56893da10bff81
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,54 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.6.11
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- **Frontends answer HTTP 404 for an address that doesn't exist.** A single-page
|
|
8
|
+
app on S3 + CloudFront answered every path with `index.html` and 200, so
|
|
9
|
+
crawlers and agents couldn't tell a missing page from a real one. For a
|
|
10
|
+
frontend with `src/seo.js`, the prerender step now writes `404.html` (the app's
|
|
11
|
+
own "not found" route, `noindex`) and `.belt-routes.json`: the public pages plus
|
|
12
|
+
the app's routes, read from its `<Route path="/...">` declarations and
|
|
13
|
+
`seo.privatePaths`, so there is no second route list to keep in sync.
|
|
14
|
+
`belt deploy frontend` publishes the routes to a CloudFront KeyValueStore, and
|
|
15
|
+
a router function on the distribution serves pages from their prerendered
|
|
16
|
+
files, app routes from `index.html`, and anything else from `404.html` with
|
|
17
|
+
status 404. It fails open: before routes are published, or if the store can't
|
|
18
|
+
be read, every path behaves as before. Generated `frontend.tf` includes the
|
|
19
|
+
store and function; existing apps opt in via UPGRADING.md. (Fizzy #1985)
|
|
20
|
+
- A frontend whose own build prerenders its pages gets real 404s too: belt
|
|
21
|
+
leaves the pages alone and adds the app routes (and `404.html` if the build
|
|
22
|
+
wrote none). The build can write `.belt-routes.json` with `pages` and
|
|
23
|
+
`objects` to say where its pages are stored.
|
|
24
|
+
- `llms-full.txt` is uploaded `no-cache`, like `llms.txt`.
|
|
25
|
+
- **`belt frontend router`** writes or refreshes
|
|
26
|
+
`infrastructure/modules/app/belt_frontend_router.js`.
|
|
27
|
+
- The React template has a `NotFound` page on `<Route path="*">`.
|
|
28
|
+
|
|
29
|
+
## 0.6.10
|
|
30
|
+
|
|
31
|
+
### Changed
|
|
32
|
+
|
|
33
|
+
- **SEO prerender is a belt deploy step, not app code.** `belt deploy frontend`
|
|
34
|
+
now prerenders any frontend with a `src/seo.js` manifest after `npm run build`:
|
|
35
|
+
it builds `src/entry-server.*` with `vite build --ssr` (when present) and writes
|
|
36
|
+
per-page HTML (title, description, canonical, Open Graph, JSON-LD),
|
|
37
|
+
`robots.txt`, `sitemap.xml` and `llms.txt` into the build output, so the
|
|
38
|
+
`--delete` sync reproduces them on every deploy. The script ships in the gem
|
|
39
|
+
(`lib/belt/frontend/prerender.mjs`), so existing apps opt in with just a
|
|
40
|
+
manifest instead of copying `scripts/prerender.js`. New React frontends build
|
|
41
|
+
with plain `vite build`. Frontends whose build already prerenders (0.6.6–0.6.9
|
|
42
|
+
templates) are detected and skipped; frontends without `src/seo.js` deploy as
|
|
43
|
+
before. (Fizzy #1930)
|
|
44
|
+
|
|
45
|
+
### Added
|
|
46
|
+
|
|
47
|
+
- **`belt frontend prerender [--frontend NAME]`** builds a frontend and runs the
|
|
48
|
+
SEO prerender locally.
|
|
49
|
+
- **`public/robots.txt` overrides the generated robots.txt** in indexable
|
|
50
|
+
environments. Non-indexable environments still get `Disallow: /`.
|
|
51
|
+
|
|
3
52
|
## 0.6.9
|
|
4
53
|
|
|
5
54
|
### Fixed
|
|
@@ -7,7 +7,9 @@ require_relative 'app_detection'
|
|
|
7
7
|
require_relative 'env_resolver'
|
|
8
8
|
require_relative 'environment_config'
|
|
9
9
|
require_relative 'frontend_env_map'
|
|
10
|
+
require_relative 'frontend_prerender'
|
|
10
11
|
require_relative 'frontend_registry'
|
|
12
|
+
require_relative 'frontend_routes'
|
|
11
13
|
require_relative 'terraform_command'
|
|
12
14
|
|
|
13
15
|
module Belt
|
|
@@ -15,9 +17,9 @@ module Belt
|
|
|
15
17
|
class FrontendDeployCommand
|
|
16
18
|
include AppDetection
|
|
17
19
|
|
|
18
|
-
PAGES_MANIFEST =
|
|
20
|
+
PAGES_MANIFEST = FrontendPrerender::PAGES_MANIFEST
|
|
19
21
|
# Uploaded with no-cache instead of the immutable header hashed assets get
|
|
20
|
-
REVALIDATED_FILES = %w[*.html robots.txt sitemap.xml llms.txt].freeze
|
|
22
|
+
REVALIDATED_FILES = %w[*.html robots.txt sitemap.xml llms.txt llms-full.txt].freeze
|
|
21
23
|
|
|
22
24
|
def self.run(args)
|
|
23
25
|
frontend_name = FrontendRegistry.extract_flag!(args, '--frontend')
|
|
@@ -72,6 +74,7 @@ module Belt
|
|
|
72
74
|
puts "━━━ #{@frontend.label} (#{@frontend.path}/) ━━━"
|
|
73
75
|
build_frontend
|
|
74
76
|
sync_to_s3
|
|
77
|
+
publish_routes
|
|
75
78
|
invalidate_cloudfront
|
|
76
79
|
url = fetch_frontend_url
|
|
77
80
|
puts "\n✅ #{@frontend.label.capitalize} deployed to #{@env}!"
|
|
@@ -112,9 +115,10 @@ module Belt
|
|
|
112
115
|
env = frontend_build_env
|
|
113
116
|
puts " Injecting env: #{env.keys.sort.join(', ')}" if env.any?
|
|
114
117
|
run!(env, 'npm', 'run', 'build', chdir: @frontend.path)
|
|
118
|
+
FrontendPrerender.new(@frontend, env: env).run
|
|
115
119
|
end
|
|
116
120
|
|
|
117
|
-
# BELT_ENV and VITE_SITE_URL drive the SEO prerender step (
|
|
121
|
+
# BELT_ENV and VITE_SITE_URL drive the SEO prerender step (FrontendPrerender):
|
|
118
122
|
# only indexable environments get crawled, and canonical/sitemap URLs need the
|
|
119
123
|
# site's origin. The env map or the shell can override either.
|
|
120
124
|
def frontend_build_env
|
|
@@ -148,7 +152,7 @@ module Belt
|
|
|
148
152
|
# excluded so --delete doesn't remove them before they're re-uploaded.
|
|
149
153
|
run!('aws', 's3', 'sync', dist_prefix, "s3://#{bucket}", '--delete',
|
|
150
154
|
'--size-only', '--cache-control', 'public, max-age=31536000, immutable',
|
|
151
|
-
*[*REVALIDATED_FILES, PAGES_MANIFEST].flat_map { |f| ['--exclude', f] },
|
|
155
|
+
*[*REVALIDATED_FILES, PAGES_MANIFEST, FrontendRoutes::MANIFEST].flat_map { |f| ['--exclude', f] },
|
|
152
156
|
*pages.keys.flat_map { |key| ['--exclude', key] })
|
|
153
157
|
|
|
154
158
|
index = File.join(dist, 'index.html')
|
|
@@ -162,7 +166,7 @@ module Belt
|
|
|
162
166
|
upload_prerendered_pages(dist, bucket, pages)
|
|
163
167
|
end
|
|
164
168
|
|
|
165
|
-
#
|
|
169
|
+
# The prerender step writes dist/.belt-pages.json mapping each page's S3 key
|
|
166
170
|
# to its file ({ "about" => "about.html" }). CloudFront has no directory
|
|
167
171
|
# indexes, so /about must be an object named `about` served as text/html.
|
|
168
172
|
def prerendered_pages(dist)
|
|
@@ -187,8 +191,23 @@ module Belt
|
|
|
187
191
|
end
|
|
188
192
|
end
|
|
189
193
|
|
|
194
|
+
# Tells CloudFront which addresses exist, so anything else answers 404
|
|
195
|
+
# (FrontendRoutes). Before the invalidation, so the first uncached request
|
|
196
|
+
# already sees the new routes.
|
|
197
|
+
def publish_routes
|
|
198
|
+
return unless File.file?(File.join(@frontend.dist_dir, FrontendRoutes::MANIFEST))
|
|
199
|
+
|
|
200
|
+
FrontendRoutes.new(dist_dir: @frontend.dist_dir, distribution_id: distribution_id).publish
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def distribution_id
|
|
204
|
+
return @distribution_id if defined?(@distribution_id)
|
|
205
|
+
|
|
206
|
+
@distribution_id = fetch_distribution_id
|
|
207
|
+
end
|
|
208
|
+
|
|
190
209
|
def invalidate_cloudfront
|
|
191
|
-
dist_id =
|
|
210
|
+
dist_id = distribution_id
|
|
192
211
|
unless dist_id
|
|
193
212
|
puts '⚠️ No CloudFront distribution found (skipping cache invalidation)'
|
|
194
213
|
return
|
|
@@ -4,13 +4,17 @@ require_relative 'app_detection'
|
|
|
4
4
|
require_relative 'env_resolver'
|
|
5
5
|
require_relative 'environment_config'
|
|
6
6
|
require_relative 'frontend_env_map'
|
|
7
|
+
require_relative 'frontend_prerender'
|
|
7
8
|
require_relative 'frontend_registry'
|
|
9
|
+
require_relative 'frontend_routes'
|
|
8
10
|
require_relative 'terraform_command'
|
|
9
11
|
|
|
10
12
|
module Belt
|
|
11
13
|
module CLI
|
|
12
14
|
# belt frontend env <environment> [--frontend NAME]
|
|
13
15
|
# belt frontend list
|
|
16
|
+
# belt frontend prerender [--frontend NAME]
|
|
17
|
+
# belt frontend router
|
|
14
18
|
#
|
|
15
19
|
# Smart-merges terraform outputs into <frontend>/.env using the declarative map
|
|
16
20
|
# (<frontend>/env.yml or .belt/frontend_env.yml for the default frontend).
|
|
@@ -25,6 +29,10 @@ module Belt
|
|
|
25
29
|
run_env(args)
|
|
26
30
|
when 'list', 'ls'
|
|
27
31
|
run_list
|
|
32
|
+
when 'prerender'
|
|
33
|
+
run_prerender(args)
|
|
34
|
+
when 'router'
|
|
35
|
+
run_router
|
|
28
36
|
when nil, '-h', '--help', 'help'
|
|
29
37
|
puts usage
|
|
30
38
|
exit(subcommand.nil? ? 1 : 0)
|
|
@@ -50,6 +58,31 @@ module Belt
|
|
|
50
58
|
end
|
|
51
59
|
end
|
|
52
60
|
|
|
61
|
+
# Local check of the SEO prerender `belt deploy frontend` runs: builds the
|
|
62
|
+
# frontend and prerenders from src/seo.js. Not indexable unless BELT_ENV or
|
|
63
|
+
# SEO_INDEXABLE says so; VITE_SITE_URL from the shell sets canonical URLs.
|
|
64
|
+
def self.run_prerender(args)
|
|
65
|
+
name = FrontendRegistry.extract_flag!(args, '--frontend') || args.reject { |a| a.start_with?('-') }.first
|
|
66
|
+
frontend = FrontendRegistry.new.resolve!(name)
|
|
67
|
+
prerender = FrontendPrerender.new(frontend)
|
|
68
|
+
abort "No #{frontend.path}/src/seo.js — nothing to prerender. See `belt explain seo`." unless prerender.manifest
|
|
69
|
+
|
|
70
|
+
abort "\n✗ npm run build failed in #{frontend.path}/" unless system('npm', 'run', 'build', chdir: frontend.path)
|
|
71
|
+
prerender.run
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Writes (or refreshes) the CloudFront router function next to the frontend
|
|
75
|
+
# terraform that loads it. For apps adopting real 404s, and after a belt
|
|
76
|
+
# upgrade that changes the function.
|
|
77
|
+
def self.run_router
|
|
78
|
+
module_dir = FrontendRoutes::MODULE_DIR
|
|
79
|
+
abort "Error: Module directory not found at #{module_dir}/." unless Dir.exist?(module_dir)
|
|
80
|
+
|
|
81
|
+
dest = FrontendRoutes.install_router(module_dir)
|
|
82
|
+
puts dest ? " write #{dest}" : " current #{File.join(module_dir, FrontendRoutes::ROUTER_FILE)}"
|
|
83
|
+
puts "\nApply it with `belt deploy <environment>`. See `belt explain seo`, \"Real 404s\"."
|
|
84
|
+
end
|
|
85
|
+
|
|
53
86
|
def self.run_env(args)
|
|
54
87
|
frontend_name = FrontendRegistry.extract_flag!(args, '--frontend')
|
|
55
88
|
env = EnvResolver.resolve(args)
|
|
@@ -88,6 +121,8 @@ module Belt
|
|
|
88
121
|
Usage:
|
|
89
122
|
belt frontend env <environment> [--frontend NAME]
|
|
90
123
|
belt frontend list
|
|
124
|
+
belt frontend prerender [--frontend NAME] Build + SEO prerender locally (src/seo.js)
|
|
125
|
+
belt frontend router Write the CloudFront router function (real 404s)
|
|
91
126
|
belt frontend --help
|
|
92
127
|
|
|
93
128
|
Env map (optional, per frontend):
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'shellwords'
|
|
5
|
+
|
|
6
|
+
module Belt
|
|
7
|
+
module CLI
|
|
8
|
+
# Belt-owned SEO/AEO prerender step, run after a frontend's `npm run build`.
|
|
9
|
+
#
|
|
10
|
+
# Opt-in by manifest: a frontend with src/seo.js gets per-page HTML (title,
|
|
11
|
+
# description, canonical, Open Graph, JSON-LD), robots.txt, sitemap.xml and
|
|
12
|
+
# llms.txt written into its build output, so `aws s3 sync --delete`
|
|
13
|
+
# reproduces them on every deploy. When src/entry-server.* exists it is built
|
|
14
|
+
# with `vite build --ssr` and each page's markup is prerendered too. No
|
|
15
|
+
# manifest → nothing happens.
|
|
16
|
+
class FrontendPrerender
|
|
17
|
+
SCRIPT = File.expand_path('../frontend/prerender.mjs', __dir__)
|
|
18
|
+
MANIFESTS = %w[src/seo.js src/seo.mjs].freeze
|
|
19
|
+
SSR_ENTRIES = %w[src/entry-server.jsx src/entry-server.tsx src/entry-server.js src/entry-server.ts].freeze
|
|
20
|
+
# Inside node_modules so the SSR bundle resolves the app's dependencies and
|
|
21
|
+
# existing apps don't grow an untracked build directory.
|
|
22
|
+
SSR_OUT_DIR = 'node_modules/.cache/belt-ssr'
|
|
23
|
+
PAGES_MANIFEST = '.belt-pages.json'
|
|
24
|
+
ROUTES_MANIFEST = '.belt-routes.json'
|
|
25
|
+
|
|
26
|
+
def initialize(frontend, env: {})
|
|
27
|
+
@frontend = frontend
|
|
28
|
+
@env = env
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def manifest
|
|
32
|
+
@manifest ||= MANIFESTS.find { |file| File.file?(File.join(@frontend.path, file)) }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Frontends scaffolded by belt 0.6.6–0.6.9 run their own scripts/prerender.js
|
|
36
|
+
# inside `npm run build`, and an app can prerender its own way and say where
|
|
37
|
+
# its pages are in .belt-routes.json; either way the pages are already in dist.
|
|
38
|
+
def prerendered_by_build?
|
|
39
|
+
[PAGES_MANIFEST, ROUTES_MANIFEST].any? { |file| File.file?(File.join(@frontend.dist_dir, file)) }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Returns true when it prerendered, false when the frontend didn't opt in.
|
|
43
|
+
def run # rubocop:disable Naming/PredicateMethod
|
|
44
|
+
return false unless manifest
|
|
45
|
+
|
|
46
|
+
if prerendered_by_build?
|
|
47
|
+
puts '🔎 SEO: prerendered by the app build — skipping belt prerender, adding the app routes for 404s'
|
|
48
|
+
run_script('--routes-only')
|
|
49
|
+
return false
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
puts "🔎 SEO: prerendering pages from #{manifest}"
|
|
53
|
+
ssr = build_ssr_bundle
|
|
54
|
+
run_script(*(ssr ? ['--ssr', ssr] : []))
|
|
55
|
+
true
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
def run_script(*extra)
|
|
61
|
+
cmd = ['node', SCRIPT,
|
|
62
|
+
'--manifest', File.expand_path(manifest, @frontend.path),
|
|
63
|
+
'--dist', File.expand_path(@frontend.dist_dir),
|
|
64
|
+
'--public', File.expand_path('public', @frontend.path),
|
|
65
|
+
'--src', File.expand_path('src', @frontend.path), *extra]
|
|
66
|
+
return if system(@env, *cmd, chdir: @frontend.path)
|
|
67
|
+
|
|
68
|
+
abort "\n✗ SEO prerender failed: #{cmd.shelljoin}"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Builds the SSR bundle and returns its entry path, or nil when there's no
|
|
72
|
+
# entry or it doesn't build (typically a module touching window/document at
|
|
73
|
+
# import time). Without it, pages still get their tags, sitemap and robots.
|
|
74
|
+
def build_ssr_bundle
|
|
75
|
+
entry = SSR_ENTRIES.find { |file| File.file?(File.join(@frontend.path, file)) }
|
|
76
|
+
return nil unless entry
|
|
77
|
+
|
|
78
|
+
out_dir = File.join(@frontend.path, SSR_OUT_DIR)
|
|
79
|
+
ok = system(@env, 'npx', 'vite', 'build', '--ssr', entry, '--outDir', SSR_OUT_DIR, '--emptyOutDir',
|
|
80
|
+
'--logLevel', 'warn', chdir: @frontend.path)
|
|
81
|
+
unless ok
|
|
82
|
+
warn "⚠️ SSR build of #{entry} failed — pages get SEO tags but no prerendered content"
|
|
83
|
+
return nil
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Vite emits ESM; make sure Node loads it as such whatever the app's package type.
|
|
87
|
+
File.write(File.join(out_dir, 'package.json'), "#{JSON.generate(type: 'module')}\n")
|
|
88
|
+
name = File.basename(entry, '.*')
|
|
89
|
+
bundle = %w[.js .mjs].map { |ext| File.join(out_dir, "#{name}#{ext}") }.find { |f| File.file?(f) }
|
|
90
|
+
bundle && File.expand_path(bundle)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'fileutils'
|
|
4
|
+
require 'json'
|
|
5
|
+
require 'open3'
|
|
6
|
+
|
|
7
|
+
module Belt
|
|
8
|
+
module CLI
|
|
9
|
+
# Real 404s for a single-page frontend.
|
|
10
|
+
#
|
|
11
|
+
# CloudFront can't tell which addresses an SPA has, so the classic setup
|
|
12
|
+
# answers every path with index.html and 200. Belt's router function
|
|
13
|
+
# (lib/belt/frontend/router.js) fixes that at the edge, and this class keeps
|
|
14
|
+
# it informed: after each deploy it publishes the routes the prerender step
|
|
15
|
+
# wrote to <dist>/.belt-routes.json (from src/seo.js: `pages` and
|
|
16
|
+
# `privatePaths`) into the CloudFront KeyValueStore the function reads.
|
|
17
|
+
#
|
|
18
|
+
# Nothing here is required for a deploy to work. No manifest, no router on the
|
|
19
|
+
# distribution, or a failed publish all leave the site serving as it did.
|
|
20
|
+
class FrontendRoutes
|
|
21
|
+
ROUTER_SOURCE = File.expand_path('../frontend/router.js', __dir__)
|
|
22
|
+
ROUTER_FILE = 'belt_frontend_router.js'
|
|
23
|
+
MODULE_DIR = 'infrastructure/modules/app'
|
|
24
|
+
MANIFEST = '.belt-routes.json'
|
|
25
|
+
KEY = 'belt:routes'
|
|
26
|
+
# A KeyValueStore value holds 1 KB.
|
|
27
|
+
CHUNK_SIZE = 1000
|
|
28
|
+
|
|
29
|
+
# Copies the router function next to the terraform that loads it with
|
|
30
|
+
# file(). Returns the path when the file was written or refreshed.
|
|
31
|
+
def self.install_router(module_dir = MODULE_DIR)
|
|
32
|
+
dest = File.join(module_dir, ROUTER_FILE)
|
|
33
|
+
source = File.read(ROUTER_SOURCE)
|
|
34
|
+
return nil if File.file?(dest) && File.read(dest) == source
|
|
35
|
+
|
|
36
|
+
FileUtils.mkdir_p(module_dir)
|
|
37
|
+
File.write(dest, source)
|
|
38
|
+
dest
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# The KeyValueStore entries for a manifest: the chunk count under KEY and
|
|
42
|
+
# the JSON split across KEY:0, KEY:1, ...
|
|
43
|
+
def self.entries(manifest)
|
|
44
|
+
routes = { 'app' => Array(manifest['app']), 'pages' => Array(manifest['pages']) }
|
|
45
|
+
routes['objects'] = manifest['objects'] if manifest['objects'].is_a?(Hash) && manifest['objects'].any?
|
|
46
|
+
json = JSON.generate(routes, ascii_only: true)
|
|
47
|
+
chunks = json.scan(/.{1,#{CHUNK_SIZE}}/m)
|
|
48
|
+
chunks.each_with_index.to_h { |chunk, i| ["#{KEY}:#{i}", chunk] }.merge(KEY => chunks.size.to_s)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def initialize(dist_dir:, distribution_id:)
|
|
52
|
+
@dist_dir = dist_dir
|
|
53
|
+
@distribution_id = distribution_id
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def manifest
|
|
57
|
+
path = File.join(@dist_dir, MANIFEST)
|
|
58
|
+
return nil unless File.file?(path)
|
|
59
|
+
|
|
60
|
+
data = JSON.parse(File.read(path))
|
|
61
|
+
data.is_a?(Hash) ? data : nil
|
|
62
|
+
rescue JSON::ParserError
|
|
63
|
+
nil
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Returns :published, or the reason nothing was published.
|
|
67
|
+
def publish
|
|
68
|
+
routes = manifest
|
|
69
|
+
return :no_manifest unless routes
|
|
70
|
+
return :no_distribution unless @distribution_id
|
|
71
|
+
|
|
72
|
+
store = key_value_store_arn
|
|
73
|
+
unless store
|
|
74
|
+
puts 'ℹ️ Unknown paths still answer 200: this CloudFront distribution has no belt router. ' \
|
|
75
|
+
'See "Real 404s" in `belt explain seo`.'
|
|
76
|
+
return :no_router
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
write(store, self.class.entries(routes))
|
|
80
|
+
puts "🧭 Published routes: #{Array(routes['pages']).size} page(s), " \
|
|
81
|
+
"#{Array(routes['app']).size} app path(s); anything else answers 404"
|
|
82
|
+
:published
|
|
83
|
+
rescue StandardError => e
|
|
84
|
+
warn "⚠️ Could not publish routes (#{e.message.lines.first&.strip}). " \
|
|
85
|
+
'The site keeps serving with the routes from the last deploy.'
|
|
86
|
+
:failed
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
private
|
|
90
|
+
|
|
91
|
+
# The store is whichever KeyValueStore the distribution's default-behavior
|
|
92
|
+
# functions are associated with, so no extra terraform output is needed.
|
|
93
|
+
def key_value_store_arn
|
|
94
|
+
functions = aws_json('cloudfront', 'get-distribution-config', '--id', @distribution_id)
|
|
95
|
+
.dig('DistributionConfig', 'DefaultCacheBehavior', 'FunctionAssociations', 'Items') || []
|
|
96
|
+
functions.each do |association|
|
|
97
|
+
name = association['FunctionARN'].to_s.split('/').last
|
|
98
|
+
next if name.to_s.empty?
|
|
99
|
+
|
|
100
|
+
stores = aws_json('cloudfront', 'describe-function', '--name', name, '--stage', 'LIVE')
|
|
101
|
+
.dig('FunctionSummary', 'FunctionConfig', 'KeyValueStoreAssociations', 'Items') || []
|
|
102
|
+
arn = stores.first&.fetch('KeyValueStoreARN', nil)
|
|
103
|
+
return arn if arn
|
|
104
|
+
end
|
|
105
|
+
nil
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# One atomic update: new entries in, chunks left over from a longer list out.
|
|
109
|
+
def write(store, entries)
|
|
110
|
+
etag = aws_json('cloudfront-keyvaluestore', 'describe-key-value-store', '--kvs-arn', store).fetch('ETag')
|
|
111
|
+
existing = (aws_json('cloudfront-keyvaluestore', 'list-keys', '--kvs-arn', store)['Items'] || [])
|
|
112
|
+
.map { |item| item['Key'] }
|
|
113
|
+
stale = existing.select { |key| key.start_with?(KEY) } - entries.keys
|
|
114
|
+
|
|
115
|
+
cmd = ['cloudfront-keyvaluestore', 'update-keys', '--kvs-arn', store, '--if-match', etag,
|
|
116
|
+
'--puts', JSON.generate(entries.map { |key, value| { 'Key' => key, 'Value' => value } })]
|
|
117
|
+
cmd.push('--deletes', JSON.generate(stale.map { |key| { 'Key' => key } })) if stale.any?
|
|
118
|
+
aws_json(*cmd)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def aws_json(*args)
|
|
122
|
+
out, err, status = Open3.capture3('aws', *args, '--output', 'json')
|
|
123
|
+
raise err.strip.empty? ? "aws #{args.first(2).join(' ')} failed" : err.strip unless status.success?
|
|
124
|
+
|
|
125
|
+
out.strip.empty? ? {} : JSON.parse(out)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
|
@@ -5,6 +5,7 @@ require 'erb'
|
|
|
5
5
|
require_relative 'app_detection'
|
|
6
6
|
require_relative 'env_resolver'
|
|
7
7
|
require_relative 'frontend_registry'
|
|
8
|
+
require_relative 'frontend_routes'
|
|
8
9
|
|
|
9
10
|
module Belt
|
|
10
11
|
module CLI
|
|
@@ -70,6 +71,7 @@ module Belt
|
|
|
70
71
|
def run
|
|
71
72
|
validate!
|
|
72
73
|
generate_frontend_tf
|
|
74
|
+
install_router
|
|
73
75
|
ensure_cloudfront_cors
|
|
74
76
|
append_env_outputs
|
|
75
77
|
return if @quiet
|
|
@@ -96,6 +98,12 @@ module Belt
|
|
|
96
98
|
puts " create #{dest}" unless @quiet
|
|
97
99
|
end
|
|
98
100
|
|
|
101
|
+
# The CloudFront function frontend.tf loads with file().
|
|
102
|
+
def install_router
|
|
103
|
+
dest = FrontendRoutes.install_router(MODULE_DIR)
|
|
104
|
+
puts " create #{dest}" if dest && !@quiet
|
|
105
|
+
end
|
|
106
|
+
|
|
99
107
|
def append_env_outputs
|
|
100
108
|
Dir.glob('infrastructure/*/outputs.tf').reject { |f| f.include?('modules') }.each do |file|
|
|
101
109
|
before = File.read(file)
|
data/lib/belt/cli.rb
CHANGED
|
@@ -166,6 +166,8 @@ module Belt
|
|
|
166
166
|
dns show Show root zone name servers for registrar
|
|
167
167
|
frontend env <env> [--frontend NAME] Write <frontend>/.env from terraform outputs
|
|
168
168
|
frontend list List configured frontends
|
|
169
|
+
frontend prerender [--frontend NAME] Build + SEO prerender a frontend locally
|
|
170
|
+
frontend router Write the CloudFront router function (real 404s)
|
|
169
171
|
routes [-g PATTERN] [-f json] Show route definitions
|
|
170
172
|
contracts [-g PATTERN] [-f json] Show API request/response contracts
|
|
171
173
|
lambda-config [-e ENV] [-f json|terraform] Show merged lambda configuration
|
data/lib/belt/docs/frontend.md
CHANGED
|
@@ -89,27 +89,47 @@ fallback. See the existing env-map behavior: only mapped keys are written into
|
|
|
89
89
|
|
|
90
90
|
Search crawlers and AI answer engines (ChatGPT, Claude, Perplexity, Google AI)
|
|
91
91
|
mostly don't run JavaScript, so an SPA that ships an empty `<div id="root">` is
|
|
92
|
-
invisible to them.
|
|
93
|
-
|
|
92
|
+
invisible to them. Belt prerenders public pages as part of `belt deploy frontend`.
|
|
93
|
+
It's opt-in by manifest — the app owns the content, belt owns the mechanism:
|
|
94
94
|
|
|
95
95
|
```
|
|
96
|
-
src/seo.js # site name/description, public pages, private paths
|
|
97
|
-
src/entry-server.jsx #
|
|
98
|
-
scripts/prerender.js # runs at the end of `npm run build`
|
|
96
|
+
src/seo.js # the manifest: site name/description, public pages, private paths
|
|
97
|
+
src/entry-server.jsx # optional: exports render(url) → HTML of <App /> at that URL
|
|
99
98
|
```
|
|
100
99
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
100
|
+
React frontends from `belt generate frontend react` ship both. Any other Vite
|
|
101
|
+
frontend opts in by adding `src/seo.js` (same shape as the React template's). A
|
|
102
|
+
frontend without one deploys exactly as before.
|
|
103
|
+
|
|
104
|
+
After `npm run build`, belt builds `src/entry-server.*` with `vite build --ssr`
|
|
105
|
+
(into `node_modules/.cache/belt-ssr`) and, for every entry in `seo.pages`, writes
|
|
106
|
+
real HTML into the build output with the page's rendered content, `<title>`, meta
|
|
107
|
+
description, canonical URL, Open Graph tags and JSON-LD. It also writes
|
|
108
|
+
`robots.txt`, `sitemap.xml` (from the same page list; needs a site URL) and
|
|
109
|
+
`llms.txt`. No SSR entry, or one that fails to build, still gets the tags,
|
|
110
|
+
sitemap and robots — just no prerendered content. Because it all lands in the
|
|
111
|
+
build output, every deploy's `s3 sync --delete` reproduces it; nothing is
|
|
112
|
+
uploaded out of band. Each page goes to its extensionless S3 key (`/pricing` →
|
|
113
|
+
`pricing`, served as `text/html`), and HTML and crawler files are served with
|
|
114
|
+
`no-cache`.
|
|
115
|
+
|
|
116
|
+
`robots.txt`: in indexable environments a project `public/robots.txt` is used
|
|
117
|
+
verbatim; otherwise belt generates `Allow: /`, a `Disallow:` per private path and
|
|
118
|
+
the `Sitemap:` line. Non-indexable environments always get `Disallow: /`.
|
|
119
|
+
|
|
120
|
+
Preview locally with `belt frontend prerender [--frontend NAME]` (builds, then
|
|
121
|
+
prerenders into `dist/`). Frontends scaffolded by belt 0.6.6–0.6.9 prerender inside
|
|
122
|
+
`npm run build` (`scripts/prerender.js`); belt sees their `dist/.belt-pages.json`
|
|
123
|
+
and skips its own step.
|
|
107
124
|
|
|
108
125
|
**When you add a public route, add it to `seo.pages`** with a specific title and a
|
|
109
126
|
one- or two-sentence description that says plainly what the page is. Login-gated
|
|
110
127
|
routes go in `privatePaths`. Components on public pages must render without
|
|
111
128
|
`window`/`document`/`localStorage` — touch those in `useEffect`. A page that throws
|
|
112
|
-
during prerender still gets its tags, but no content, and the
|
|
129
|
+
during prerender still gets its tags, but no content, and the deploy warns.
|
|
130
|
+
The SSR build uses the app's `vite.config.js`, so a library that touches the
|
|
131
|
+
browser at import time (Cognito/Amplify, analytics) can be stubbed with a plugin
|
|
132
|
+
that only applies when `isSsrBuild` is true — public pages render signed-out.
|
|
113
133
|
|
|
114
134
|
Only environments in `seo.indexableEnvironments` (default `['prod']`) are
|
|
115
135
|
indexable; every other environment and local builds ship `Disallow: /` and
|
|
@@ -120,6 +140,56 @@ the build; set `SEO_INDEXABLE=true|false` to override.
|
|
|
120
140
|
After the first prod deploy, submit `https://<domain>/sitemap.xml` to Google
|
|
121
141
|
Search Console and Bing Webmaster Tools.
|
|
122
142
|
|
|
143
|
+
### Real 404s
|
|
144
|
+
|
|
145
|
+
A single-page app on S3 + CloudFront classically answers every path with
|
|
146
|
+
`index.html` and HTTP 200, because CloudFront can't tell which addresses the app
|
|
147
|
+
has. Crawlers and agents then can't tell a missing page from a real one. A
|
|
148
|
+
frontend with `src/seo.js` answers a real 404 instead:
|
|
149
|
+
|
|
150
|
+
- **A public page** (`seo.pages`) is served from its prerendered file.
|
|
151
|
+
- **A route of the app** is served `index.html`, as before.
|
|
152
|
+
- **Anything else** gets `404.html` with HTTP 404. That page is the app's own
|
|
153
|
+
"not found" route (`<Route path="*">`) prerendered, with `noindex`.
|
|
154
|
+
|
|
155
|
+
You don't keep a route list. The prerender step reads the app's routes from its
|
|
156
|
+
`<Route path="/...">` declarations under `src/` (test files are skipped), adds
|
|
157
|
+
`seo.privatePaths` as prefixes, and writes them to `dist/.belt-routes.json`.
|
|
158
|
+
`belt deploy frontend` publishes that to a CloudFront KeyValueStore, and belt's
|
|
159
|
+
router function (`infrastructure/modules/app/belt_frontend_router.js`, on the
|
|
160
|
+
distribution's viewer-request and viewer-response events) reads it at the edge.
|
|
161
|
+
The deploy prints what it published:
|
|
162
|
+
|
|
163
|
+
```
|
|
164
|
+
SEO: 14 app route(s) and 6 page(s) answer; any other address is a 404
|
|
165
|
+
🧭 Published routes: 5 page(s), 14 app path(s); anything else answers 404
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Belt only sees a route written as `<Route path="/literal/or/:param">`. A route
|
|
169
|
+
declared another way — a nested `<Route path="settings">`, a `createBrowserRouter`
|
|
170
|
+
config object, a path built at runtime — needs its prefix in `privatePaths`
|
|
171
|
+
(`'/account'` covers `/account` and everything under it). If one is missed,
|
|
172
|
+
nothing breaks in a browser: `404.html` loads the app, which routes to the real
|
|
173
|
+
page. Only the HTTP status is wrong, so check `dist/.belt-routes.json` after
|
|
174
|
+
adding a route in an unusual way.
|
|
175
|
+
|
|
176
|
+
A frontend that prerenders its own way (its `npm run build` writes the pages, as
|
|
177
|
+
the 0.6.6–0.6.9 templates did) still gets this: belt leaves its pages alone and
|
|
178
|
+
only adds the app routes and, if the build wrote none, `404.html`. Such a build
|
|
179
|
+
can write `dist/.belt-routes.json` itself to say where its pages are:
|
|
180
|
+
`{ "pages": ["/docs"], "objects": { "/": "/_pages/index.html", "/docs": "/_pages/docs.html" } }`.
|
|
181
|
+
`objects` is only needed for a page that isn't stored at its own extensionless
|
|
182
|
+
key.
|
|
183
|
+
|
|
184
|
+
Published routes reach the edge within about a minute of the deploy. Check with
|
|
185
|
+
`curl -s -o /dev/null -w '%{http_code}\n' https://<site>/no-such-page` (404) and
|
|
186
|
+
the same for a route of the app (200).
|
|
187
|
+
|
|
188
|
+
It fails open. Until a deploy has published routes, or if the store can't be
|
|
189
|
+
read or written, every path behaves as it did before (index.html, 200). Paths
|
|
190
|
+
with a file extension are never rewritten. Customize the 404 page's metadata
|
|
191
|
+
with `seo.notFound: { title, description }`.
|
|
192
|
+
|
|
123
193
|
## Infrastructure
|
|
124
194
|
|
|
125
195
|
`belt setup frontend` (and `belt generate frontend`) writes S3 + CloudFront into
|
|
@@ -127,6 +197,9 @@ Search Console and Bing Webmaster Tools.
|
|
|
127
197
|
`{name}_frontend.tf` with unique resource names and outputs
|
|
128
198
|
(`ops_frontend_bucket_name`, …). Extra frontends get a CloudFront URL only —
|
|
129
199
|
custom DNS stays on the default frontend unless you add records yourself.
|
|
200
|
+
The generated distribution includes the router function and KeyValueStore for
|
|
201
|
+
real 404s (above); `belt frontend router` rewrites `belt_frontend_router.js`
|
|
202
|
+
after a belt upgrade changes it.
|
|
130
203
|
|
|
131
204
|
CORS: each CloudFront domain is added to `frontend_urls` on the conveyor-belt
|
|
132
205
|
resource so SPA → API calls work.
|