Skip to content

Commit

Permalink
Feature/OGPf (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttizze authored Jan 6, 2025
2 parents b97e498 + e6c13c3 commit 4ab8abf
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions web/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,16 @@ import { Footer } from "~/routes/resources+/footer";
import { Header } from "~/routes/resources+/header";
import tailwind from "~/tailwind.css?url";
import { authenticator } from "~/utils/auth.server";
import { commitSession, getSession } from "~/utils/session.server";
import { ensureGuestId } from "~/utils/ensureGuestId.server";
import { commitSession } from "~/utils/session.server";
export async function loader({ request }: LoaderFunctionArgs) {
const isDevelopment = process.env.NODE_ENV === "development";

const gaTrackingId = isDevelopment
? ""
: (process.env.GOOGLE_ANALYTICS_ID ?? "");
const currentUser = await authenticator.isAuthenticated(request);
const session = await getSession(request.headers.get("Cookie"));
const guestId = session.get("guestId");
if (!currentUser && !guestId) {
session.set("guestId", crypto.randomUUID());
return redirect(request.url, {
headers: { "Set-Cookie": await commitSession(session) },
});
}
const { session, guestId } = await ensureGuestId(request);
const locale = (await i18nServer.getLocale(request)) || "en";
const url = new URL(request.url);
const pathSegments: string[] = url.pathname.split("/").filter(Boolean);
Expand All @@ -47,6 +41,13 @@ export async function loader({ request }: LoaderFunctionArgs) {
return redirect(url.toString());
}
const currentLocale = pathSegments[0];
const headers = new Headers();
headers.set(
"Set-Cookie",
(await commitSession(session)) +
(await localeCookie.serialize(currentLocale)),
);

return data(
{
isDevelopment,
Expand All @@ -55,7 +56,7 @@ export async function loader({ request }: LoaderFunctionArgs) {
gaTrackingId,
},
{
headers: { "Set-Cookie": await localeCookie.serialize(currentLocale) },
headers,
},
);
}
Expand Down

0 comments on commit 4ab8abf

Please sign in to comment.