[Pending internal discussions] tweak the parsing of _redirects files#12490
[Pending internal discussions] tweak the parsing of _redirects files#12490
Conversation
🦋 Changeset detectedLatest commit: 09e3bb1 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
da8987b to
7e8c7fc
Compare
create-cloudflare
@cloudflare/kv-asset-handler
miniflare
@cloudflare/pages-shared
@cloudflare/unenv-preset
@cloudflare/vite-plugin
@cloudflare/vitest-pool-workers
@cloudflare/workers-editor-shared
@cloudflare/workers-utils
wrangler
commit: |
packages/workers-shared/utils/tests/parseRedirects.valid.test.ts
Outdated
Show resolved
Hide resolved
petebacondarwin
left a comment
There was a problem hiding this comment.
The "valid" test needs fixing.
packages/workers-shared/utils/tests/parseRedirects.valid.test.ts
Outdated
Show resolved
Hide resolved
0ded52c to
35096b4
Compare
35096b4 to
09e3bb1
Compare
|
Claude finished @vicb's task —— View job Changeset ReviewReviewing changeset files...
The changeset
Suggestions for improvement: ---
"@cloudflare/workers-shared": patch
---
Exclude comments from `_redirects` line length validation
Previously, comments in `_redirects` files counted toward the maximum line length, which could cause valid redirects with comments to be rejected. Comments are now excluded from length validation. Additionally, the maximum line length for redirect directives has been corrected to 1000 characters (matching the Workers platform limit), down from 2000. |
Follow up for #12467
The main idea of this PR is that the maximum line length of a
_redirectsshould not take comments into account.There are also a few other changes.
There was a single
MAX_LINE_LENGTHused for both the_headersand_redirectswhile the first one should be 2000 and second one 1000 (See opened questions below for that one). The new constants areMAX_HEADER_LINE_LENGTHandMAX_REDIRECT_LINE_LENGTH.Note that other constants have been renamed to clearly indicated when they relates to either
HEADERorREDIRECT.Also a bit of cleanup in the tests to make them consistent.
question
_redirectsparser version, not sure how it is used - it is defined both here and EWCfuture work
Usage of
_redirects(by Claude and I)1. Workers Assets deploy (
wrangler deploy/wrangler versions upload)packages/wrangler/src/assets.ts:469—maybeGetFile()reads raw textpackages/wrangler/src/deploy/deploy.ts:867(deploy),packages/wrangler/src/versions/upload.ts:726(versions upload)packages/wrangler/src/deployment-bundle/create-worker-upload-form.ts:90—raw string embedded in
AssetConfigMetadataJSON inside the FormDatametadatafieldparseRedirects? No — the Cloudflare API parses it server-side2. Workers Assets remote dev (
wrangler dev --remote)packages/wrangler/src/dev/remote.ts:197parseRedirects? No3. Pages deploy (
wrangler pages deploy)packages/wrangler/src/api/pages/deploy.ts:136—readFileSync()packages/wrangler/src/api/pages/deploy.ts:297-299— appended as aFileobject in FormData (not inside JSON)
parseRedirects? No — the Pages API parses it server-side4. Miniflare local dev (Workers Assets,
wrangler dev --local)packages/miniflare/src/plugins/assets/index.ts:99-124parseRedirects()→constructRedirects()→RedirectsSchema.parse()→ injected asJSON
CONFIGbinding into the local workerd asset-workerparseRedirects? Yes5. Pages local dev (
wrangler pages dev)packages/wrangler/src/miniflare-cli/assets.ts:150-161parseRedirects()→createMetadataObject()→constructRedirects()parseRedirects? Yes6. Vite plugin dev (experimental)
packages/vite-plugin-cloudflare/src/asset-config.ts:105-117parseRedirects()→constructRedirects()→RedirectsSchema.parse()→ passed toMiniflare options
experimental.headersAndRedirectsDevModeSupporthasAssetsConfigChanged()(line 27-46)parseRedirects? Yes7. Runtime: Workers asset-worker
packages/workers-shared/asset-worker/src/handler.ts:48,999-1050packages/workers-shared/asset-worker/src/utils/rules-engine.ts:116-160configuration.redirectsviaCONFIGbindingstaticRedirectsMatcher()(O(1) dict lookup) thengenerateRedirectsMatcher()(regex-based dynamic matching) on each request
8. Runtime: Pages asset-server
packages/pages-shared/asset-server/handler.ts:202-243metadata.redirectspackages/pages-shared/metadata-generator/createMetadataObject.tsA picture of a cute animal (not mandatory, but encouraged)