From e14714d5ddff0c7d179dbc1e5cd17e9490f07ee9 Mon Sep 17 00:00:00 2001 From: Aslam H Date: Mon, 7 Oct 2024 17:59:37 +0700 Subject: [PATCH] fix: dont refetch every navigate --- web/app/actions.ts | 4 +--- web/app/routes/__root.tsx | 19 +++++++------------ web/app/routes/_layout/_pages/_protected.tsx | 3 ++- .../_pages/_protected/pages/$pageId/index.tsx | 1 - 4 files changed, 10 insertions(+), 17 deletions(-) diff --git a/web/app/actions.ts b/web/app/actions.ts index 31838fa3..8f3fff17 100644 --- a/web/app/actions.ts +++ b/web/app/actions.ts @@ -21,9 +21,7 @@ const DEFAULT_VALUES = { export const fetchClerkAuth = createServerFn("GET", async (_, ctx) => { const auth = await getAuth(ctx.request) - return { - user: auth, - } + return auth }) export const getFeatureFlag = createServerFn( diff --git a/web/app/routes/__root.tsx b/web/app/routes/__root.tsx index 390f107d..110e6aa2 100644 --- a/web/app/routes/__root.tsx +++ b/web/app/routes/__root.tsx @@ -4,14 +4,12 @@ import { Outlet, ScrollRestoration, createRootRouteWithContext, - useLocation, } from "@tanstack/react-router" import { Body, Head, Html, Meta, Scripts } from "@tanstack/start" import * as React from "react" import { fetchClerkAuth } from "~/actions" import { DefaultCatchBoundary } from "~/components/DefaultCatchBoundary.js" import { NotFound } from "~/components/NotFound.js" -import { isServer } from "~/lib/utils" import appCss from "~/styles/app.css?url" export const TanStackRouterDevtools = @@ -66,18 +64,15 @@ export const Route = createRootRouteWithContext<{ { rel: "manifest", href: "/site.webmanifest", color: "#fffff" }, { rel: "icon", href: "/favicon.ico" }, ], - beforeLoad: async ({ context, cause, location }) => { - // console.log(cause, location) - - const { user } = await fetchClerkAuth() - return { - user, + beforeLoad: async ({ cause }) => { + if (cause !== "stay") { + const auth = await fetchClerkAuth() + return { auth } } - // } - // return { - // user: null, - // } + return { + auth: null, + } }, errorComponent: (props) => { return ( diff --git a/web/app/routes/_layout/_pages/_protected.tsx b/web/app/routes/_layout/_pages/_protected.tsx index b008283c..fd0f7134 100644 --- a/web/app/routes/_layout/_pages/_protected.tsx +++ b/web/app/routes/_layout/_pages/_protected.tsx @@ -2,7 +2,8 @@ import { createFileRoute, Outlet, redirect } from "@tanstack/react-router" export const Route = createFileRoute("/_layout/_pages/_protected")({ beforeLoad: ({ context, location, cause }) => { - if (!context?.user?.userId) { + if (cause === "stay") return + if (!context?.auth?.userId) { throw redirect({ to: "/sign-in/$", search: { redirect_url: location.pathname }, diff --git a/web/app/routes/_layout/_pages/_protected/pages/$pageId/index.tsx b/web/app/routes/_layout/_pages/_protected/pages/$pageId/index.tsx index dc9233a7..b1a0e3f1 100644 --- a/web/app/routes/_layout/_pages/_protected/pages/$pageId/index.tsx +++ b/web/app/routes/_layout/_pages/_protected/pages/$pageId/index.tsx @@ -290,7 +290,6 @@ const DetailPageForm = ({ page }: { page: PersonalPage }) => { onUpdate={(c) => updatePageContent(c, page)} handleKeyDown={handleContentKeyDown} onBlur={(c) => updatePageContent(c, page)} - onNewBlock={(c) => updatePageContent(c, page)} />