From a21cde8a5a5143db4fae5413869def2dd9f74c96 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Fri, 17 Oct 2025 09:15:33 +0200 Subject: [PATCH] [ty] Fix playground crash for very large files (#20934) --- playground/ruff/src/Editor/settings.ts | 9 +++++++++ playground/ty/src/Editor/persist.ts | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/playground/ruff/src/Editor/settings.ts b/playground/ruff/src/Editor/settings.ts index 411399da5d..7a7be3d95b 100644 --- a/playground/ruff/src/Editor/settings.ts +++ b/playground/ruff/src/Editor/settings.ts @@ -79,6 +79,15 @@ export function persistLocal({ settingsSource: string; pythonSource: string; }) { + const totalLength = settingsSource.length + pythonSource.length; + + // Don't persist large files to local storage because they can exceed the local storage quota + // The number here is picked rarely arbitrarily. Also note, JS uses UTF 16: + // that means the limit here is strings larger than 1MB (because UTf 16 uses 2 bytes per character) + if (totalLength > 500_000) { + return; + } + localStorage.setItem( "source", JSON.stringify([settingsSource, pythonSource]), diff --git a/playground/ty/src/Editor/persist.ts b/playground/ty/src/Editor/persist.ts index 2adc5f3fe9..1767e14d0f 100644 --- a/playground/ty/src/Editor/persist.ts +++ b/playground/ty/src/Editor/persist.ts @@ -40,6 +40,18 @@ export async function restore(): Promise { } export function persistLocal(workspace: Workspace) { + let totalLength = 0; + for (const fileContent of Object.values(workspace.files)) { + totalLength += fileContent.length; + + // Don't persist large files to local storage because they can exceed the local storage quota + // The number here is picked rarely arbitrarily. Also note, JS uses UTF 16: + // that means the limit here is strings larger than 1MB (because UTf 16 uses 2 bytes per character) + if (totalLength > 500_000) { + return; + } + } + localStorage.setItem("workspace", JSON.stringify(workspace)); }