Writing .ics files by hand in vanilla JS: the 5 bugs I hit (octet folding, all-day alarms, stable UIDs)
I built a small browser-only tool that turns a list of subscriptions into calendar reminders for "cancel by" dates. No library, no server: the page builds the .ics text itself and hands it to the browser as a download. R
I built a small browser-only tool that turns a list of subscriptions into calendar reminders for "cancel by" dates. No library, no server: the page builds the .ics text itself and hands it to the browser as a download. RFC 5545 looks simple until you test the output in a strict parser. Here are the five things that broke, and the fixes.
1. Line folding counts octets, not characters
RFC 5545 says content lines SHOULD NOT be longer than 75 octets, excluding the line break. Longer lines are folded with CRLF followed by a single space.
"abc".length counts UTF-16 code units, not bytes. A Japanese or emoji subscription name blows past 75 bytes while .length still says 40.
const enc = new TextEncoder();
function fold(line) {
const out = [];
let cur = "", curBytes = 0;
// tokenise so we never split a code point OR a backslash escape like "\\,"
for (const tok of line.match(/\\[\s\S]|[\s\S]/gu) || []) {
const b = enc.encode(tok).length;
const limit = out.length === 0 ? 75 : 74; // continuation lines start with a space
if (curBytes + b > limit) { out.push(cur); cur = ""; curBytes = 0; }
cur += tok; curBytes += b;
}
out.push(cur);
return out.join("\r\n ");
}
The u flag stops a surrogate pair being split. Matching \\[\s\S] as a single token stops an escaped comma (\,) being cut in half.
2. Escape the text values
In TEXT values, escape \, ;, , and newlines. Do backslashes first:
const esc = s => s.replace(/\\/g, "\\\\").replace(/;/g, "\\;").replace(/,/g, "\\,").replace(/\r?\n/g, "\\n");
3. A VALARM on an all-day event is relative to midnight
For DTSTART;VALUE=DATE:20261201, the trigger offset counts from 00:00 that day. "Remind me at 9:00 the day before" is therefore minus 15 hours, not minus 1 day:
BEGIN:VALARM
ACTION:DISPLAY
DESCRIPTION:Cancel before renewal
TRIGGER:-PT15H
END:VALARM
4. Stable UIDs mean re-imports update events instead of duplicating them
If the UID is random, re-downloading the file after an edit gives users two copies of every event. I hash name + currency + renewal date into the UID. Most calendar apps then treat the second import as an update. Results vary by app, so say "generally" in your UI rather than promising it.
5. Month-end dates drift if you step from the previous date
A plan renewing on 31 Jan: stepping one month at a time gives 31 Jan -> 28 Feb -> 28 Mar, which is wrong. Always compute anchor + k months from the original date and clamp to the month's last day: 31 Jan -> 28/29 Feb -> 31 Mar. Do the arithmetic on UTC day numbers (Date.UTC(...) / 86400000) so DST and the user's time zone never shift a date.
How I tested it
- A tiny validator in the test suite: CRLF only, every physical line <= 75 octets, BEGIN/END balanced, no unescaped
,or;in TEXT values, one VALARM per VEVENT. - One round-trip through Python's
icalendarpackage as an independent parser. - jsdom for the UI, with
URL.createObjectURLstubbed so the test can read the generated file.
Bonus: CSV export has its own trap
If users can export their list to CSV, prefix cells that start with = + - @ with an apostrophe (formula injection). Add a UTF-8 BOM and use CRLF if you want Excel to open the file cleanly.
The tool is free and runs entirely in the browser, with no sign-up and no network requests: Subscription cancel-by planner. View source if you want to see the whole generator. Disclosure: I made it, and parts of this write-up were drafted with AI assistance and checked by hand.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.