{"record":{"id":"07da6443429f40ca","repo":"evanw/esbuild","slug":"could-not-decode-base64-data-s","errorCode":null,"errorMessage":"could not decode base64 data: %s","messagePattern":"could not decode base64 data: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/resolver/dataurl.go","lineNumber":65,"sourceCode":"\t// Hard-code a few supported types\n\tswitch mimeType {\n\tcase \"text/css\":\n\t\treturn MIMETypeTextCSS\n\tcase \"text/javascript\":\n\t\treturn MIMETypeTextJavaScript\n\tcase \"application/json\":\n\t\treturn MIMETypeApplicationJSON\n\tdefault:\n\t\treturn MIMETypeUnsupported\n\t}\n}\n\nfunc (parsed DataURL) DecodeData() (string, error) {\n\t// Try to read base64 data\n\tif parsed.isBase64 {\n\t\tbytes, err := base64.StdEncoding.DecodeString(parsed.data)\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"could not decode base64 data: %s\", err.Error())\n\t\t}\n\t\treturn string(bytes), nil\n\t}\n\n\t// Try to read percent-escaped data\n\tcontent, err := url.PathUnescape(parsed.data)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"could not decode percent-escaped data: %s\", err.Error())\n\t}\n\treturn content, nil\n}\n","sourceCodeStart":47,"sourceCodeEnd":77,"githubUrl":"https://github.com/evanw/esbuild/blob/f6058f8364fe7ab91ca57a83e02577ed74c9cae4/internal/resolver/dataurl.go#L47-L77","documentation":"When esbuild resolves a data: URL with the ;base64 flag, it calls Go's base64.StdEncoding.DecodeString on the payload. If the base64 data is malformed (wrong characters, incorrect padding, truncated), the decode fails and this error is returned.","triggerScenarios":"Importing a data: URL like 'data:text/javascript;base64,...' where the base64 portion contains invalid characters or incorrect padding.","commonSituations":"Hand-constructed data URLs with truncated base64, copy-paste errors introducing whitespace or non-base64 characters, or incorrect padding (missing '=' characters).","solutions":["Validate the base64 payload with a decoder before using it as a data URL import","Ensure standard base64 encoding with correct padding characters (= or ==)","Switch to percent-encoding instead of base64 if the content is plain text"],"exampleFix":"// before\nimport mod from 'data:text/javascript;base64,ZXhwb3J0IGRlZmF1bHQgMQ'\n// after (fix padding)\nimport mod from 'data:text/javascript;base64,ZXhwb3J0IGRlZmF1bHQgMQ=='","handlingStrategy":"validation","validationCode":"function makeDataUrl(mimeType, content) {\n  const base64 = Buffer.from(content).toString('base64')\n  // Verify round-trip\n  if (Buffer.from(base64, 'base64').toString() !== content) {\n    throw new Error('base64 encoding round-trip failed')\n  }\n  return `data:${mimeType};base64,${base64}`\n}\nconst url = makeDataUrl('text/javascript', 'export default 1')\n// import mod from url // safe to import now","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Generate data URLs programmatically using Buffer/TextEncoder instead of hand-typing base64","Avoid copying base64 strings from untrusted sources without validation","Prefer percent-encoding over base64 for short text payloads"],"tags":["esbuild","data-url","base64","resolver"],"backgroundTag":null,"analyzedSha":"f6058f8364fe7ab91ca57a83e02577ed74c9cae4","analyzedAt":"2026-08-09T18:37:22.223Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}