fix: dont refetch every navigate

This commit is contained in:
Aslam H
2024-10-07 17:59:37 +07:00
parent b43c9762a1
commit e14714d5dd
4 changed files with 10 additions and 17 deletions

View File

@@ -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(

View File

@@ -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 (

View File

@@ -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 },

View File

@@ -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)}
/>
</div>
</div>