{"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/6ff1d8b0d8c134e867a397eef39702a223ebef9e/internal/resolver/dataurl.go#L47-L77","documentation":"Returned by DataURL.DecodeData when a `data:` URL marked as base64 (`;base64` suffix) contains bytes that fail Go's base64.StdEncoding.DecodeString. esbuild encounters data URLs during import/URL resolution (e.g. `import x from 'data:text/javascript;base64,...'`). This indicates the data URL is malformed at the byte level, not just unsupported.","triggerScenarios":"An import specifier or CSS url(...) resolves to a data: URL whose payload after the comma is not valid standard base64: wrong length, non-alphabet characters, or a missing ';base64' marker mismatch. Reproduces when bundling source that embeds assets as data URLs.","commonSituations":"Hand-authored data URLs with typos; tools that emit URL-safe base64 ('-_' alphabet) into a standard-base64 slot; truncation of a data URL by a linter or templating engine; copy-paste that drops trailing '=' padding.","solutions":["Re-encode the payload with standard base64 padding (alphabet A-Za-z0-9+/).","If the data is URL-safe base64, convert it to standard base64 or drop the ';base64' marker and percent-escape instead.","Validate the data URL with a one-liner (e.g. Buffer.from(payload, 'base64')) before feeding it to esbuild.","If the asset is large, prefer a file import with a loader (LoaderFile/LoaderBase64) instead of an inline data URL."],"exampleFix":"// before\nimport x from 'data:text/javascript;base64,YWxlcnQoMQ==' // but with a stray char\n\n// after\nimport x from 'data:text/javascript;base64,YWxlcnQoMSk='  // valid standard base64","handlingStrategy":"validation","validationCode":"// Verify a base64 data URL payload decodes before feeding it to esbuild.\nfunction checkBase64DataUrl(url) {\n  if (!url.startsWith('data:') || !url.includes(';base64,')) return true\n  const payload = url.slice(url.indexOf(',') + 1)\n  try { Buffer.from(payload, 'base64') } catch { return false }\n  return true\n}\nif (!checkBase64DataUrl(specifier)) throw new Error('Malformed base64 data URL')","typeGuard":"function isStandardBase64(payload: string): boolean {\n  return /^[A-Za-z0-9+/]*={0,2}$/.test(payload) && payload.length % 4 === 0\n}","tryCatchPattern":"try {\n  await esbuild.build({ ... })\n} catch (e) {\n  if (/could not decode base64 data/i.test(e.message)) {\n    console.error('A data URL import has invalid base64:', e.message)\n  }\n  throw e\n}","preventionTips":["Generate data URLs from bytes via Buffer.from(x).toString('base64'), not by hand.","Prefer file-based loaders (loader: 'file' | 'base64') over inline data URLs for assets.","Lint import specifiers matching /^data:.*;base64,/ in a pre-build step."],"tags":["resolver","data-url","base64","import","encoding"],"analyzedSha":"6ff1d8b0d8c134e867a397eef39702a223ebef9e","analyzedAt":"2026-08-03T19:42:38.433Z","schemaVersion":2}