Skip to content

Commit

Permalink
feat(useImage): support referrerPolicy option (#3132)
Browse files Browse the repository at this point in the history
  • Loading branch information
btea committed Jun 6, 2023
1 parent 78a3a62 commit 04d32d8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/core/useImage/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const UseImage = /*#__PURE__*/ defineComponent<UseImageOptions & Renderab
'class',
'loading',
'crossorigin',
'referrerPolicy',
] as unknown as undefined,
setup(props, { slots }) {
const data = reactive(useImage(props))
Expand Down
7 changes: 6 additions & 1 deletion packages/core/useImage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ export interface UseImageOptions {
loading?: HTMLImageElement['loading']
/** Image CORS settings */
crossorigin?: string
/** Referrer policy for fetch https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy */
referrerPolicy?: HTMLImageElement['referrerPolicy']
}

async function loadImage(options: UseImageOptions): Promise<HTMLImageElement> {
return new Promise((resolve, reject) => {
const img = new Image()
const { src, srcset, sizes, class: clazz, loading, crossorigin } = options
const { src, srcset, sizes, class: clazz, loading, crossorigin, referrerPolicy } = options

img.src = src

Expand All @@ -43,6 +45,9 @@ async function loadImage(options: UseImageOptions): Promise<HTMLImageElement> {
if (crossorigin)
img.crossOrigin = crossorigin

if (referrerPolicy)
img.referrerPolicy = referrerPolicy

img.onload = () => resolve(img)
img.onerror = reject
})
Expand Down

0 comments on commit 04d32d8

Please sign in to comment.