facebook/lexical · error
Unrecognized charset
Error message
Unrecognized charset
What it means
LexicalCharacterLimitPlugin computes the current text length using a charset-dependent strlen function. Only 'UTF-8' (byte-length counting via utf8Length) and 'UTF-16' (JS string length) are supported; passing any other charset value makes the memoized strlen throw 'Unrecognized charset'.
Source
Thrown at packages/lexical-react/src/LexicalCharacterLimitPlugin.tsx:91
remainingCharacters,
}: {
remainingCharacters: number;
}) => JSX.Element;
}): JSX.Element {
const [editor] = useLexicalComposerContext();
const [remainingCharacters, setRemainingCharacters] = useState(maxLength);
const characterLimitProps = useMemo(
() => ({
remainingCharacters: setRemainingCharacters,
strlen: (text: string) => {
if (charset === 'UTF-8') {
return utf8Length(text);
} else if (charset === 'UTF-16') {
return text.length;
} else {
throw new Error('Unrecognized charset');
}
},
}),
[charset],
);
useCharacterLimit(editor, maxLength, characterLimitProps);
return renderer({remainingCharacters});
}
View on GitHub (pinned to 76a22dcba9)
Solutions
- Pass charset='UTF-8' or charset='UTF-16' exactly (case-sensitive, with hyphen).
- Normalize any external config value to one of the two supported strings before rendering.
- If you need another encoding, supply the counting logic yourself or omit the plugin and compute limits manually.
- Check the plugin's prop types (charset?: 'UTF-8' | 'UTF-16') to catch invalid values at compile time.
Example fix
// before
<CharacterLimitPlugin charset="utf-8" maxLength={280} />
// after
<CharacterLimitPlugin charset="UTF-8" maxLength={280} /> Defensive patterns
Strategy: validation
Validate before calling
const SUPPORTED = new Set(['UTF-8', 'UTF-16']);
if (!SUPPORTED.has(charset)) {
throw new Error(`charset must be 'UTF-8' or 'UTF-16', got ${charset}`);
} Type guard
function isSupportedCharset(c: unknown): c is 'UTF-8' | 'UTF-16' {
return c === 'UTF-8' || c === 'UTF-16';
} Try / catch
let strlen: (text: string) => number;
try {
strlen = computeStrlen(charset);
} catch (e) {
if (e instanceof Error && e.message === 'Unrecognized charset') {
strlen = (t) => t.length; // fallback to UTF-16 semantics
} else { throw e; }
} Prevention
- Use the exact strings 'UTF-8'/'UTF-16'.
- Type the prop as 'UTF-8' | 'UTF-16' so TS rejects invalid values.
- Normalize config-driven charset values with a whitelist map.
When it happens
Trigger: Rendering <CharacterLimitPlugin charset="..." /> with a charset value other than the string 'UTF-8' or 'UTF-16' — e.g. 'utf-8' (lowercase), 'ASCII', undefined, or a typo like 'UTF8'.
Common situations: Passing lowercase 'utf-8' from a config value; assuming 'UTF8' (no hyphen) is valid; feeding charset from an enum defined elsewhere with unsupported values; leaving charset undefined expecting a default.
Related errors
- HashtagPlugin: HashtagNode not registered on editor
- LinkPlugin: LinkNode not registered on editor
- ListPlugin: ListNode and/or ListItemNode not registered on e
- CodeHighlightPlugin: CodeNode or CodeHighlightNode not regis
- CodeHighlightPlugin: CodeNode or CodeHighlightNode not regis
AI-assisted analysis of facebook/lexical@76a22dcba9 (2026-08-31).
Data as JSON: /api/errors/b89c3e5b73fcf421.
Report an issue: GitHub.