inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

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

하트 누를 때 optimistic update 적용하기

link태그의 prefetching 질문

231

챠챠_

작성한 질문수 40

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적용후 새로고침하거나 팔로우중 클릭시

react next.js react-query next-auth msw

답변 1

0

제로초(조현영)

첫 번째 이미지는 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.

1

챠챠_

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

[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한정이었군요.
이상하게 이해하고 넘어갈뻔했는데 감사합니다!

캡처링부분 질문있습니다.

0

76

2

깃에 소스코드를 찾을 수 없습니다.

0

114

2

useInfiniteQuery promise와 react use 사용시 페이지 무한 로딩

0

98

1

import 파일 경로를 찾지 못 해서 에러가 발생합니다.

0

111

2

css 라이브러리 추천 부탁드립니다

0

141

2

팔로우 추천 목록이 빈 배열로 들어옵니다.

0

133

1

게시물 업로드 시 userId가 undefined로 들어가는 오류

0

119

1

useSuspenseQuery 사용 시 SSR 401 이슈 관련

0

173

1

리액트 쿼리 useinfinitequery 무한스크롤 구현 시 페이지가 이동할 경우 데이터가 보존되게 할 수 있나요??

0

186

3

폴링이 필요없는 이유

0

93

2

next Request Memoization과 react cache

0

110

2

seo 최적화 기준은 데이터 fetching인가요 아님 데이터 렌더링인가요?

0

84

2

next.js 서버fetch 에러 fallback ui 구현 방법

0

173

2

프레임워크 여론 파악법

0

125

2

필터옵션이 많은 페이지에서 서버 fetch를 하는게 맞는걸까요??

0

103

2

서버 fetch suspense 로 감싸고 새로고침 시 잠시 빈 화면이 노출된 후 fallback ui가 노출됩니다.

0

102

2

template.tsx 내 서버fetch 응답값과 클라이언트 컴포넌트 상태값 싱크가 맞지 않는 이슈

0

66

2

Auth.js 사용 시 authorize 함수가 호출되지 않습니다

0

132

2

next.js 에서 로그인에 관하여

0

139

1

Next의 route handler에 대한 질문이 있습니다.

0

101

2

게시판 리스트 만들때 use client를 어디서부터 집어넣어야할지 모르겠습니다

0

99

2

프라이빗 폴더를 해야 하는 이유가 모호한 것 같아요.

0

85

2

vanilla-extract 못찾는 문제

0

230

2

fetch 캐싱과 revalidate 관련

0

86

2