perf(proxy): isolate settings database reads

This commit is contained in:
ajnart committed 2026-07-29 06:47:07 +02:00
1 parent add6ee56ba
commit f8d8a09c56
17 files changed
+604 -18

No files matched your search

+84
View File
@@ -0,0 +1,84 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { NextRequest, NextResponse } from "next/server";
import { localeCookieKey } from "@homarr/definitions";
const mocks = vi.hoisted(() => ({
getOnboardingStep: vi.fn(),
getDefaultLocale: vi.fn(),
createI18nMiddleware: vi.fn(),
}));
vi.mock("@homarr/db/proxy-reader", () => ({
getDefaultLocaleForProxyAsync: mocks.getDefaultLocale,
getOnboardingStepForProxyAsync: mocks.getOnboardingStep,
}));
vi.mock("@homarr/translation/middleware", () => ({
createI18nMiddleware: mocks.createI18nMiddleware,
}));
const loadProxy = async () => {
vi.resetModules();
return import("./proxy");
};
const createRequest = (pathname: string, locale?: string) => {
const request = new NextRequest(`http://localhost${pathname}`);
if (locale) request.cookies.set(localeCookieKey, locale);
return request;
};
describe("locale and onboarding proxy", () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.getOnboardingStep.mockResolvedValue("finish");
mocks.getDefaultLocale.mockResolvedValue("en");
mocks.createI18nMiddleware.mockImplementation((defaultLocale: string) => (request: NextRequest) => {
request.headers.set("x-test-default-locale", defaultLocale);
return NextResponse.next();
});
});
it("redirects unfinished onboarding to /init", async () => {
mocks.getOnboardingStep.mockResolvedValue("integrations");
const { proxy } = await loadProxy();
const response = await proxy(createRequest("/en/dashboard"));
expect(response.status).toBe(307);
expect(response.headers.get("location")).toBe("http://localhost/init");
expect(mocks.getOnboardingStep).toHaveBeenCalledOnce();
});
it("allows /init while onboarding is unfinished", async () => {
mocks.getOnboardingStep.mockResolvedValue("start");
const { proxy } = await loadProxy();
const response = await proxy(createRequest("/en/init"));
expect(response.status).toBe(200);
expect(mocks.getOnboardingStep).not.toHaveBeenCalled();
expect(mocks.getDefaultLocale).toHaveBeenCalledOnce();
});
it("uses the configured default locale for a missing or invalid locale cookie", async () => {
mocks.getDefaultLocale.mockResolvedValue("de");
const { proxy } = await loadProxy();
await proxy(createRequest("/en/dashboard", "not-a-locale"));
expect(mocks.getDefaultLocale).toHaveBeenCalledOnce();
expect(mocks.createI18nMiddleware).toHaveBeenCalledWith("de");
});
it("keeps a valid locale cookie and does not query the configured default", async () => {
const { proxy } = await loadProxy();
const response = await proxy(createRequest("/fr/dashboard", "fr"));
expect(response.status).toBe(200);
expect(mocks.getDefaultLocale).not.toHaveBeenCalled();
expect(mocks.createI18nMiddleware).toHaveBeenCalledWith("en");
});
});
+10 -7
View File
@@ -1,10 +1,10 @@
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { api } from "@homarr/api/server";
import { localeCookieKey } from "@homarr/definitions";
import type { SupportedLanguage } from "@homarr/translation";
import { supportedLanguages } from "@homarr/translation";
import { getDefaultLocaleForProxyAsync, getOnboardingStepForProxyAsync } from "@homarr/db/proxy-reader";
import { localeCookieKey } from "@homarr/definitions/cookie";
import type { SupportedLanguage } from "@homarr/translation/languages";
import { supportedLanguages } from "@homarr/translation/languages";
import { createI18nMiddleware } from "@homarr/translation/middleware";
let isOnboardingFinished = false;
@@ -14,8 +14,8 @@ export async function proxy(request: NextRequest) {
const pathname = request.nextUrl.pathname;
if (!isOnboardingFinished && !pathname.endsWith("/init")) {
const currentOnboardingStep = await api.onboard.currentStep();
if (currentOnboardingStep.current !== "finish") {
const currentOnboardingStep = await getOnboardingStepForProxyAsync();
if (currentOnboardingStep !== "finish") {
return NextResponse.redirect(new URL("/init", request.url));
}
@@ -26,7 +26,10 @@ export async function proxy(request: NextRequest) {
const currentLocale = request.cookies.get(localeCookieKey)?.value;
let defaultLocale: SupportedLanguage = "en";
if (!currentLocale || !supportedLanguages.includes(currentLocale as SupportedLanguage)) {
defaultLocale = await api.serverSettings.getCulture().then((culture) => culture.defaultLocale);
const configuredLocale = await getDefaultLocaleForProxyAsync();
if (supportedLanguages.includes(configuredLocale as SupportedLanguage)) {
defaultLocale = configuredLocale as SupportedLanguage;
}
}
// We don't want to fallback to accept-language header so we clear it