Best way to show Landing or Dashboard Page, based on Session? #2274
-
Hi, when a user visits my index page At the moment my index page looks like this: import { BlitzPage, useSession } from "blitz"
import { Suspense } from "react"
import Dashboard from "./dashboard"
import Landing from "./Landing"
const Home: BlitzPage = () => {
const CurrentSession = () => {
const session = useSession()
if (session.userId) {
return <Dashboard />
} else {
return <Landing />
}
}
return (
<div>
<Suspense fallback="Loading...">
<CurrentSession />
</Suspense>
</div>
)
}
Home.suppressFirstRenderFlicker = true
export default Home however, this approach has a problem: if I know the URLs, I can still manually navigate to How can I best solve this so that there are no performance problems or similar later in production mode? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
That code looks exactly right. If you don't want
Any components inside a |
Beta Was this translation helpful? Give feedback.
-
hey @flybayer what is Also, if you use the useSession inside the getServerSideProps in this case, wouldn't that allow to remove the Suspense and not show the loading indicator, any downside in that approach? |
Beta Was this translation helpful? Give feedback.
-
hey @flybayer sorry to re iterate over this, I'm just trying to understands how useSession plus suspense and suppressFirstRenderFlicker, authenticated and getServerSideProps all fit together. I have a Page if a user is logged in, it shows one thing, if not some other thing. Like the OP. Another scenario is the following I'm trying to avoid any loading indicator when the page loads for the first time by prefetching the queries inside the getServerSideProps but I cannot do the same for the useSession, so I always end up with a loading state, unless I set the suspense to false in the useSession hook, but then this brings another issue where I need to check if the session is loaded or not Sorry if I'm deviating from the OP, I can start a new discussion if you think it's better or not related |
Beta Was this translation helpful? Give feedback.
That code looks exactly right. If you don't want
/landing
or/dashboard
pages to work, then put those components elsewhere in the file system like this:Any components inside a
pages
folder will be exposed at a URL. To not have a url, put the components in a different folder.