Skip to content

Commit db8dea4

Browse files
authored
fix build script (tldraw#8352)
we were compiling too many asset urls into a single pattern to match against for our assets caching rules. this splits them up into multiple rules kudos @MitjaBezensek ### Change type - [ ] `bugfix` - [ ] `improvement` - [ ] `feature` - [ ] `api` - [x] `other`
1 parent 839e118 commit db8dea4

3 files changed

Lines changed: 49 additions & 15 deletions

File tree

apps/dotcom/client/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"@types/qrcode": "^1.5.5",
7272
"@types/react": "^19.2.7",
7373
"@types/react-dom": "^19.2.3",
74+
"@types/regexgen": "^1",
7475
"@vitejs/plugin-react": "^6.0.1",
7576
"dotenv": "^16.4.7",
7677
"fast-glob": "^3.3.3",
@@ -79,6 +80,7 @@
7980
"kysely": "^0.28.12",
8081
"lazyrepo": "0.0.0-alpha.27",
8182
"pg": "^8.13.1",
83+
"regexgen": "^1.3.0",
8284
"vite": "^8.0.1",
8385
"vitest": "^3.2.4",
8486
"ws": "^8.18.0"

apps/dotcom/client/scripts/build.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { T } from '@tldraw/validate'
33
import { config } from 'dotenv'
44
import glob from 'fast-glob'
55
import json5 from 'json5'
6+
import _ from 'lodash'
7+
import regexgen from 'regexgen'
68
import { exec } from '../../../../internal/scripts/lib/exec'
79
import { nicelog } from '../../../../internal/scripts/lib/nicelog'
810
import { csp } from '../src/utils/csp'
@@ -46,7 +48,7 @@ async function build() {
4648
// make sure we have the latest routes
4749
await exec('yarn', ['test', 'src/routes.test.tsx'])
4850
const spaRoutes = loadSpaRoutes()
49-
await exec('vite', ['build', '--emptyOutDir'])
51+
await exec('../../../node_modules/.bin/vite', ['build', '--emptyOutDir'])
5052
await exec('yarn', ['run', '-T', 'sentry-cli', 'sourcemaps', 'inject', 'dist/assets'])
5153
// Clear output static folder (in case we are running locally and have already built the app once before)
5254
await exec('rm', ['-rf', '.vercel/output'])
@@ -90,17 +92,10 @@ async function build() {
9092
writeFileSync('.vercel/output/static/index.html', newIndex)
9193

9294
const multiplayerServerUrl = getMultiplayerServerURL() ?? 'http://localhost:8787'
93-
const assetExtensions = [
94-
...new Set(
95-
assetsList
96-
.filter((f) => !f.endsWith('.js.map') && f.includes('.'))
97-
.map((f) => f.split('.').pop()!)
98-
),
99-
]
100-
if (assetExtensions.length === 0) {
101-
throw new Error('No asset extensions found in dist/assets')
102-
}
103-
const assetsToCacheRegex = `^\\/assets\\/.+\\.(${assetExtensions.join('|')})$`
95+
96+
const assetsToCache = assetsList.filter((f) => !f.endsWith('.js.map')).map((f) => `/assets/${f}`)
97+
// need to batch these because Vercel's route limit is 4096 characters
98+
const assetsBatches = _.chunk(assetsToCache, 50)
10499

105100
writeFileSync(
106101
'.vercel/output/config.json',
@@ -125,12 +120,12 @@ async function build() {
125120
},
126121
// cache static assets immutably. we match by extension to avoid exceeding
127122
// Vercel's 4096-char route limit (see #8286).
128-
{
129-
src: assetsToCacheRegex,
123+
...assetsBatches.map((batch) => ({
124+
src: `^${regexgen(batch).source}$`,
130125
headers: {
131126
'Cache-Control': 'public, max-age=31536000, immutable',
132127
},
133-
},
128+
})),
134129
// server up index.html specifically because we want to include
135130
// security headers. otherwise, it goes to the handle: 'miss'
136131
// part below (and _not_ to the spaRoutes as maybe expected!)

yarn.lock

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12618,6 +12618,13 @@ __metadata:
1261812618
languageName: node
1261912619
linkType: hard
1262012620

12621+
"@types/regexgen@npm:^1":
12622+
version: 1.3.3
12623+
resolution: "@types/regexgen@npm:1.3.3"
12624+
checksum: 10/aa95eeb89adc724edc9babf5d2b68d512ae09f1c8fb633997ae03fedfc09c30a5ea00a4bba3919a5e3f1ba02ec379e2e1ca3e147d99d05f38858ff477cb87855
12625+
languageName: node
12626+
linkType: hard
12627+
1262112628
"@types/retry@npm:*":
1262212629
version: 0.12.5
1262312630
resolution: "@types/retry@npm:0.12.5"
@@ -17100,6 +17107,7 @@ __metadata:
1710017107
"@types/qrcode": "npm:^1.5.5"
1710117108
"@types/react": "npm:^19.2.7"
1710217109
"@types/react-dom": "npm:^19.2.3"
17110+
"@types/regexgen": "npm:^1"
1710317111
"@vitejs/plugin-react": "npm:^6.0.1"
1710417112
axe-core: "npm:^4.10.3"
1710517113
browser-fs-access: "npm:^0.35.0"
@@ -17124,6 +17132,7 @@ __metadata:
1712417132
react-intl: "npm:^8.1.3"
1712517133
react-markdown: "npm:^10.1.0"
1712617134
react-router-dom: "npm:^6.28.2"
17135+
regexgen: "npm:^1.3.0"
1712717136
tldraw: "workspace:*"
1712817137
vite: "npm:^8.0.1"
1712917138
vitest: "npm:^3.2.4"
@@ -21130,6 +21139,15 @@ __metadata:
2113021139
languageName: node
2113121140
linkType: hard
2113221141

21142+
"jsesc@npm:^2.3.0":
21143+
version: 2.5.2
21144+
resolution: "jsesc@npm:2.5.2"
21145+
bin:
21146+
jsesc: bin/jsesc
21147+
checksum: 10/d2096abdcdec56969764b40ffc91d4a23408aa2f351b4d1c13f736f25476643238c43fdbaf38a191c26b1b78fd856d965f5d4d0dde7b89459cd94025190cdf13
21148+
languageName: node
21149+
linkType: hard
21150+
2113321151
"jsesc@npm:^3.0.2":
2113421152
version: 3.1.0
2113521153
resolution: "jsesc@npm:3.1.0"
@@ -26585,6 +26603,13 @@ __metadata:
2658526603
languageName: node
2658626604
linkType: hard
2658726605

26606+
"regenerate@npm:^1.3.2":
26607+
version: 1.4.2
26608+
resolution: "regenerate@npm:1.4.2"
26609+
checksum: 10/dc6c95ae4b3ba6adbd7687cafac260eee4640318c7a95239d5ce847d9b9263979758389e862fe9c93d633b5792ea4ada5708df75885dc5aa05a309fa18140a87
26610+
languageName: node
26611+
linkType: hard
26612+
2658826613
"regex-recursion@npm:^5.1.1":
2658926614
version: 5.1.1
2659026615
resolution: "regex-recursion@npm:5.1.1"
@@ -26611,6 +26636,18 @@ __metadata:
2661126636
languageName: node
2661226637
linkType: hard
2661326638

26639+
"regexgen@npm:^1.3.0":
26640+
version: 1.3.0
26641+
resolution: "regexgen@npm:1.3.0"
26642+
dependencies:
26643+
jsesc: "npm:^2.3.0"
26644+
regenerate: "npm:^1.3.2"
26645+
bin:
26646+
regexgen: bin/cli.js
26647+
checksum: 10/b23d4b743e8f7312db4622e3384b86dc0fac3a2dcc1f4a4daa70b678b4ee817e9a5cf22d2634576aca213306525b44ef23678d0b1d44cc42f95acdacc44b3c5e
26648+
languageName: node
26649+
linkType: hard
26650+
2661426651
"rehype-autolink-headings@npm:^7.1.0":
2661526652
version: 7.1.0
2661626653
resolution: "rehype-autolink-headings@npm:7.1.0"

0 commit comments

Comments
 (0)