UploadFile
Uploads a file to Supabase Storage with validation and optional image compression. Source:src/App.jsx:121
Parameters
File
required
The File object to upload. Must be obtained from an HTML file input element.
string
default:"file"
The type of file being uploaded. Can be:
"file"- General file upload (documents, PDFs, etc.)"image"- Image file (triggers compression)
File Size Validation
The function enforces a maximum file size of 10MB (10 * 1024 * 1024 bytes). Files exceeding this limit are rejected with an error message.Storage Path
Files are uploaded to Supabase Storage with the following path structure:Parameters
File
required
The image File object to compress.
number
Maximum file size in megabytes after compression. Default is 0.2 MB (200 KB).
number
default:1920
Maximum width or height in pixels. The image will be scaled down proportionally if it exceeds this dimension.
boolean
default:true
Whether to use a Web Worker for compression to avoid blocking the main thread. Recommended to keep as
true for better performance.Compression Options
The function uses thebrowser-image-compression library with the following defaults:
- Target size: 200 KB maximum
- Max dimensions: 1920px (width or height)
- Web Worker: Enabled for non-blocking compression
Return Value
ReturnsPromise<File> - A new File object containing the compressed image with the original filename and MIME type preserved.
Error Handling
Throws anError if compression fails. The error message from the underlying compression library is preserved.
Performance Considerations
- Uses Web Workers by default to prevent UI blocking during compression
- Compression time varies based on original image size and quality
- Larger images may take several seconds to compress