perf: implement Phase 1 homepage diet via Next.js Cache Components
P1-0: V8 heap cap (HOMARR_MAX_OLD_SPACE_SIZE, default 512MB)
P1-1: Module graph cuts
- Dynamic integration imports (55 static → lazy loaders)
- Conditional DB driver loading (only configured driver loaded)
- Replace jsdom with linkedom for SVG sanitization
- Delete eager widget barrel (55 import * as → 0)
- Extract createRetryableLoader to @homarr/common
P1-2: Enable cacheComponents + restructure root layout
- Suspense boundaries around session-dependent providers
- ColorSchemeScript replaces server-side cookie reads
- Board/manage/auth layouts wrapped in Suspense
P1-3: Cache read paths
- Cache tag registry + invalidation helpers
- Cache invalidation wired into all mutation routers
- Translation deep-merge memoized per locale
P1-4: Prerendered shells + loading.tsx files
P1-5: Shared integration data cache (60s TTL, stale-while-revalidate)
P1-6: Redis-backed Next.js cache handler (@homarr/next-cache-handler)
P1-7: Redis optional (memory channel fallback), TTL on pubSub:last:*,
Redis clients consolidated from 6 to 2
This commit is contained in:
86 files changed
+2219
-1232
No files matched your search
@@ -0,0 +1,27 @@
|
||||
import type { CacheHandler } from "next/dist/server/lib/cache-handlers/types";
|
||||
|
||||
let handler: CacheHandler;
|
||||
|
||||
const getHandler = async (): Promise<CacheHandler> => {
|
||||
if (handler) return handler;
|
||||
|
||||
const useRedis = process.env.REDIS_HOST || process.env.REDIS_IS_EXTERNAL === "true";
|
||||
if (useRedis) {
|
||||
const { RedisCacheHandler } = await import("./redis-handler");
|
||||
handler = new RedisCacheHandler();
|
||||
} else {
|
||||
const { createDefaultCacheHandler } = await import("next/dist/server/lib/cache-handlers/default");
|
||||
handler = createDefaultCacheHandler(32 * 1024 * 1024);
|
||||
}
|
||||
return handler;
|
||||
};
|
||||
|
||||
export default {
|
||||
get: async (...args: Parameters<CacheHandler["get"]>) => (await getHandler()).get(...args),
|
||||
set: async (...args: Parameters<CacheHandler["set"]>) => (await getHandler()).set(...args),
|
||||
refreshTags: async () => (await getHandler()).refreshTags(),
|
||||
getExpiration: async (...args: Parameters<CacheHandler["getExpiration"]>) =>
|
||||
(await getHandler()).getExpiration(...args),
|
||||
updateTags: async (...args: Parameters<CacheHandler["updateTags"]>) =>
|
||||
(await getHandler()).updateTags(...args),
|
||||
} satisfies CacheHandler;
|
||||
@@ -0,0 +1,184 @@
|
||||
import Redis from "ioredis";
|
||||
|
||||
import type { CacheHandler } from "next/dist/server/lib/cache-handlers/types";
|
||||
|
||||
type CacheEntry = Awaited<ReturnType<CacheHandler["get"]>> & {};
|
||||
|
||||
const BUILD_ID = process.env.__NEXT_BUILD_ID ?? "dev";
|
||||
|
||||
export class RedisCacheHandler implements CacheHandler {
|
||||
private redis: Redis;
|
||||
private pendingSets = new Map<string, Promise<CacheEntry>>();
|
||||
private tagCache = new Map<string, number>();
|
||||
private loggedError = false;
|
||||
|
||||
constructor() {
|
||||
this.redis = new Redis({
|
||||
host: process.env.REDIS_HOST ?? "localhost",
|
||||
port: Number(process.env.REDIS_PORT ?? 6379),
|
||||
password: process.env.REDIS_PASSWORD || undefined,
|
||||
db: Number(process.env.REDIS_CACHE_DB ?? 1),
|
||||
lazyConnect: true,
|
||||
retryStrategy: (times) => Math.min(times * 100, 3000),
|
||||
});
|
||||
this.redis.on("error", () => {
|
||||
if (!this.loggedError) {
|
||||
console.warn("[next-cache-handler] Redis connection error, falling back to memory");
|
||||
this.loggedError = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private entryKey(cacheKey: string) {
|
||||
return `nextCache:v1:${BUILD_ID}:${cacheKey}`;
|
||||
}
|
||||
|
||||
private tagKey(tag: string) {
|
||||
return `nextCache:tag:${tag}`;
|
||||
}
|
||||
|
||||
async get(cacheKey: string, softTags: string[]): Promise<CacheEntry | undefined> {
|
||||
const pending = this.pendingSets.get(cacheKey);
|
||||
if (pending) return pending;
|
||||
|
||||
try {
|
||||
await this.redis.connect().catch(() => {});
|
||||
const raw = await this.redis.getBuffer(this.entryKey(cacheKey));
|
||||
if (!raw) return undefined;
|
||||
|
||||
const parsed = JSON.parse(raw.toString()) as {
|
||||
tags: string[];
|
||||
stale: number;
|
||||
timestamp: number;
|
||||
expire: number;
|
||||
revalidate: number;
|
||||
body: number[];
|
||||
};
|
||||
|
||||
const allTags = [...parsed.tags, ...softTags];
|
||||
const expiration = await this.getExpiration(allTags);
|
||||
if (expiration !== 0 && expiration < parsed.timestamp) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const body = new Uint8Array(parsed.body);
|
||||
return {
|
||||
value: new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue(body);
|
||||
controller.close();
|
||||
},
|
||||
}),
|
||||
tags: parsed.tags,
|
||||
stale: parsed.stale,
|
||||
timestamp: parsed.timestamp,
|
||||
expire: parsed.expire,
|
||||
revalidate: parsed.revalidate,
|
||||
};
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
async set(cacheKey: string, pendingEntry: Promise<CacheEntry>): Promise<void> {
|
||||
this.pendingSets.set(cacheKey, pendingEntry);
|
||||
|
||||
try {
|
||||
const entry = await pendingEntry;
|
||||
const reader = entry.value.getReader();
|
||||
const chunks: Uint8Array[] = [];
|
||||
let done = false;
|
||||
while (!done) {
|
||||
const result = await reader.read();
|
||||
done = result.done;
|
||||
if (result.value) chunks.push(result.value);
|
||||
}
|
||||
|
||||
const totalLength = chunks.reduce((acc, c) => acc + c.length, 0);
|
||||
const body = new Uint8Array(totalLength);
|
||||
let offset = 0;
|
||||
for (const chunk of chunks) {
|
||||
body.set(chunk, offset);
|
||||
offset += chunk.length;
|
||||
}
|
||||
|
||||
const serialized = JSON.stringify({
|
||||
tags: entry.tags,
|
||||
stale: entry.stale,
|
||||
timestamp: entry.timestamp,
|
||||
expire: entry.expire,
|
||||
revalidate: entry.revalidate,
|
||||
body: Array.from(body),
|
||||
});
|
||||
|
||||
const ttlMs = entry.expire * 1000;
|
||||
await this.redis.connect().catch(() => {});
|
||||
if (ttlMs > 0) {
|
||||
await this.redis.set(this.entryKey(cacheKey), serialized, "PX", ttlMs);
|
||||
} else {
|
||||
await this.redis.set(this.entryKey(cacheKey), serialized);
|
||||
}
|
||||
} catch {
|
||||
// Fail open
|
||||
} finally {
|
||||
if (this.pendingSets.get(cacheKey) === pendingEntry) {
|
||||
this.pendingSets.delete(cacheKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async refreshTags(): Promise<void> {
|
||||
try {
|
||||
await this.redis.connect().catch(() => {});
|
||||
const keys = await this.redis.keys("nextCache:tag:*");
|
||||
if (keys.length === 0) {
|
||||
this.tagCache.clear();
|
||||
return;
|
||||
}
|
||||
const values = await this.redis.mget(...keys);
|
||||
this.tagCache.clear();
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
const tag = keys[i]!.replace("nextCache:tag:", "");
|
||||
const val = values[i];
|
||||
if (val) this.tagCache.set(tag, Number(val));
|
||||
}
|
||||
} catch {
|
||||
// Fail open — stale tag cache is better than crash
|
||||
}
|
||||
}
|
||||
|
||||
async getExpiration(tags: string[]): Promise<number> {
|
||||
if (tags.length === 0) return 0;
|
||||
|
||||
let maxTimestamp = 0;
|
||||
for (const tag of tags) {
|
||||
const cached = this.tagCache.get(tag);
|
||||
if (cached && cached > maxTimestamp) {
|
||||
maxTimestamp = cached;
|
||||
}
|
||||
}
|
||||
return maxTimestamp;
|
||||
}
|
||||
|
||||
async updateTags(tags: string[], durations?: { expire?: number }): Promise<void> {
|
||||
if (tags.length === 0) return;
|
||||
try {
|
||||
await this.redis.connect().catch(() => {});
|
||||
const now = Date.now();
|
||||
const pipeline = this.redis.pipeline();
|
||||
for (const tag of tags) {
|
||||
const key = this.tagKey(tag);
|
||||
pipeline.set(key, String(now));
|
||||
if (durations?.expire) {
|
||||
pipeline.pexpire(key, durations.expire * 1000);
|
||||
} else {
|
||||
pipeline.pexpire(key, 7 * 24 * 60 * 60 * 1000);
|
||||
}
|
||||
this.tagCache.set(tag, now);
|
||||
}
|
||||
await pipeline.exec();
|
||||
} catch {
|
||||
// Fail open
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export type { CacheEntry, CacheHandler, Timestamp } from "next/dist/server/lib/cache-handlers/types";
|
||||
Reference in new issue
Block a user