Skip to content

Commit 7a494d5

Browse files
committed
fix: encoding/decoding
1 parent e9235ac commit 7a494d5

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

docs/app/playground/page.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,25 @@ import { useCallback, useEffect, useRef, useState } from "react"
44
import type { VM } from "@stackblitz/sdk"
55
import { StackBlitzPlayground } from "../../components/playground/StackBlitzPlayground"
66

7-
const encodeCode = (code: string) => btoa(encodeURIComponent(code))
8-
const decodeCode = (encoded: string) => {
7+
// Encode/decode code for URL sharing - handles Unicode properly with base64url
8+
const encodeCode = (code: string): string => {
9+
const bytes = new TextEncoder().encode(code)
10+
const binString = Array.from(bytes, (byte) => String.fromCodePoint(byte)).join("")
11+
return btoa(binString)
12+
.replace(/\+/g, "-")
13+
.replace(/\//g, "_")
14+
.replace(/=+$/, "")
15+
}
16+
17+
const decodeCode = (encoded: string): string | null => {
918
try {
10-
return decodeURIComponent(atob(encoded))
19+
const base64 = encoded
20+
.replace(/-/g, "+")
21+
.replace(/_/g, "/")
22+
const padded = base64.padEnd(base64.length + (4 - base64.length % 4) % 4, "=")
23+
const binString = atob(padded)
24+
const bytes = Uint8Array.from(binString, (char) => char.codePointAt(0)!)
25+
return new TextDecoder().decode(bytes)
1126
} catch {
1227
return null
1328
}

0 commit comments

Comments
 (0)