Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Fixed react-tweet issue

Steven Teyc0b64f1cda09
diff --git a/app/dub.sh/(blog-post)/blog/[slug]/page.tsx b/app/dub.sh/(blog-post)/blog/[slug]/page.tsx
index 8455516d83..c38eea4f19 100644
--- a/app/dub.sh/(blog-post)/blog/[slug]/page.tsx
+++ b/app/dub.sh/(blog-post)/blog/[slug]/page.tsx
@@ -7,9 +7,9 @@ import { MDX } from "#/ui/content/mdx";
import { getBlurDataURL } from "#/lib/images";
import { Metadata } from "next";
import { constructMetadata, formatDate } from "#/lib/utils";
-import { getTweet } from "react-tweet/api";
import BlurImage from "#/ui/blur-image";
import { BLOG_CATEGORIES } from "#/lib/constants/content";
+import { getAndCacheTweet } from "#/lib/twitter";
export async function generateStaticParams() {
return allBlogPosts.map((post) => ({
@@ -56,7 +56,9 @@ export default async function BlogArticle({
blurDataURL: await getBlurDataURL(src),
})),
),
- await Promise.all(data.tweetIds.map(async (id: string) => getTweet(id))),
+ await Promise.all(
+ data.tweetIds.map(async (id: string) => getAndCacheTweet(id)),
+ ),
]);
const category = BLOG_CATEGORIES.find(
diff --git a/app/dub.sh/help/article/[slug]/page.tsx b/app/dub.sh/help/article/[slug]/page.tsx
index ac90be42eb..287832f644 100644
--- a/app/dub.sh/help/article/[slug]/page.tsx
+++ b/app/dub.sh/help/article/[slug]/page.tsx
@@ -13,7 +13,7 @@ import ArticleLink from "#/ui/content/article-link";
import { getBlurDataURL } from "#/lib/images";
import { Metadata } from "next";
import { constructMetadata } from "#/lib/utils";
-import { getTweet } from "react-tweet/api";
+import { getAndCacheTweet } from "#/lib/twitter";
export async function generateStaticParams() {
return allHelpPosts.map((post) => ({
@@ -64,7 +64,9 @@ export default async function HelpArticle({
blurDataURL: await getBlurDataURL(src),
})),
),
- await Promise.all(data.tweetIds.map(async (id: string) => getTweet(id))),
+ await Promise.all(
+ data.tweetIds.map(async (id: string) => getAndCacheTweet(id)),
+ ),
]);
const relatedArticles =
diff --git a/app/dub.sh/metatags/launch.tsx b/app/dub.sh/metatags/launch.tsx
index 52b4bd8023..67adcf133d 100644
--- a/app/dub.sh/metatags/launch.tsx
+++ b/app/dub.sh/metatags/launch.tsx
@@ -1,8 +1,8 @@
+import { getAndCacheTweet } from "#/lib/twitter";
import Tweet from "#/ui/tweet";
-import { getTweet } from "react-tweet/api";
export default async function LaunchTweet() {
- const tweet = await getTweet("1595465648938930180");
+ const tweet = await getAndCacheTweet("1595465648938930180");
if (!tweet) return null;
return (
<div className="text-left">
diff --git a/app/ui/home/testimonials.tsx b/app/ui/home/testimonials.tsx
index 290cf26d8a..6f4e5f3b62 100644
--- a/app/ui/home/testimonials.tsx
+++ b/app/ui/home/testimonials.tsx
@@ -3,7 +3,8 @@ import { nFormatter } from "#/lib/utils";
import MaxWidthWrapper from "@/components/shared/max-width-wrapper";
import TestimonialsMobile from "./testimonials-mobile";
import Tweet from "#/ui/tweet";
-import { getTweet, Tweet as TweetProps } from "react-tweet/api";
+import { Tweet as TweetProps } from "react-tweet/api";
+import { getAndCacheTweet } from "#/lib/twitter";
const tweets = [
"1631671657617059842",
@@ -27,7 +28,7 @@ export default async function Testimonials() {
const [userCount, tweetsData] = await Promise.all([
prisma.user.count(),
(
- await Promise.all(tweets.map((id) => getTweet(id)))
+ await Promise.all(tweets.map((id) => getAndCacheTweet(id)))
).filter((t) => t) as TweetProps[],
]);
diff --git a/lib/twitter.ts b/lib/twitter.ts
new file mode 100644
index 0000000000..5feac24063
--- /dev/null
+++ b/lib/twitter.ts
@@ -0,0 +1,21 @@
+import { getTweet, Tweet } from "react-tweet/api";
+import { redis } from "./upstash";
+
+export async function getAndCacheTweet(id: string): Promise<Tweet | null> {
+ try {
+ const tweet = await getTweet(id);
+ if (!tweet) {
+ throw new Error("Tweet not found");
+ }
+ // cache the tweet
+ await redis.set(`tweet:${id}`, JSON.stringify(tweet));
+ return tweet;
+ } catch (error) {
+ // check if the tweet is cached
+ const cachedTweet = (await redis.get(`tweet:${id}`)) as string;
+ if (cachedTweet) {
+ return JSON.parse(cachedTweet);
+ }
+ return null;
+ }
+}