Skip to content

Commit

Permalink
revalidateIfStaleがtrueになっているとサーバーで取得した古い情報が残るためfalseに
Browse files Browse the repository at this point in the history
  • Loading branch information
takecchi committed Dec 5, 2023
1 parent b6a08be commit 4c756bd
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import PrimaryColumn from '@/app/(menu)/_components/main/PrimaryColumn';
import { UserPost } from '@cuculus/cuculus-api';
import { usePostImmutable } from '@/swr/client/post';
import { usePost } from '@/swr/client/post';
import Post from '@/app/(menu)/_components/timeline/layouts/Post';

type Props = {
Expand All @@ -12,7 +12,7 @@ type Props = {

// FIXME 仮作成。一旦タイムラインのコンポーネントと同じものを使用する
export function PostPage({ postId, fallbackData }: Props) {
const { data } = usePostImmutable(postId, fallbackData);
const { data } = usePost(postId, fallbackData);

if (!data) {
// FIXME 読み込み中
Expand Down
4 changes: 2 additions & 2 deletions src/app/(menu)/_components/timeline/TimelinePost.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { usePostImmutable } from '@/swr/client/post';
import { usePost } from '@/swr/client/post';
import ViewTrigger from '@/app/(menu)/_components/timeline/ViewportTrigger';
import Post from '@/app/(menu)/_components/timeline/layouts/Post';
import { UserPost } from '@cuculus/cuculus-api';
Expand All @@ -21,7 +21,7 @@ function TimelinePost({
}) {
// FIXME 個別取得APIが出来たらmutateで更新するようにする
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { data, mutate } = usePostImmutable(postId, fallbackData);
const { data, mutate } = usePost(postId, fallbackData);

return data ? (
<ViewTrigger
Expand Down
21 changes: 5 additions & 16 deletions src/swr/client/post.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import useSWR from 'swr';
import useSWRImmutable from 'swr/immutable';
import useSWRMutation from 'swr/mutation';
import { postsApi } from '@/libs/cuculus-client';
import { CreatePostRequest, UserPost } from '@cuculus/cuculus-api';
Expand Down Expand Up @@ -60,19 +59,6 @@ const update = async (
return userPost;
};

/**
* ポスト取得
* 自動検証されないので、一覧ページで使うのが望ましい
* @param postId
* @param initialData
*/
export const usePostImmutable = (postId: string, initialData?: UserPost) => {
const { data: authId } = useAuth();
return useSWRImmutable<UserPost>(getKey(postId, authId), fetcher, {
fallbackData: initialData,
});
};

/**
* 投稿に対するアクション
* ※非ログイン状態だと使えません。
Expand All @@ -95,10 +81,13 @@ export const usePostMutation = (postId: string) => {
* ポスト取得
* 自動再検証されるので、詳細ページで使うのが望ましい
* @param postId
* @param initialData
*/
export const usePost = (postId: string) => {
export const usePost = (postId: string, initialData?: UserPost) => {
const { data: authId } = useAuth();
return useSWR<UserPost>(getKey(postId, authId), fetcher);
return useSWR<UserPost>(getKey(postId, authId), fetcher, {
fallbackData: initialData,
});
};

type SendKey = {
Expand Down
1 change: 0 additions & 1 deletion src/swr/client/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export const useUser = (username: string, fallbackData?: UserWithFollows) => {
fetcher,
{
fallbackData,
revalidateIfStale: false,
},
);
};
Expand Down

0 comments on commit 4c756bd

Please sign in to comment.