Skip to content

Commit

Permalink
fix tweet caching to consider tombstones
Browse files Browse the repository at this point in the history
  • Loading branch information
rauchg committed Dec 17, 2023
1 parent a9bdadb commit 24df4e4
Show file tree
Hide file tree
Showing 4 changed files with 610 additions and 191 deletions.
36 changes: 17 additions & 19 deletions app/(post)/components/tweet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,34 @@ interface TweetArgs {
}

async function getAndCacheTweet(id: string): Promise<Tweet | undefined> {
// we first prioritize getting a fresh tweet
try {
const tweet = await getTweet(id);

if (tweet) {
await redis.set(`tweet:${id}`, JSON.stringify(tweet));
// @ts-ignore
if (tweet && !tweet.tombstone) {
// we populate the cache if we have a fresh tweet
await redis.set(`tweet:${id}`, tweet);
return tweet;
} else {
return (await redis.get(`tweet:${id}`)) ?? undefined;
}
} catch (error) {
return (await redis.get(`tweet:${id}`)) ?? undefined;
console.error("tweet fetch error", error);
}

const cachedTweet: Tweet | null = await redis.get(`tweet:${id}`);

// @ts-ignore
if (!cachedTweet || cachedTweet.tombstone) return undefined;
console.log("tweet cache hit", id);

return cachedTweet;
}

const TweetContent = async ({ id, components, onError }: TweetProps) => {
let error;
const tweet = id
? await getAndCacheTweet(id).catch(err => {
if (onError) {
error = onError(err);
} else {
console.error(err);
error = err;
}
})
: undefined;
const TweetContent = async ({ id, components }: TweetProps) => {
const tweet = id ? await getAndCacheTweet(id) : undefined;

if (!tweet) {
const NotFound = components?.TweetNotFound || TweetNotFound;
return <NotFound error={error} />;
return <TweetNotFound />;
}

return <EmbeddedTweet tweet={tweet} components={components} />;
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"date-fns": "^2.29.3",
"image-size": "^1.0.2",
"load-script": "^1.0.0",
"next": "14.0.4-canary.6",
"next": "14.0.5-canary.16",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-intersection-observer": "^8.34.0",
Expand All @@ -30,8 +30,8 @@
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"autoprefixer": "^10.4.14",
"eslint": "^8.36.0",
"eslint-config-next": "^13.2.4",
"eslint": "^8.56.0",
"eslint-config-next": "^14.0.4",
"postcss": "^8.4.21",
"prettier": "^2.8.4",
"tailwindcss": "^3.3.1",
Expand Down
Loading

1 comment on commit 24df4e4

@vercel
Copy link

@vercel vercel bot commented on 24df4e4 Dec 17, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

blog – ./

blog-rauchg.vercel.app
www.rauchg.com
rauchg.com
blog-git-main-rauchg.vercel.app

Please sign in to comment.