Fix UTF-8 mojibake and sorting for non-ASCII characters in style names - #174
Conversation
Co-authored-by: richardthe3rd <573334+richardthe3rd@users.noreply.github.com>
Co-authored-by: richardthe3rd <573334+richardthe3rd@users.noreply.github.com>
Co-authored-by: richardthe3rd <573334+richardthe3rd@users.noreply.github.com>
Co-authored-by: richardthe3rd <573334+richardthe3rd@users.noreply.github.com>
|
@copilot Can we make the cloudflare worker return correct response saying it's utf8? |
Co-authored-by: richardthe3rd <573334+richardthe3rd@users.noreply.github.com>
Yes! Updated the Cloudflare Worker to explicitly set The worker now:
This provides defense-in-depth - even though the client now handles UTF-8 correctly, the server explicitly declares the encoding so browsers won't guess incorrectly. |
LCOV of commit
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
🚀 Cloudflare Pages PreviewYour preview deployment is ready! Preview URL: https://copilot-fix-non-english-char.staging-cambeerfestival.pages.dev This preview will be automatically updated when you push new commits to this PR. |
There was a problem hiding this comment.
Pull request overview
This PR fixes UTF-8 mojibake issues where non-ASCII characters (é, ö, ä, ñ) were displaying as garbled text like "Rosé" instead of "Rosé", and implements locale-aware sorting to ensure accented characters sort alphabetically near their base characters rather than at the end of lists.
Key Changes:
- Fixed UTF-8 decoding in API service by using
utf8.decode(response.bodyBytes)instead ofresponse.body - Added case-insensitive, locale-aware string comparison helper for proper sorting of accented characters
- Updated Cloudflare Worker to explicitly declare UTF-8 charset in all JSON responses for defense-in-depth
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
lib/services/beer_api_service.dart |
Switched from response.body to utf8.decode(response.bodyBytes) to fix mojibake caused by Latin-1 default decoding |
lib/utils/string_comparison_helper.dart |
New helper class providing case-insensitive, locale-aware string comparison for proper sorting of accented characters |
lib/utils/utils.dart |
Added export for new string comparison helper in barrel file |
lib/screens/drinks_screen.dart |
Updated style filter to use locale-aware sorting instead of binary comparison |
cloudflare-worker/worker.js |
Added explicit charset=utf-8 to all JSON responses and logic to fix proxied responses missing charset |
test/utf8_encoding_test.dart |
Comprehensive tests (3 test cases) verifying UTF-8 characters decode correctly without mojibake |
test/string_comparison_helper_test.dart |
Thorough tests (9 test cases) for locale-aware comparison including edge cases and transitivity |
test/drinks_screen_style_filter_test.dart |
Integration test verifying accented style names display and sort correctly in the UI |
Review Summary: The implementation is solid with excellent test coverage (12 new tests added, 367 total tests passing). The code is well-documented with clear comments explaining the technical reasons for the changes. The fix addresses both the root cause (UTF-8 decoding) and provides defense-in-depth (server-side charset headers). No issues found in the changed files.
Non-ASCII characters (é, ö, ä, ñ) were displaying as mojibake ("Rosé" → "Rosé") and sorting incorrectly in the style filter.
Changes
UTF-8 decoding fix (
beer_api_service.dart)json.decode(response.body)tojson.decode(utf8.decode(response.bodyBytes))httppackage decodesresponse.bodyas Latin-1 when charset is unspecified in Content-Type, causing UTF-8 bytes (0xC3 0xA9 for "é") to be misinterpreted as Latin-1 chars ("é")Locale-aware sorting (
string_comparison_helper.dart,drinks_screen.dart)Cloudflare Worker UTF-8 headers (
cloudflare-worker/worker.js)Content-Type: application/json; charset=utf-8on all JSON responsesTesting
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.