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

chore: Update npm dependencies and add normalize-url package #109

Merged
merged 6 commits into from
Jul 27, 2024

Conversation

ttizze
Copy link
Owner

@ttizze ttizze commented Jul 27, 2024

Summary by CodeRabbit

### リリースノート

#### New Feature
- `normalize-url`パッケージを使用してURLを正規化およびサニタイズする機能を追加しました。
- データベース内のURLを正規化およびサニタイズするスクリプトを追加しました。

#### Refactor
- `ContentWithTranslations`コンポーネントから`targetLanguage`プロパティを削除しました。
- URL処理のために`prepareUrlForSearchFromRawInput`関数と`normalizeAndSanitizeUrl`関数を導入しました。

#### Bug fix
- `Translation`コンポーネントのホバー遅延を100msから500msに変更し、背景色を`slae-50`から`white`に変更しました。
- `fetchLatestPageVersionWithTranslations`関数で部分一致検索を行うように修正しました。

#### Chore
- `package.json`から`deploy`コマンドを削除しました。

Copy link

github-actions bot commented Jul 27, 2024

Image description CodeRabbit

### ウォークスルー
この変更では、URLの正規化とサニタイズを中心に、いくつかのコンポーネントや関数が更新されました。特に、`normalize-url`パッケージを導入し、URL処理の一貫性とセキュリティを向上させています。また、UIのホバー動作やデータベースクエリの改善も行われています。

### 変更点
| ファイル | 要約 |
| --- | --- |
| web/app/routes/reader.$encodedUrl/components/ContentWithTranslations.tsx | `ContentWithTranslationsProps`インターフェースから`targetLanguage`プロパティが削除されました。 |
| web/app/routes/reader.$encodedUrl/components/Translation.tsx | ホバー遅延が100msから500msに変更、背景色が`slae-50`から`white`に変更されました。 |
| web/app/routes/reader.$encodedUrl/functions/queries.server.ts | `fetchLatestPageVersionWithTranslations` 関数内の `prisma.pageVersion.findFirst` の `where` 条件が部分一致するように変更されました。 |
| web/app/routes/reader.$encodedUrl/route.tsx | `prepareUrlForSearchFromRawInput` と `normalizeAndSanitizeUrl` を使用して `encodedUrl` を処理するように変更されました。 |
| web/app/routes/translate/route.tsx | `normalizeAndSanitizeUrl` を使用して `submission.value.url` を `normalizedUrl` に置き換えました。 |
| web/app/utils/normalize-and-sanitize-url.server.ts | `normalize-url`パッケージを使用してURLを正規化およびサニタイズする関数を追加しました。 |
| web/package.json | `normalize-url`パッケージ(バージョン8.0.1)が追加され、`deploy`コマンドが削除されました。 |
| web/scripts/normalize-page-urls.ts | Prismaを使用してデータベース内のURLを正規化およびサニタイズするスクリプトを追加しました。 |

Uplevel your code reviews with CodeRabbit Pro

CodeRabbit Pro

If you like this project, please support us by purchasing the Pro version. The Pro version has advanced context, superior noise reduction and several proprietary improvements compared to the open source version. Moreover, CodeRabbit Pro is free for open source projects.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Image description CodeRabbit

Commits Files that changed from the base of the PR and between 9c08c48 and 278c3eb commits.
Files selected (7)
  • web/app/routes/reader.$encodedUrl/components/ContentWithTranslations.tsx (1)
  • web/app/routes/reader.$encodedUrl/components/Translation.tsx (1)
  • web/app/routes/reader.$encodedUrl/functions/queries.server.ts (1)
  • web/app/routes/reader.$encodedUrl/route.tsx (3)
  • web/app/routes/translate/route.tsx (3)
  • web/app/utils/normalize-and-sanitize-url.server.ts (1)
  • web/package.json (1)
Review comments generated (14)
  • Review: 4
  • LGTM: 10

Tips

Chat with Image description CodeRabbit Bot (@coderabbitai)

  • Reply on review comments left by this bot to ask follow-up questions. A review comment is a comment on a diff or a file.
  • Invite the bot into a review comment chain by tagging @coderabbitai in a reply.

Code suggestions

  • The bot may make code suggestions, but please review them carefully before committing since the line number ranges may be misaligned.
  • You can edit the comment made by the bot and manually tweak the suggestion if it is slightly off.

Pausing incremental reviews

  • Add @coderabbitai: ignore anywhere in the PR description to pause further reviews from the bot.

web/app/routes/reader.$encodedUrl/route.tsx Outdated Show resolved Hide resolved
web/app/routes/reader.$encodedUrl/route.tsx Show resolved Hide resolved
web/package.json Show resolved Hide resolved
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Image description CodeRabbit

Commits Files that changed from the base of the PR and between 278c3eb and 79ea683 commits.
Files selected (3)
  • web/app/routes/reader.$encodedUrl/route.tsx (3)
  • web/app/utils/normalize-and-sanitize-url.server.ts (1)
  • web/scripts/normalize-page-urls.ts (1)
Review comments generated (13)
  • Review: 6
  • LGTM: 7

Tips

Chat with Image description CodeRabbit Bot (@coderabbitai)

  • Reply on review comments left by this bot to ask follow-up questions. A review comment is a comment on a diff or a file.
  • Invite the bot into a review comment chain by tagging @coderabbitai in a reply.

Code suggestions

  • The bot may make code suggestions, but please review them carefully before committing since the line number ranges may be misaligned.
  • You can edit the comment made by the bot and manually tweak the suggestion if it is slightly off.

Pausing incremental reviews

  • Add @coderabbitai: ignore anywhere in the PR description to pause further reviews from the bot.

web/app/routes/reader.$encodedUrl/route.tsx Show resolved Hide resolved
Comment on lines +3 to +15
export function normalizeAndSanitizeUrl(inputUrl: string): string {
let decodedUrl: string;
try {
decodedUrl = decodeURIComponent(inputUrl);
} catch {
decodedUrl = inputUrl;
}

return normalizeUrl(decodedUrl, {
stripHash: true,
removeQueryParameters: true,
});
}

Choose a reason for hiding this comment

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

Image description CodeRabbit

normalizeAndSanitizeUrl関数内で、URLのデコードに失敗した場合のエラーハンドリングが不十分です。具体的なエラーをキャッチしてログに記録するか、適切なエラーメッセージを返すべきです。

	try {
		decodedUrl = decodeURIComponent(inputUrl);
	} catch (error) {
		console.error(`Failed to decode URL: ${inputUrl}`, error);
		decodedUrl = inputUrl;
	}

Comment on lines 22 to 30
for (const page of pages) {
const normalizedUrl = normalizeAndSanitizeUrl(page.url);
if (normalizedUrl !== page.url) {
await prisma.page.update({
where: { id: page.id },
data: { url: normalizedUrl },
});
console.log(`Updated Page URL: ${page.url} -> ${normalizedUrl}`);
}

Choose a reason for hiding this comment

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

Image description CodeRabbit

forループ内でのデータベース更新は、パフォーマンスに影響を与える可能性があります。バッチ処理を使用して一度に複数のレコードを更新することを検討してください。

-       for (const page of pages) {
-         const normalizedUrl = normalizeAndSanitizeUrl(page.url);
-         if (normalizedUrl !== page.url) {
-           await prisma.page.update({
-             where: { id: page.id },
-             data: { url: normalizedUrl },
-           });
-           console.log(`Updated Page URL: ${page.url} -> ${normalizedUrl}`);
-         }
-       }
+       const updatePromises = pages.map(async (page) => {
+         const normalizedUrl = normalizeAndSanitizeUrl(page.url);
+         if (normalizedUrl !== page.url) {
+           await prisma.page.update({
+             where: { id: page.id },
+             data: { url: normalizedUrl },
+           });
+           console.log(`Updated Page URL: ${page.url} -> ${normalizedUrl}`);
+         }
+       });
+       await Promise.all(updatePromises);

Comment on lines 47 to 55
for (const pageVersion of pageVersions) {
const normalizedUrl = normalizeAndSanitizeUrl(pageVersion.url);
if (normalizedUrl !== pageVersion.url) {
await prisma.pageVersion.update({
where: { id: pageVersion.id },
data: { url: normalizedUrl },
});
console.log(`Updated PageVersion URL: ${pageVersion.url} -> ${normalizedUrl}`);
}

Choose a reason for hiding this comment

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

Image description CodeRabbit

同様に、forループ内でのデータベース更新はバッチ処理を使用して最適化できます。

-       for (const pageVersion of pageVersions) {
-         const normalizedUrl = normalizeAndSanitizeUrl(pageVersion.url);
-         if (normalizedUrl !== pageVersion.url) {
-           await prisma.pageVersion.update({
-             where: { id: pageVersion.id },
-             data: { url: normalizedUrl },
-           });
-           console.log(`Updated PageVersion URL: ${pageVersion.url} -> ${normalizedUrl}`);
-         }
-       }
+       const updatePromises = pageVersions.map(async (pageVersion) => {
+         const normalizedUrl = normalizeAndSanitizeUrl(pageVersion.url);
+         if (normalizedUrl !== pageVersion.url) {
+           await prisma.pageVersion.update({
+             where: { id: pageVersion.id },
+             data: { url: normalizedUrl },
+           });
+           console.log(`Updated PageVersion URL: ${pageVersion.url} -> ${normalizedUrl}`);
+         }
+       });
+       await Promise.all(updatePromises);

} catch (error) {
console.error("Error during URL normalization:", error);
} finally {
await prisma.$disconnect();

Choose a reason for hiding this comment

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

Image description CodeRabbit

エラーハンドリングのために、prisma.$disconnect()が失敗した場合のログ出力を追加することをお勧めします。

-     await prisma.$disconnect();
+     try {
+       await prisma.$disconnect();
+     } catch (disconnectError) {
+       console.error("Error during Prisma disconnection:", disconnectError);
+     }

} finally {
await prisma.$disconnect();
}
}

Choose a reason for hiding this comment

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

Image description CodeRabbit

ファイルの最後に改行を追加してください。

normalizeUrls();
+ 

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Image description CodeRabbit

Commits Files that changed from the base of the PR and between 79ea683 and 2202350 commits.
Files selected (2)
  • web/package.json (2)
  • web/scripts/normalize-page-urls.ts (1)
Review comments generated (9)
  • Review: 2
  • LGTM: 7

Tips

Chat with Image description CodeRabbit Bot (@coderabbitai)

  • Reply on review comments left by this bot to ask follow-up questions. A review comment is a comment on a diff or a file.
  • Invite the bot into a review comment chain by tagging @coderabbitai in a reply.

Code suggestions

  • The bot may make code suggestions, but please review them carefully before committing since the line number ranges may be misaligned.
  • You can edit the comment made by the bot and manually tweak the suggestion if it is slightly off.

Pausing incremental reviews

  • Add @coderabbitai: ignore anywhere in the PR description to pause further reviews from the bot.

Comment on lines +11 to +35
try {
// Normalize Page URLs
while (true) {
const pages = await prisma.page.findMany({
take: batchSize,
skip: processedPageCount,
select: { id: true, url: true },
});

if (pages.length === 0) break;

for (const page of pages) {
const normalizedUrl = normalizeAndSanitizeUrl(page.url);
if (normalizedUrl !== page.url) {
await prisma.page.update({
where: { id: page.id },
data: { url: normalizedUrl },
});
console.log(`Updated Page URL: ${page.url} -> ${normalizedUrl}`);
}
}

processedPageCount += pages.length;
console.log(`Processed ${processedPageCount} pages`);
}

Choose a reason for hiding this comment

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

Image description CodeRabbit

この部分のコードは、ページURLを正規化およびサニタイズするためにバッチ処理を行っていますが、データベース操作のパフォーマンスを向上させるためにトランザクションを使用することを検討してください。これにより、複数の更新操作を一括でコミットでき、パフォーマンスが向上します。

		await prisma.$transaction(async (prisma) => {
			while (true) {
				const pages = await prisma.page.findMany({
					take: batchSize,
					skip: processedPageCount,
					select: { id: true, url: true },
				});

				if (pages.length === 0) break;

				for (const page of pages) {
					const normalizedUrl = normalizeAndSanitizeUrl(page.url);
					if (normalizedUrl !== page.url) {
						await prisma.page.update({
							where: { id: page.id },
							data: { url: normalizedUrl },
						});
						console.log(`Updated Page URL: ${page.url} -> ${normalizedUrl}`);
					}
				}

				processedPageCount += pages.length;
				console.log(`Processed ${processedPageCount} pages`);
			}
		});

Comment on lines +37 to +62
// Normalize PageVersion URLs
while (true) {
const pageVersions = await prisma.pageVersion.findMany({
take: batchSize,
skip: processedPageVersionCount,
select: { id: true, url: true },
});

if (pageVersions.length === 0) break;

for (const pageVersion of pageVersions) {
const normalizedUrl = normalizeAndSanitizeUrl(pageVersion.url);
if (normalizedUrl !== pageVersion.url) {
await prisma.pageVersion.update({
where: { id: pageVersion.id },
data: { url: normalizedUrl },
});
console.log(
`Updated PageVersion URL: ${pageVersion.url} -> ${normalizedUrl}`,
);
}
}

processedPageVersionCount += pageVersions.length;
console.log(`Processed ${processedPageVersionCount} page versions`);
}

Choose a reason for hiding this comment

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

Image description CodeRabbit

この部分のコードも同様に、ページバージョンURLを正規化およびサニタイズするためにバッチ処理を行っていますが、データベース操作のパフォーマンスを向上させるためにトランザクションを使用することを検討してください。

		await prisma.$transaction(async (prisma) => {
			while (true) {
				const pageVersions = await prisma.pageVersion.findMany({
					take: batchSize,
					skip: processedPageVersionCount,
					select: { id: true, url: true },
				});

				if (pageVersions.length === 0) break;

				for (const pageVersion of pageVersions) {
					const normalizedUrl = normalizeAndSanitizeUrl(pageVersion.url);
					if (normalizedUrl !== pageVersion.url) {
						await prisma.pageVersion.update({
							where: { id: pageVersion.id },
							data: { url: normalizedUrl },
						});
						console.log(
							`Updated PageVersion URL: ${pageVersion.url} -> ${normalizedUrl}`,
						);
					}
				}

				processedPageVersionCount += pageVersions.length;
				console.log(`Processed ${processedPageVersionCount} page versions`);
			}
		});

@ttizze ttizze merged commit be4cdc2 into main Jul 27, 2024
5 checks passed
@ttizze ttizze deleted the ttizze/bug-issue-97 branch July 27, 2024 14:23
@ttizze ttizze added the bug Something isn't working as expected label Jul 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working as expected
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

1 participant