Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export const SharedItemsList = ({
<Link
key={`${sharedItem.id}-${sharedItem.category}`}
href={getItemHref(fullItem)}
prefetch={false}
onClick={(e) => handleItemClick(e, fullItem)}
data-sidebar-item-selected={isSelected}
className={cn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ export const SidebarItem = ({
<div className="flex items-center group/item" style={style}>
<Link
href={itemHref}
prefetch={false}
onClick={handleClick}
data-sidebar-item-selected={isSelected}
className={cn(
Expand Down
1 change: 1 addition & 0 deletions app/_components/GlobalComponents/Sidebar/SidebarItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const SidebarItem = ({
return (
<Link
href={href}
prefetch={false}
onClick={onClick}
className={cn(
"w-full flex items-center gap-3 px-3 py-2.5 text-md lg:text-sm rounded-jotty transition-colors",
Expand Down
5 changes: 4 additions & 1 deletion app/_providers/WebSocketProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from "react";
import { useRouter } from "next/navigation";
import { useEditorActivityStore } from "@/app/_utils/editor-activity-store";
import { useAppMode } from "@/app/_providers/AppModeProvider";
import type { WsEvent } from "@/app/_types";

interface WebSocketContextType {
Expand All @@ -29,6 +30,7 @@ const REFRESH_DEBOUNCE = 500;

export const WebSocketProvider = ({ children }: { children: ReactNode }) => {
const router = useRouter();
const { user } = useAppMode();
const [isConnected, setIsConnected] = useState(false);
const wsRef = useRef<WebSocket | null>(null);
const connectionIdRef = useRef<string | null>(null);
Expand Down Expand Up @@ -70,6 +72,7 @@ export const WebSocketProvider = ({ children }: { children: ReactNode }) => {

const connect = useCallback(() => {
if (!mountedRef.current) return;
if (!user) return;
if (wsRef.current?.readyState === WebSocket.OPEN) return;

const isDev = process.env.NODE_ENV === "development";
Expand Down Expand Up @@ -111,7 +114,7 @@ export const WebSocketProvider = ({ children }: { children: ReactNode }) => {
};

wsRef.current = ws;
}, [handleMessage]);
}, [handleMessage, user]);

useEffect(() => {
mountedRef.current = true;
Expand Down
7 changes: 1 addition & 6 deletions app/api/serwist/[path]/route.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { spawnSync } from "node:child_process";
import { createSerwistRoute } from "@serwist/turbopack";

const revision = spawnSync("git", ["rev-parse", "HEAD"], {
encoding: "utf-8",
}).stdout?.trim() ?? crypto.randomUUID();

export const { dynamic, dynamicParams, revalidate, generateStaticParams, GET } =
createSerwistRoute({
additionalPrecacheEntries: [{ url: "/~offline", revision }],
swSrc: "app/sw.ts",
useNativeEsbuild: true,
nextConfig: {},
maximumFileSizeToCacheInBytes: 6 * 1024 * 1024,
globIgnores: ["flags/**"],
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jotty.page",
"version": "1.21.0",
"version": "1.21.1",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
65 changes: 44 additions & 21 deletions proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,43 @@ import { isEnvEnabled, isDebugFlag } from "./app/_utils/env-utils";

const debugProxy = isDebugFlag("proxy");

const SESSION_CACHE_TTL = 10_000;
const sessionCache = new Map<string, { valid: boolean; ts: number }>();

const _checkSession = async (sessionId: string, internalApiUrl: string, cookie: string) => {
const cached = sessionCache.get(sessionId);
if (cached && Date.now() - cached.ts < SESSION_CACHE_TTL) return cached.valid;

const sessionCheckUrl = new URL(`${internalApiUrl}/api/auth/check-session`);

if (debugProxy) {
console.log("MIDDLEWARE - Session Check URL:", sessionCheckUrl.href);
}

const res = await fetch(sessionCheckUrl, {
headers: { Cookie: cookie },
cache: "no-store",
});

if (debugProxy) {
console.log("MIDDLEWARE - Session Check Response:");
console.log(" status:", res.status);
console.log(" statusText:", res.statusText);
console.log(" ok:", res.ok);
}

sessionCache.set(sessionId, { valid: res.ok, ts: Date.now() });

if (sessionCache.size > 1000) {
const now = Date.now();
sessionCache.forEach((val, key) => {
if (now - val.ts > SESSION_CACHE_TTL) sessionCache.delete(key);
});
}

return res.ok;
};

export const proxy = async (request: NextRequest) => {
const { pathname } = request.nextUrl;

Expand Down Expand Up @@ -61,27 +98,13 @@ export const proxy = async (request: NextRequest) => {
console.log(" → Using:", internalApiUrl);
}

const sessionCheckUrl = new URL(`${internalApiUrl}/api/auth/check-session`);

if (debugProxy) {
console.log("MIDDLEWARE - Session Check URL:", sessionCheckUrl.href);
}

const sessionCheck = await fetch(sessionCheckUrl, {
headers: {
Cookie: request.headers.get("Cookie") || "",
},
cache: "no-store",
});

if (debugProxy) {
console.log("MIDDLEWARE - Session Check Response:");
console.log(" status:", sessionCheck.status);
console.log(" statusText:", sessionCheck.statusText);
console.log(" ok:", sessionCheck.ok);
}
const valid = await _checkSession(
sessionId,
internalApiUrl,
request.headers.get("Cookie") || "",
);

if (!sessionCheck.ok) {
if (!valid) {
const redirectResponse = NextResponse.redirect(loginUrl);
redirectResponse.cookies.delete(cookieName);

Expand All @@ -102,6 +125,6 @@ export const proxy = async (request: NextRequest) => {

export const config = {
matcher: [
"/((?!_next/static|_next/image|favicon.ico|site.webmanifest|sw.js|app-icons).*)",
"/((?!_next/static|_next/image|favicon.ico|site.webmanifest|sw.js|app-icons|app-screenshots|flags|fonts|images|repo-images|themes|openapi.yaml).*)",
],
};
Loading