-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoteOptimizedImages.js
More file actions
131 lines (108 loc) · 3.67 KB
/
Copy pathremoteOptimizedImages.js
File metadata and controls
131 lines (108 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* Remote Optimized Images Configuration
*
* This file exports an array of remote image URLs that will be optimized
* during the build process by next-image-export-optimizer.
*
* The optimizer will download these images, optimize them, and replace
* the URLs in the exported static site.
*/
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:9000";
/**
* Fetches all projects from the API and extracts image URLs
*/
async function fetchProjectImages() {
try {
const response = await fetch(`${API_BASE_URL}/api/projects`, {
cache: "no-store", // Always fetch fresh data at build time
});
if (!response.ok) {
console.warn(`Failed to fetch projects: ${response.statusText}`);
return [];
}
const result = await response.json();
if (!result.success || !result.data) {
console.warn("No project data found");
return [];
}
const projects = result.data;
const imageUrls = new Set();
projects.forEach((project) => {
// Add thumbnail if it exists and is a remote URL
if (project.thumbnail && (project.thumbnail.startsWith("http://") || project.thumbnail.startsWith("https://"))) {
imageUrls.add(project.thumbnail);
}
// Add all images from the images array
if (Array.isArray(project.images)) {
project.images.forEach((image) => {
if (image && (image.startsWith("http://") || image.startsWith("https://"))) {
imageUrls.add(image);
}
});
}
// Add tech stack icon URLs
if (Array.isArray(project.tech_stacks)) {
project.tech_stacks.forEach((techStack) => {
if (techStack.icon_url && (techStack.icon_url.startsWith("http://") || techStack.icon_url.startsWith("https://"))) {
imageUrls.add(techStack.icon_url);
}
});
}
});
return Array.from(imageUrls);
} catch (error) {
console.error("Error fetching project images:", error);
return [];
}
}
/**
* Fetches all blogs from the API and extracts image URLs
*/
async function fetchBlogImages() {
try {
const response = await fetch(`${API_BASE_URL}/api/blogs`, {
cache: "no-store", // Always fetch fresh data at build time
});
if (!response.ok) {
console.warn(`Failed to fetch blogs: ${response.statusText}`);
return [];
}
const result = await response.json();
if (!result.success || !result.data) {
console.warn("No blog data found");
return [];
}
const blogs = result.data;
const imageUrls = new Set();
blogs.forEach((blog) => {
// Add thumbnail if it exists and is a remote URL
if (blog.thumbnail && (blog.thumbnail.startsWith("http://") || blog.thumbnail.startsWith("https://"))) {
imageUrls.add(blog.thumbnail);
}
});
return Array.from(imageUrls);
} catch (error) {
console.error("Error fetching blog images:", error);
return [];
}
}
/**
* Main function that fetches all remote image URLs
* Returns a Promise that resolves to an array of image URLs
*/
module.exports = new Promise(async (resolve) => {
try {
console.log("Fetching remote image URLs for optimization...");
const [projectImages, blogImages] = await Promise.all([
fetchProjectImages(),
fetchBlogImages(),
]);
const allImages = [...projectImages, ...blogImages];
const uniqueImages = Array.from(new Set(allImages));
console.log(`Found ${uniqueImages.length} unique remote image(s) to optimize`);
resolve(uniqueImages);
} catch (error) {
console.error("Error fetching remote images:", error);
resolve([]); // Return empty array on error to prevent build failure
}
});