Fix node asset cache busting
This commit is contained in:
@@ -65,6 +65,12 @@ export async function PATCH(req: NextRequest) {
|
|||||||
if (data.faviconData !== undefined) {
|
if (data.faviconData !== undefined) {
|
||||||
updateData.faviconData = data.faviconData;
|
updateData.faviconData = data.faviconData;
|
||||||
}
|
}
|
||||||
|
if (data.logoUrl === '') {
|
||||||
|
updateData.logoData = null;
|
||||||
|
}
|
||||||
|
if (data.faviconUrl === '') {
|
||||||
|
updateData.faviconData = null;
|
||||||
|
}
|
||||||
|
|
||||||
[node] = await db.update(nodes)
|
[node] = await db.update(nodes)
|
||||||
.set(updateData)
|
.set(updateData)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from 'next/server';
|
|||||||
import { db, nodes } from '@/db';
|
import { db, nodes } from '@/db';
|
||||||
import { eq } from 'drizzle-orm';
|
import { eq } from 'drizzle-orm';
|
||||||
import { requireAdmin } from '@/lib/auth/admin';
|
import { requireAdmin } from '@/lib/auth/admin';
|
||||||
|
import { getVersionedNodeAssetUrl } from '@/lib/node/assets';
|
||||||
|
|
||||||
// Logo constraints
|
// Logo constraints
|
||||||
const MAX_LOGO_SIZE = 2 * 1024 * 1024; // 2MB
|
const MAX_LOGO_SIZE = 2 * 1024 * 1024; // 2MB
|
||||||
@@ -112,9 +113,14 @@ export async function POST(req: NextRequest) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update the appropriate field
|
// Update the appropriate field
|
||||||
const updateData = isLogo
|
const updatedAt = new Date();
|
||||||
? { logoData: dataUrl, logoUrl: `/api/node/logo`, updatedAt: new Date() }
|
const assetUrl = isLogo
|
||||||
: { faviconData: dataUrl, faviconUrl: `/api/node/favicon`, updatedAt: new Date() };
|
? getVersionedNodeAssetUrl('/api/node/logo', updatedAt)
|
||||||
|
: getVersionedNodeAssetUrl('/api/node/favicon', updatedAt);
|
||||||
|
|
||||||
|
const updateData = isLogo
|
||||||
|
? { logoData: dataUrl, logoUrl: assetUrl, updatedAt }
|
||||||
|
: { faviconData: dataUrl, faviconUrl: assetUrl, updatedAt };
|
||||||
|
|
||||||
await db.update(nodes)
|
await db.update(nodes)
|
||||||
.set(updateData)
|
.set(updateData)
|
||||||
@@ -122,7 +128,7 @@ export async function POST(req: NextRequest) {
|
|||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
url: isLogo ? '/api/node/logo' : '/api/node/favicon',
|
url: assetUrl,
|
||||||
type,
|
type,
|
||||||
size: file.size,
|
size: file.size,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -48,7 +48,9 @@ export async function GET() {
|
|||||||
status: 200,
|
status: 200,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': mimeType,
|
'Content-Type': mimeType,
|
||||||
'Cache-Control': 'public, max-age=3600, stale-while-revalidate=86400',
|
'Cache-Control': 'no-store, max-age=0',
|
||||||
|
Pragma: 'no-cache',
|
||||||
|
Expires: '0',
|
||||||
'Content-Length': buffer.length.toString(),
|
'Content-Length': buffer.length.toString(),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -48,7 +48,9 @@ export async function GET() {
|
|||||||
status: 200,
|
status: 200,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': mimeType,
|
'Content-Type': mimeType,
|
||||||
'Cache-Control': 'public, max-age=3600, stale-while-revalidate=86400',
|
'Cache-Control': 'no-store, max-age=0',
|
||||||
|
Pragma: 'no-cache',
|
||||||
|
Expires: '0',
|
||||||
'Content-Length': buffer.length.toString(),
|
'Content-Length': buffer.length.toString(),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { db } from '@/db';
|
|||||||
import { nodes, users } from '@/db';
|
import { nodes, users } from '@/db';
|
||||||
import { eq, inArray } from 'drizzle-orm';
|
import { eq, inArray } from 'drizzle-orm';
|
||||||
import { getNodePublicKey } from '@/lib/swarm/node-keys';
|
import { getNodePublicKey } from '@/lib/swarm/node-keys';
|
||||||
|
import { getVersionedNodeAssetUrl } from '@/lib/node/assets';
|
||||||
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
try {
|
try {
|
||||||
@@ -58,8 +59,17 @@ export async function GET() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const logoUrl = node.logoData
|
||||||
|
? getVersionedNodeAssetUrl('/api/node/logo', node.updatedAt)
|
||||||
|
: node.logoUrl;
|
||||||
|
const faviconUrl = node.faviconData
|
||||||
|
? getVersionedNodeAssetUrl('/api/node/favicon', node.updatedAt)
|
||||||
|
: node.faviconUrl;
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
...node,
|
...node,
|
||||||
|
logoUrl,
|
||||||
|
faviconUrl,
|
||||||
publicKey, // Always include the public key
|
publicKey, // Always include the public key
|
||||||
admins,
|
admins,
|
||||||
// Don't expose the secret keys
|
// Don't expose the secret keys
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
export function getNodeAssetVersion(updatedAt: Date | string | null | undefined): string {
|
||||||
|
if (!updatedAt) {
|
||||||
|
return Date.now().toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
const timestamp = new Date(updatedAt).getTime();
|
||||||
|
return Number.isNaN(timestamp) ? Date.now().toString() : timestamp.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getVersionedNodeAssetUrl(path: string, updatedAt: Date | string | null | undefined): string {
|
||||||
|
const version = getNodeAssetVersion(updatedAt);
|
||||||
|
return `${path}?v=${encodeURIComponent(version)}`;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user