Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Revisions to skip in `git blame` (mechanical, no-logic-change commits).
# Enable locally with: git config blame.ignoreRevsFile .git-blame-ignore-revs
# GitHub honours this file automatically.

# style: format codebase with prettier
49f28e900a6a6c1bdf8893d699e9ee1cd574c787
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ jobs:
- name: Install dependencies
run: npm ci

- name: Format check
run: npm run format:check

- name: Lint
run: npm run lint

Expand Down
15 changes: 15 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Build output / deps
dist/
**/dist/
node_modules/
**/node_modules/
coverage/

# Cloud-sync conflict copies (also gitignored / ignored by eslint)
**/* 2.ts
* 2.*

# Generated / data files — not source, formatting adds churn without value
*.jsonl
package-lock.json
.kit/
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"semi": true,
"singleQuote": false,
"tabWidth": 2,
"trailingComma": "all",
"printWidth": 100,
"arrowParens": "always"
}
144 changes: 72 additions & 72 deletions examples/cdn-cache-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* to optimize CloudFront caching behavior.
*/

import { NextRequest, NextResponse } from 'next/server';
import { NextRequest, NextResponse } from "next/server";

// ─── Public API Endpoints (Cacheable) ───────────────────────────────────

Expand All @@ -14,9 +14,9 @@ export async function getPublicPlugins(req: NextRequest) {

return NextResponse.json(data, {
headers: {
'Cache-Control': 'public, max-age=300, s-maxage=600, stale-while-revalidate=3600',
'Content-Type': 'application/json',
'ETag': generateETag(data),
"Cache-Control": "public, max-age=300, s-maxage=600, stale-while-revalidate=3600",
"Content-Type": "application/json",
ETag: generateETag(data),
},
});
}
Expand All @@ -27,24 +27,24 @@ export async function getPluginDetails(req: NextRequest, { slug }: { slug: strin
return NextResponse.json(plugin, {
headers: {
// Cache for 1 hour, allow stale content for up to 1 day
'Cache-Control': 'public, max-age=3600, s-maxage=3600, stale-while-revalidate=86400',
'Content-Type': 'application/json',
'ETag': generateETag(plugin),
'Last-Modified': new Date(plugin.updatedAt).toUTCString(),
"Cache-Control": "public, max-age=3600, s-maxage=3600, stale-while-revalidate=86400",
"Content-Type": "application/json",
ETag: generateETag(plugin),
"Last-Modified": new Date(plugin.updatedAt).toUTCString(),
},
});
}

export async function searchPlugins(req: NextRequest) {
const query = req.nextUrl.searchParams.get('q');
const query = req.nextUrl.searchParams.get("q");
const results = await searchPluginsByQuery(query!);

return NextResponse.json(results, {
headers: {
// Search results cached, but include query string in cache key
'Cache-Control': 'public, max-age=300, s-maxage=600, stale-while-revalidate=3600',
'Content-Type': 'application/json',
'Vary': 'Accept-Encoding, Accept-Language',
"Cache-Control": "public, max-age=300, s-maxage=600, stale-while-revalidate=3600",
"Content-Type": "application/json",
Vary: "Accept-Encoding, Accept-Language",
},
});
}
Expand All @@ -55,15 +55,15 @@ export async function getUserProfile(req: NextRequest) {
const user = await getCurrentUser(req);

if (!user) {
return new NextResponse('Unauthorized', { status: 401 });
return new NextResponse("Unauthorized", { status: 401 });
}

return NextResponse.json(user, {
headers: {
// Cache user data briefly (1 minute)
'Cache-Control': 'private, max-age=60, s-maxage=300',
'Content-Type': 'application/json',
'Vary': 'Authorization, Cookie',
"Cache-Control": "private, max-age=60, s-maxage=300",
"Content-Type": "application/json",
Vary: "Authorization, Cookie",
},
});
}
Expand All @@ -72,17 +72,17 @@ export async function getUserPlugins(req: NextRequest) {
const user = await getCurrentUser(req);

if (!user) {
return new NextResponse('Unauthorized', { status: 401 });
return new NextResponse("Unauthorized", { status: 401 });
}

const plugins = await fetchUserPlugins(user.id);

return NextResponse.json(plugins, {
headers: {
// User's plugins cached briefly with authorization context
'Cache-Control': 'private, max-age=60, s-maxage=300',
'Content-Type': 'application/json',
'Vary': 'Authorization, Cookie, X-User-ID',
"Cache-Control": "private, max-age=60, s-maxage=300",
"Content-Type": "application/json",
Vary: "Authorization, Cookie, X-User-ID",
},
});
}
Expand All @@ -91,17 +91,17 @@ export async function getUserSettings(req: NextRequest) {
const user = await getCurrentUser(req);

if (!user) {
return new NextResponse('Unauthorized', { status: 401 });
return new NextResponse("Unauthorized", { status: 401 });
}

const settings = await fetchUserSettings(user.id);

return NextResponse.json(settings, {
headers: {
// Settings cached very briefly
'Cache-Control': 'private, max-age=30, s-maxage=60',
'Content-Type': 'application/json',
'Vary': 'Authorization, Cookie',
"Cache-Control": "private, max-age=30, s-maxage=60",
"Content-Type": "application/json",
Vary: "Authorization, Cookie",
},
});
}
Expand All @@ -112,7 +112,7 @@ export async function createPlugin(req: NextRequest) {
const user = await getCurrentUser(req);

if (!user) {
return new NextResponse('Unauthorized', { status: 401 });
return new NextResponse("Unauthorized", { status: 401 });
}

const body = await req.json();
Expand All @@ -122,22 +122,19 @@ export async function createPlugin(req: NextRequest) {
status: 201,
headers: {
// Never cache write operations
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0',
'Content-Type': 'application/json',
"Cache-Control": "no-cache, no-store, must-revalidate",
Pragma: "no-cache",
Expires: "0",
"Content-Type": "application/json",
},
});
}

export async function updatePluginSettings(
req: NextRequest,
{ id }: { id: string }
) {
export async function updatePluginSettings(req: NextRequest, { id }: { id: string }) {
const user = await getCurrentUser(req);

if (!user) {
return new NextResponse('Unauthorized', { status: 401 });
return new NextResponse("Unauthorized", { status: 401 });
}

const body = await req.json();
Expand All @@ -146,10 +143,10 @@ export async function updatePluginSettings(
return NextResponse.json(plugin, {
headers: {
// Never cache write operations
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0',
'Content-Type': 'application/json',
"Cache-Control": "no-cache, no-store, must-revalidate",
Pragma: "no-cache",
Expires: "0",
"Content-Type": "application/json",
},
});
}
Expand All @@ -158,7 +155,7 @@ export async function deletePlugin(req: NextRequest, { id }: { id: string }) {
const user = await getCurrentUser(req);

if (!user) {
return new NextResponse('Unauthorized', { status: 401 });
return new NextResponse("Unauthorized", { status: 401 });
}

await deletePluginById(id);
Expand All @@ -167,9 +164,9 @@ export async function deletePlugin(req: NextRequest, { id }: { id: string }) {
status: 204,
headers: {
// Never cache delete operations
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0',
"Cache-Control": "no-cache, no-store, must-revalidate",
Pragma: "no-cache",
Expires: "0",
},
});
}
Expand All @@ -180,28 +177,31 @@ export async function login(req: NextRequest) {
const body = await req.json();
const { token, user } = await authenticateUser(body.email, body.password);

return NextResponse.json({ token, user }, {
status: 200,
headers: {
// Never cache auth responses
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0',
'Set-Cookie': `authToken=${token}; Path=/; HttpOnly; Secure; SameSite=Strict`,
'Content-Type': 'application/json',
return NextResponse.json(
{ token, user },
{
status: 200,
headers: {
// Never cache auth responses
"Cache-Control": "no-cache, no-store, must-revalidate",
Pragma: "no-cache",
Expires: "0",
"Set-Cookie": `authToken=${token}; Path=/; HttpOnly; Secure; SameSite=Strict`,
"Content-Type": "application/json",
},
},
});
);
}

export async function logout(req: NextRequest) {
return new NextResponse(null, {
status: 200,
headers: {
// Never cache logout
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0',
'Set-Cookie': 'authToken=; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=0',
"Cache-Control": "no-cache, no-store, must-revalidate",
Pragma: "no-cache",
Expires: "0",
"Set-Cookie": "authToken=; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=0",
},
});
}
Expand All @@ -210,34 +210,34 @@ export async function refreshToken(req: NextRequest) {
const user = await getCurrentUser(req);

if (!user) {
return new NextResponse('Unauthorized', { status: 401 });
return new NextResponse("Unauthorized", { status: 401 });
}

const newToken = await generateNewToken(user.id);

return NextResponse.json({ token: newToken }, {
headers: {
// Never cache token refresh
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0',
'Content-Type': 'application/json',
return NextResponse.json(
{ token: newToken },
{
headers: {
// Never cache token refresh
"Cache-Control": "no-cache, no-store, must-revalidate",
Pragma: "no-cache",
Expires: "0",
"Content-Type": "application/json",
},
},
});
);
}

// ─── Utility Functions ────────────────────────────────────────────────

function generateETag(data: any): string {
const hash = require('crypto')
.createHash('md5')
.update(JSON.stringify(data))
.digest('hex');
const hash = require("crypto").createHash("md5").update(JSON.stringify(data)).digest("hex");
return `"${hash}"`;
}

async function getCurrentUser(req: NextRequest) {
const token = req.headers.get('authorization')?.split(' ')[1];
const token = req.headers.get("authorization")?.split(" ")[1];

if (!token) {
return null;
Expand Down Expand Up @@ -280,11 +280,11 @@ async function deletePluginById(id: string) {
}

async function authenticateUser(email: string, password: string) {
return { token: '', user: {} };
return { token: "", user: {} };
}

async function generateNewToken(userId: string) {
return '';
return "";
}

async function verifyToken(token: string) {
Expand Down
Loading
Loading