Skip to content

Commit

Permalink
easy
Browse files Browse the repository at this point in the history
  • Loading branch information
0-don committed Nov 22, 2024
1 parent ba1dd1a commit 632d24b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
5 changes: 2 additions & 3 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { NextResponse, type NextRequest } from "next/server";
import { serverEnv } from "./utils/env/server";
import { SERVER_URL_KEY } from "./utils/constants";

/**
* Nextjs bypass to set the server URL in the request headers so it can be read while in server components.
*/
export default async function middleware(request: NextRequest) {
const headers = new Headers(request.headers);
headers.set(serverEnv.SERVER_URL_KEY, request.url);
headers.set(SERVER_URL_KEY, request.url);
return NextResponse.next({ headers });
}

export const config = {
matcher: ["/((?!api|_next|_vercel|.*\\..*).*)"],
unstable_allowDynamic: ["/node_modules/elysia/dist/index.mjs"],
};
2 changes: 2 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Note: bug in nodejs cant import serverEnv into middleware.ts
export const SERVER_URL_KEY = "x-url";
5 changes: 3 additions & 2 deletions src/utils/env/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SERVER_URL_KEY } from "@/utils/constants";
import { Elysia, t } from "elysia";

const {
Expand All @@ -13,7 +14,7 @@ const {
},
),
AUTH_COOKIE: t.Literal("auth", { error: "AUTH_COOKIE not set!" }),
SERVER_URL_KEY: t.Literal("x-url", { error: "SERVER_URL not set!" }),
SERVER_URL_KEY: t.Literal(SERVER_URL_KEY, { error: "SERVER_URL not set!" }),
SEVEN_DAYS: t.Integer({ minimum: 1, error: "SEVEN_DAYS not set!" }),
}),
});
Expand All @@ -23,7 +24,7 @@ const serverEnvResult = serverSchema.safeParse({
SECRET: process.env.SECRET,
NODE_ENV: process.env.NODE_ENV,
AUTH_COOKIE: "auth",
SERVER_URL_KEY: "x-url",
SERVER_URL_KEY,
SEVEN_DAYS: 60 * 60 * 24 * 7, // 7 days in seconds
});

Expand Down

0 comments on commit 632d24b

Please sign in to comment.