Chore: add build time to env and update API fetch to prevent caching with timestamp query parameter

This commit is contained in:
Bram Suurd
2024-11-05 16:07:06 +01:00
parent e7d8ffb574
commit e5bb89bee7
3 changed files with 12 additions and 17 deletions

View File

@@ -14,6 +14,10 @@ const nextConfig = {
],
},
env: {
NEXT_PUBLIC_BUILD_TIME: `${Date.now()}`,
},
output: "export",
// basePath: "/proxmox-helper-scripts",
};

View File

@@ -47,7 +47,9 @@ function ScriptContent() {
};
useEffect(() => {
fetch("api/categories")
fetch(
`api/categories?_=${process.env.NEXT_PUBLIC_BUILD_TIME || Date.now()}`,
)
.then((response) => response.json())
.then((categories) => {
const sortedCategories = sortCategories(categories);

View File

@@ -50,24 +50,13 @@ export default function CommandMenu() {
}, []);
React.useEffect(() => {
const fetchCategories = async (): Promise<void> => {
try {
const response = await fetch("api/categories");
if (!response.ok) {
throw new Error("Failed to fetch categories");
}
const categories: Category[] = await response.json();
if (categories.length === 0) {
throw new Error("Empty response");
}
fetch(`api/categories?_=${process.env.NEXT_PUBLIC_BUILD_TIME || Date.now()}`)
.then((response) => response.json())
.then((categories) => {
const sortedCategories = sortCategories(categories);
setLinks(sortedCategories);
} catch (error) {
console.error(error);
}
};
fetchCategories();
})
.catch((error) => console.error(error));
}, []);
return (