1. /**
  2. * Invalidates the cache for binary files in a staging environment.
  3. *
  4. * This function identifies binary files and clears their cache,
  5. * ensuring that the staging environment receives the latest versions.
  6. */
  7. async function invalidateStagingCache() {
  8. // Define the staging environment (e.g., a specific environment variable or configuration)
  9. const isStaging = process.env.NODE_ENV === 'staging';
  10. if (!isStaging) {
  11. console.log('Not in staging environment. Cache invalidation skipped.');
  12. return;
  13. }
  14. // Function to identify binary files (customize this based on your file types)
  15. function isBinaryFile(filepath) {
  16. const mimeTypes = ['image/jpeg', 'image/png', 'image/gif', 'application/pdf', 'application/zip', 'text/csv']; //Example binary file types
  17. const ext = filepath.split('.').pop().toLowerCase(); //get file extension
  18. return mimeTypes.includes(`application/${ext}`) || mimeTypes.includes(`image/${ext}`);
  19. }
  20. // Function to get a list of all files in a directory (or specify a specific path)
  21. async function getFiles(dirPath) {
  22. try {
  23. const fs = require('fs').promises; //Use promise based fs for async operations
  24. const files = await fs.readdir(dirPath); //Read directory contents
  25. return files;
  26. } catch (error) {
  27. console.error('Error reading directory:', error);
  28. return [];
  29. }
  30. }
  31. // Get the root directory containing the binary files
  32. const binaryFilesDir = './public/assets'; // Replace with your binary files directory
  33. // Get the list of files in the directory
  34. const allFiles = await getFiles(binaryFilesDir);
  35. // Filter out non-binary files
  36. const binaryFiles = allFiles.filter(file => isBinaryFile(binaryFilesDir + '/' + file));
  37. // Invalidate the cache for each binary file
  38. if (binaryFiles.length > 0) {
  39. console.log(`Found ${binaryFiles.length} binary files to invalidate in staging environment.`);
  40. binaryFiles.forEach(file => {
  41. const filePath = binaryFilesDir + '/' + file;
  42. console.log(`Invalidating cache for: ${filePath}`);
  43. // Implement your cache invalidation logic here.
  44. // This could involve:
  45. // 1. Sending a signal to a cache service (e.g., Redis, Memcached)
  46. // 2. Deleting files from a cache directory
  47. // 3. Using a specific API endpoint to invalidate the cache.
  48. //Example: (replace with your cache invalidation method)
  49. //fetch(`/api/invalidate-cache?file=${filePath}`)
  50. // .then(response => response.json())
  51. // .then(data => {
  52. // if (data.success) {
  53. // console.log(`Cache invalidated successfully for ${filePath}`);
  54. // } else {
  55. // console.error(`Failed to invalidate cache for ${filePath}`);
  56. // }
  57. // })
  58. // .catch(error => {
  59. // console.error(`Error invalidating cache for ${filePath}:`, error);
  60. // });
  61. });
  62. console.log('Cache invalidation complete.');
  63. } else {
  64. console.log('No binary files found in the staging environment directory.');
  65. }
  66. }
  67. // Run the cache invalidation function if it's the main module.
  68. if (require.main === module) {
  69. invalidateStagingCache();
  70. }

Add your comment