The uploadService.ts has been updated to support Cache-Control headers for optimal file caching and performance. Cache-Control is now enabled by default for all uploads.
interface PresignedUrlResponse {
uploadUrl: string;
key: string;
feedId: string;
publicUrl: string;
maxSizeBytes: number;
maxSizeMB: number;
cacheControl?: boolean | string; // NEW: Added cacheControl field
}- Added
cacheControlparameter with default value oftrue - Sends
cacheControlto the backend API - Logs the
cacheControlvalue in the response
export const getPresignedUrl = async (
fileName: string,
fileType: string,
authToken: string,
cacheControl: boolean | string = true // Defaults to true
): Promise<PresignedUrlResponse>- Added
cacheControlparameter - CRITICAL: Sets the
Cache-Controlheader when uploading to S3/Spaces - Uses default value
'public, max-age=31536000, immutable'whencacheControl === true - Supports custom Cache-Control strings
export const directUpload = async (
file: File,
presignedUrl: string,
cacheControl?: boolean | string, // NEW parameter
onProgress?: (progress: number) => void
): Promise<XMLHttpRequest>Implementation Detail:
if (cacheControl) {
const cacheControlValue = typeof cacheControl === 'string'
? cacheControl
: 'public, max-age=31536000, immutable';
xhr.setRequestHeader('Cache-Control', cacheControlValue);
console.log('Setting Cache-Control header:', cacheControlValue);
}- Added
cacheControlparameter with default value oftrue - Passes
cacheControltogetPresignedUrl - Passes the response
cacheControltodirectUpload
export const processFileUpload = async (
file: File,
authToken: string,
cacheControl: boolean | string = true // Defaults to true
)By default, all uploads now use Cache-Control with the following header:
Cache-Control: public, max-age=31536000, immutable
This means:
- ✅ Files are cached for 1 year (31,536,000 seconds)
- ✅ Files are marked as
immutable(won't change at this URL) - ✅ Files are cached by browsers and CDNs
- ✅ Significantly improves performance for repeated access
// Cache-Control is automatically enabled
await UploadService.processFileUpload(file, authToken);// Explicitly disable Cache-Control
await UploadService.processFileUpload(file, authToken, false);// Use a custom Cache-Control header
await UploadService.processFileUpload(
file,
authToken,
'public, max-age=86400' // 1 day cache
);- Performance: Browsers cache files for 1 year, reducing repeated downloads
- Bandwidth: Saves bandwidth for both client and server
- CDN Efficiency: CloudFront/CDN can cache files aggressively
- User Experience: Faster subsequent page loads
- Immutable Flag: Tells browsers the file will never change at this URL
Consider disabling Cache-Control (cacheControl: false) for:
- Files that might be updated/replaced at the same URL
- Temporary uploads
- Work-in-progress content
- Files where immediate updates must be visible
For published/final content (videos, images, audio), the default behavior is optimal.
The backend API must:
- Accept the
cacheControlparameter in the POST request - Include the appropriate
Cache-Controlheader when generating the presigned URL - Return the
cacheControlvalue in the response
After uploading a file with Cache-Control enabled, verify the headers:
curl -I https://your-bucket.endpoint.com/path/to/file.mp4
# Should see:
# Cache-Control: public, max-age=31536000, immutable- This is backward compatible - existing code continues to work
- The default provides optimal caching for most use cases
- Files with long-term caching should use unique filenames for updates
- The implementation follows AWS S3 and DigitalOcean Spaces best practices