Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: middleware fetch failed for docker container #1424

Merged
merged 2 commits into from
Nov 4, 2024
Merged
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
22 changes: 20 additions & 2 deletions apps/nextjs/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import type { NextRequest } from "next/server";
import { createTRPCClient, httpLink } from "@trpc/client";
import SuperJSON from "superjson";

import { fetchApi } from "@homarr/api/client";
import type { AppRouter } from "@homarr/api";
import { createI18nMiddleware } from "@homarr/translation/middleware";

export async function middleware(request: NextRequest) {
const culture = await fetchApi.serverSettings.getCulture.query();
// fetch api does not work because window is not defined and we need to construct the url from the headers
// In next 15 we will be able to use node apis and such the db directly
const culture = await serverFetchApi.serverSettings.getCulture.query();

// We don't want to fallback to accept-language header so we clear it
request.headers.set("accept-language", "");
Expand All @@ -15,3 +19,17 @@ export async function middleware(request: NextRequest) {
export const config = {
matcher: ["/((?!api|static|.*\\..*|_next|favicon.ico|robots.txt).*)"],
};

export const serverFetchApi = createTRPCClient<AppRouter>({
links: [
httpLink({
url: `http://${process.env.HOSTNAME ?? "localhost"}:3000/api/trpc`,
transformer: SuperJSON,
headers() {
const headers = new Headers();
headers.set("x-trpc-source", "server-fetch");
return headers;
},
}),
],
});