Repository navigation
fix(platform-web): accept null in parseLocales for a missing Accept-Language header - #244
Merged
Merged
Conversation
…ept-Language` header
`Headers.get('accept-language')` returns `null` when the request carries no such header, while `parseLocales` only defaulted `undefined` and then read `.length`, so the documented `parseLocales(request.headers.get('accept-language'))` threw on the first request without the header. The parameter is now `string | null | undefined`, and both missing shapes fall back to `en` like an empty header does.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #244 +/- ##
==========================================
+ Coverage 83.28% 83.32% +0.03%
==========================================
Files 98 98
Lines 2554 2554
Branches 549 549
==========================================
+ Hits 2127 2128 +1
Misses 314 314
+ Partials 113 112 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Headers.get('accept-language')returnsnullwhen the request has noAccept-Languageheader.parseLocales(acceptLanguage = '')only defaultedundefinedand then readacceptLanguage.length, so the example from the docs,parseLocales(request.headers.get('accept-language')), threwTypeError: Cannot read properties of null (reading 'length')on the first request without the header (curl, bots, health checks). UnderstrictTypeScript the example did not even type-check, sincestring | nullis not assignable tostring | undefined.The built-in SSR renderer was not affected: it passes
headers?.acceptLanguage, which is typedstring | undefined.What
parseLocalesnow takesacceptLanguage?: string | nulland normalizes it withacceptLanguage ||= '', sonullbehaves likeundefinedand'':{ language: 'en', languages: ['en'] }.eslint/no-param-reassignis turned off for the package inoxlint.config.ts, the same wayageradoes it. A localconstwould have cost 4 B of gzip and pushed the package over its 3.4 kB size limit; the reassignment keeps it at 3399 B.should fallback to english when header is null.Checks
oxlint,tsc --noEmit,vitest run(58 tests) inpackages/platform-webpass.size-limit: 3399 B gzip (limit 3400 B), 3027 B brotli (limit 3.05 kB).