Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

usernameに@を含められるように修正 #300

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/app/(menu)/(public)/[username]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const getUser = cache(async (username: string) => {
});

export async function generateMetadata({ params }: Params): Promise<Metadata> {
const user = await getUser(params.username);
const username = decodeURIComponent(params.username);
const user = await getUser(username);
Comment on lines +25 to +26

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

decodeURIComponent関数は、不正なURIコンポーネントを渡された場合にエラーをスローします。そのため、この関数の呼び出しはtry-catchブロックで囲むべきです。これにより、ユーザーが不正なパラメータを送信した場合でもアプリケーションがクラッシュすることを防げます。

-  const username = decodeURIComponent(params.username);
+  let username;
+  try {
+    username = decodeURIComponent(params.username);
+  } catch (e) {
+    // 不正なパラメータを処理するための適切なエラーハンドリングを追加します。
+    return {};
+  }
   const user = await getUser(username);

if (!user) {
return {};
}
Expand Down Expand Up @@ -51,7 +52,8 @@ export async function generateMetadata({ params }: Params): Promise<Metadata> {
}

export default async function page({ params }: Params) {
const user = await getUser(params.username);
const username = decodeURIComponent(params.username);
const user = await getUser(username);
Comment on lines +55 to +56

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

同様に、decodeURIComponent関数の呼び出しはtry-catchブロックで囲むべきです。これにより、ユーザーが不正なパラメータを送信した場合でもアプリケーションがクラッシュすることを防げます。

-  const username = decodeURIComponent(params.username);
+  let username;
+  try {
+    username = decodeURIComponent(params.username);
+  } catch (e) {
+    // 不正なパラメータを処理するための適切なエラーハンドリングを追加します。
+    notFound();
+    return;
+  }
   const user = await getUser(username);

if (!user) {
notFound();
}
Expand Down
3 changes: 2 additions & 1 deletion src/app/(menu)/(public)/[username]/posts/[postId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export default async function page({ params }: Params) {
notFound();
}

if (post.author.username !== params.username) {
const username = decodeURIComponent(params.username);
if (post.author.username !== username) {
Comment on lines +73 to +74

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

この変更は、ユーザー名に@が含まれている場合でも正しく動作するようになりました。ただし、decodeURIComponent関数はエラーをスローする可能性があります。URLが不適切にエンコードされている場合や、無効なパーセントエンコーディングが含まれている場合などです。これらのエラーを適切に処理するために、try...catchブロックを使用してください。

-  const username = decodeURIComponent(params.username);
+  let username;
+  try {
+    username = decodeURIComponent(params.username);
+  } catch (e) {
+    // 適切なエラーハンドリングを行います。
+    console.error(e);
+    return;
+  }

redirect(`/${post.author.username}/posts/${post.id}`);
}

Expand Down
Loading