Redirect user to 404 not found if data doesn't exist

I am using t3 stack with pages directory. Since I save the projectId in the URL and use that to access a specific project the user navigates to anyone can manually adjust the url to a non-existant projectID and they will be taken to an empty or continuously loading page (depends how its handled). I want to take them to a 404 instead. How can I do that? I tried the following but it 404s even for existing projects
const { data: projectData, isLoading: isLoadingProjectData } = api.project.getById.useQuery({ id: projectId }, { enabled: !!projectId });

if (!projectData && !isLoadingProjectData) {
notFound();
}
const { data: projectData, isLoading: isLoadingProjectData } = api.project.getById.useQuery({ id: projectId }, { enabled: !!projectId });

if (!projectData && !isLoadingProjectData) {
notFound();
}
1 Reply
NotLuksus
NotLuksus7mo ago
Even tho you are using pages directory I would probably check this on the server, in getServerSideProps