Skip to content

Commit b31a721

Browse files
committed
Test blob commit
1 parent 4f4bd1e commit b31a721

File tree

4 files changed

+168
-37
lines changed

4 files changed

+168
-37
lines changed

next-js-app/package-lock.json

Lines changed: 114 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

next-js-app/package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,22 @@
1010
"export": "next export"
1111
},
1212
"dependencies": {
13-
"react": "19.1.0",
14-
"react-dom": "19.1.0",
13+
"@google/generative-ai": "^0.14.1",
14+
"@vercel/blob": "^1.1.1",
15+
"@vercel/kv": "^3.0.0",
1516
"next": "15.4.6",
16-
"@google/generative-ai": "^0.14.1"
17+
"react": "19.1.0",
18+
"react-dom": "19.1.0"
1719
},
1820
"devDependencies": {
19-
"typescript": "^5",
21+
"@eslint/eslintrc": "^3",
22+
"@tailwindcss/postcss": "^4",
2023
"@types/node": "^20",
2124
"@types/react": "^19",
2225
"@types/react-dom": "^19",
23-
"@tailwindcss/postcss": "^4",
24-
"tailwindcss": "^4",
2526
"eslint": "^9",
2627
"eslint-config-next": "15.4.6",
27-
"@eslint/eslintrc": "^3"
28+
"tailwindcss": "^4",
29+
"typescript": "^5"
2830
}
2931
}

next-js-app/src/app/api/contact-submissions/route.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
import { NextResponse } from "next/server";
2-
import fs from "fs/promises";
3-
import path from "path";
4-
5-
const submissionsDir = path.resolve(process.cwd(), "contact-submissions");
6-
const submissionsFile = path.resolve(submissionsDir, "submissions.json");
2+
import { list } from "@vercel/blob";
73

84
async function getSubmissions() {
95
try {
10-
const data = await fs.readFile(submissionsFile, "utf8");
11-
return JSON.parse(data);
6+
const { blobs } = await list({ prefix: "submissions.json" });
7+
if (blobs.length === 0) {
8+
return [];
9+
}
10+
const submissionsBlob = blobs[0];
11+
const response = await fetch(submissionsBlob.url);
12+
if (!response.ok) {
13+
if (response.status === 404) {
14+
return [];
15+
}
16+
throw new Error(`Failed to fetch submissions: ${response.statusText}`);
17+
}
18+
const submissions = await response.json();
19+
return submissions;
1220
} catch (error) {
21+
console.error("Error fetching submissions from blob:", error);
1322
return [];
1423
}
1524
}

next-js-app/src/app/api/contact/route.ts

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
import { NextRequest, NextResponse } from "next/server";
2-
import fs from "fs/promises";
3-
import path from "path";
4-
5-
const submissionsDir = path.resolve(process.cwd(), "contact-submissions");
6-
const submissionsFile = path.resolve(submissionsDir, "submissions.json");
7-
8-
async function ensureSubmissionsDirExists() {
9-
try {
10-
await fs.mkdir(submissionsDir, { recursive: true });
11-
} catch (error) {
12-
console.error("Error creating submissions directory:", error);
13-
}
14-
}
2+
import { put, list } from "@vercel/blob";
153

164
interface Submission {
175
name: string;
@@ -22,18 +10,34 @@ interface Submission {
2210

2311
async function getSubmissions(): Promise<Submission[]> {
2412
try {
25-
await ensureSubmissionsDirExists();
26-
const data = await fs.readFile(submissionsFile, "utf8");
27-
return JSON.parse(data);
28-
} catch {
13+
const { blobs } = await list({ prefix: "submissions.json" });
14+
if (blobs.length === 0) {
15+
return [];
16+
}
17+
const submissionsBlob = blobs[0];
18+
const response = await fetch(submissionsBlob.url);
19+
if (!response.ok) {
20+
// If the blob is not found or there's an error, start fresh.
21+
if (response.status === 404) {
22+
return [];
23+
}
24+
throw new Error(`Failed to fetch submissions: ${response.statusText}`);
25+
}
26+
const submissions = await response.json();
27+
return submissions;
28+
} catch (error) {
29+
console.error("Error fetching submissions from blob:", error);
30+
// In case of any error, assume no submissions exist to avoid data loss.
2931
return [];
3032
}
3133
}
3234

33-
async function saveSubmission(data: Submission) {
34-
const submissions = await getSubmissions();
35-
submissions.push(data);
36-
await fs.writeFile(submissionsFile, JSON.stringify(submissions, null, 2));
35+
async function saveSubmissions(data: Submission[]) {
36+
await put("submissions.json", JSON.stringify(data, null, 2), {
37+
access: "private",
38+
addRandomSuffix: false, // Overwrite the existing blob
39+
cacheControlMaxAge: 0, // Ensure fresh data is always read
40+
});
3741
}
3842

3943
export async function POST(req: NextRequest) {
@@ -47,14 +51,16 @@ export async function POST(req: NextRequest) {
4751
);
4852
}
4953

50-
const newSubmission = {
54+
const newSubmission: Submission = {
5155
name,
5256
email,
5357
message,
5458
date: new Date().toISOString(),
5559
};
5660

57-
await saveSubmission(newSubmission);
61+
const submissions = await getSubmissions();
62+
submissions.push(newSubmission);
63+
await saveSubmissions(submissions);
5864

5965
return NextResponse.json({ message: "Submission saved" }, { status: 200 });
6066
} catch (error) {

0 commit comments

Comments
 (0)