인프런 커뮤니티 질문&답변

챠챠_님의 프로필 이미지

작성한 질문수

Next + React Query로 SNS 서비스 만들기

하트 누를 때 optimistic update 적용하기

link태그의 prefetching 질문

24.06.02 14:21 작성

·

129

0

안녕하세요 선생님
상세페이지에서 홈으로 이동할때 로딩화면에 관련해서
Link태그의 prefetching 질문있습니다.

아래와 같이 suspense를 적용했을때
app/(afterLoging)/home/page.tsx

import style from './home.module.scss'
import Tab from "@/app/(afterLogin)/home/_component/Tab";
import TabProvider from "@/app/(afterLogin)/home/_component/TabProvider";
import PostForm from "@/app/(afterLogin)/home/_component/PostForm";

import TabDeciderSuspense from '@/app/(afterLogin)/home//_component/TabDeciderSuspense';
import { Suspense } from 'react';
import Loading from './loading';
import { auth } from '@/auth';

export default async function Home() {
  const session = await auth();
  return (
    <main className={style.main}>
      <TabProvider>
        <Tab />
        <PostForm me={session} />
        {/* suspense는 서버컴포넌트여야만 한다. */}
        {/* suspense는 부모컴포넌트여야지 자식(아래)있는 컴포넌트 감지할 수 있다. */}
        <Suspense fallback={<Loading />}>
          <TabDeciderSuspense />
        </Suspense>
      </TabProvider>
    </main>
  );
}


next.js 문서를 보면
link태그가 있는 경우, 화면에 들어왔을때
static한 부분은 prefetch하고, 데이터 호출이 필요한 경우는 loading.tsx까지 호출해준다고 되어있더라구요.

그래서 제가 기대한 것은 상세페이지에서, 홈의 Link태그가 화면에 들어오기 대문에, 홈으로 이동했을때
첫번째 이미지가 아닌, 두번째 이미지처럼 로딩이 되어야할 것 같은데 첫번째 이미지 처럼 되더라구요.
(이동한것도 30초 이내였습니다)

혹시 제가 잘못이해한건지 알려주시면 감사합니다.

유저 상세페이지에서 홈으로 이동할때



suspense적용후 새로고침하거나 팔로우중 클릭시

답변 1

0

제로초(조현영)님의 프로필 이미지
제로초(조현영)
지식공유자

2024. 06. 02. 14:30

첫 번째 이미지는 loading.tsx가 보이는 것이고, 두 번째 이미지는 suspense의 fallback이 실행되는 중일 겁니다.

그런데 공식문서에서는 Link prefetch는 layout만 prefetch한다고 되어있는데요? 그래서 바로 loading.tsx를 보여준다고 되어있습니다.

The <Link>'s default prefetching behavior (i.e. when the prefetch prop is left unspecified or set to null) is different depending on your usage of loading.js. Only the shared layout, down the rendered "tree" of components until the first loading.js file, is prefetched and cached for 30s. This reduces the cost of fetching an entire dynamic route, and it means you can show an instant loading state for better visual feedback to users.

챠챠_님의 프로필 이미지
챠챠_
질문자

2024. 06. 02. 20:19

아 설명감사합니다. 제가 잘못 이해했던것 같습니다.

[loading UI and Streaming]
Streaming works well with React's component model because each component can be considered a chunk. Components that have higher priority (e.g. product information) or that don't rely on data can be sent first (e.g. layout), and React can start hydration earlier. Components that have lower priority (e.g. reviews, related products) can be sent in the same server request after their data has been fetched.
여기서는 레이아웃에 더해서 상단의 홈, 추천, 팔로우 화면은 레이아웃처럼 데이터에 의존하지 않는 컴포넌트기에 하이드레이션이 같이 더 일찍된다고 이해를 했었고,

[Components/ <Link>]
null (default): Prefetch behavior depends on whether the route is static or dynamic. For static routes, the full route will be prefetched (including all its data). For dynamic routes, the partial route down to the nearest segment with a loading.js boundary will be prefetched.
/home이라는 정적 라우트니까 프리패치 되었을거라고 생각했었습니다.

그런데 그 prefetch가 layout한정이었군요.
이상하게 이해하고 넘어갈뻔했는데 감사합니다!

챠챠_님의 프로필 이미지

작성한 질문수

질문하기