rsnext/packages/next/next-server/lib/to-base-64.ts
Steven b2a8a2f99e
Fix html validation for Image component (#18903)
This PR fixes two bugs causing HTML validators to complain.

- Error: Bad value data:image/svg+xml;charset=utf-8, for attribute src on element img: Illegal character in scheme data: < is not allowed.
  - Fixed by using base64 for svg during `layout=intrinsic` to avoid angle brackets
- Error: Element img is missing required attribute src.
  - Fixed by using base64 transparent gif for `loading=lazy` placeholder

Fixes #18850
2020-11-07 17:39:14 +00:00

10 lines
236 B
TypeScript

/**
* Isomorphic base64 that works on the server and client
*/
export function toBase64(str: string) {
if (typeof window === 'undefined') {
return Buffer.from(str).toString('base64')
} else {
return window.btoa(str)
}
}