Overview
The getMetadata utility function fetches metadata from a given URL using a Supabase Edge Function. It extracts information like title, description, images, and domain from web pages.
Function Signature
const getMetadata = async ( url : string ) : Promise < Metadata | null >
Parameters
The URL to extract metadata from
Return Value
Returns a Metadata object on success, or null if the request fails The page title extracted from the URL
The page description or meta description
Array of image URLs found on the page (typically Open Graph images)
Time taken to fetch the metadata
The domain name of the URL
The original URL that was processed
Usage Example
import getMetadata from "../utils/getMetadata"
import { showErrorToast , showSuccessToast } from "../utils/showToast"
const refreshMetadata = async ( url : string ) => {
const toastId = showLoadingToast ( "Searching for new metadata..." )
const newMetadata = await getMetadata ( url )
if ( ! newMetadata ) {
return showErrorToast ( "Failed to retrieve new metadata." , { id: toastId })
}
const { title , domain , description , images } = newMetadata
// Update bookmark with new metadata
const response = await updateBookmark ( bookmark . id , {
... bookmark ,
title: title || domain ,
description ,
image: images [ 0 ]
})
if ( ! response . success ) {
return showErrorToast ( response . data , { id: toastId })
}
showSuccessToast ( "Bookmark refreshed successfully!" , { id: toastId })
}
Implementation Details
The function:
Sends a POST request to a Supabase Edge Function endpoint
Includes authorization headers with an anonymous key
Returns null if the request fails or if an error occurs
Does not throw exceptions - errors are handled gracefully
Source Location
~/workspace/source/src/utils/getMetadata.ts:4