now can load local detail news
This commit is contained in:
parent
14294f8a14
commit
deb82d38b8
3 changed files with 23 additions and 6 deletions
|
@ -1,9 +1,14 @@
|
|||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
export async function fetchNewsData(id: string) {
|
||||
const response = await fetch(
|
||||
`https://raw.githubusercontent.com/newspedia-crew/newspedia-web/intern-change/public/data/${id}.json`,
|
||||
);
|
||||
if (!response.ok) {
|
||||
try {
|
||||
const dataDirectory = path.join(process.cwd(), "public", "data");
|
||||
const filePath = path.join(dataDirectory, `${id}.json`);
|
||||
const fileContents = await fs.readFile(filePath, "utf8");
|
||||
return JSON.parse(fileContents);
|
||||
} catch (error) {
|
||||
console.error("Error reading news data:", error);
|
||||
return null;
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue