Next.js Discord

Discord Forum

suspense not working

Answered
Cornish Rex posted this in #help-forum
Open in Discord
Cornish RexOP
Hello, im really new to nextjs trying to learn it this is my users/page.tsx:
import React, { Suspense } from 'react'
import UsersTable from './UsersTable'

interface Props {
  searchParams: { sortOrder: string }
}

const UsersPage = ({ searchParams: { sortOrder } }: Props) => {
  return (
    <>
      <h1>Users</h1>
      <Suspense fallback={<p>Loading...</p>}>
        <UsersTable sortOrder={sortOrder} />
      </Suspense>
    </>
  )
}

export default UsersPage

this is my usersTable.tsx:
import Link from 'next/link';
import { sort } from 'fast-sort';

interface User {
    id: number;
    name: string;
    username: string;
    email: string;
}

interface Props {
    sortOrder: string;
}

const UsersTable = async ({ sortOrder }: Props) => {
    const res = await fetch('https://jsonplaceholder.typicode.com/users');
    const users: User[] = await res.json();

    const sortedUsers = sort(users).asc(
        sortOrder === 'Email' 
        ? user => user.email
        : user => user.name
    );

    return (
        <long_html_here>
    )
}

export default UsersTable


my suspense:
 <Suspense fallback={<p>Loading...</p>}>
        <UsersTable sortOrder={sortOrder} />
</Suspense>

doesnt seem to be working, if i go to network tab i can see the server is sending the page with the table instead of Loading... but if i go to devtools componenets and force suspense i do see the loading, any idea why?
Answered by Cornish Rex
    const res = await fetch('https://jsonplaceholder.typicode.com/users', {
        cache: 'no-cache',
    });

added the cache: 'no-cache'
fiex it that was a unfortunate hahaha, thank you @riský !
View full answer

22 Replies

if the whole page is loading, you should use [loading.tsx](https://nextjs.org/docs/app/building-your-application/routing/loading-ui-and-streaming) because it is more optimised for this
also, i think loading.tsx is better for server components with fetch req comapred to using it manualy
Cornish RexOP
Thanks, lemme give it a shot and see if it fixes it
nope, i tried the same loading.tsx from the docs still facing the same problem
wait so what is the issue? and loading file doesn't want any suspense... it adds it itsself
Cornish RexOP
im following a tutorial on that, and i saw once the client request he will get loading... first instead of the full table as seen here:

but for some reasons mine will just load the full page then send it to client
they may have changed how it works... but does it show loading and then you content?
Cornish RexOP
The tutorial is nextjs 13.4 while we are in 13.5 could be yeah, but in the tutorial first it shows loading then will get the table but for me it will just load the table without loading
but if i am in the homepage then go to the /users i see the loading, but reloading in the page (even if i clear the cache) wont show loading at all
well this example https://app-router.vercel.app/loading/books (made by nextjs), sends me the loading page and then the actual page coms after that initial request (but it could be a little old version)
Cornish RexOP
thats what im trying to achieve... not sure what the problem is for me 🤔 im trying first to show loading elements until all the contents on the page loads
ill push my code to github so you can take a better look at it if you want
also make sure you aren't using dev mode and instead prod (next build && next start)
@riský also make sure you aren't using dev mode and instead prod (next build && next start)
Cornish RexOP
i tried in prod as well sadly didnt work
ill wait for your github, and ill try it myself too
Cornish RexOP
Awesome, thank you! it will take a bit since i restore my pc and reinstalling git
Sorry for being late, its was the git ssh authentication took me a bit
app/users/page.tsx
Okey its fixed now, turns out it was the cache on the server 😤
Cornish RexOP
    const res = await fetch('https://jsonplaceholder.typicode.com/users', {
        cache: 'no-cache',
    });

added the cache: 'no-cache'
fiex it that was a unfortunate hahaha, thank you @riský !
Answer
ahh yeah, that makes sense... its just too speedy when not need\ing to do anything