-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Log when a tweet does not exist or if it's private (#133)
* Log when a tweet does not exist or if it's private * Added caching * Updated deps * Updated docs * Updated docs more * Added changeset * Updated deps * Updated link
- Loading branch information
Showing
19 changed files
with
2,365 additions
and
1,322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'react-tweet': minor | ||
--- | ||
|
||
Updated docs on caching tweets and added fetchTweet function. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
import { NextResponse } from 'next/server' | ||
import { getTweet } from 'react-tweet/api' | ||
import cors from 'edge-cors' | ||
|
||
type RouteSegment = { params: { id: string } } | ||
|
||
export async function GET(req: Request, { params }: RouteSegment) { | ||
export const fetchCache = 'only-cache' | ||
|
||
export async function GET(_req: Request, { params }: RouteSegment) { | ||
try { | ||
const tweet = await getTweet(params.id) | ||
return cors( | ||
req, | ||
NextResponse.json({ data: tweet ?? null }, { status: tweet ? 200 : 404 }) | ||
return NextResponse.json( | ||
{ data: tweet ?? null }, | ||
{ status: tweet ? 200 : 404 } | ||
) | ||
} catch (error: any) { | ||
return cors( | ||
req, | ||
NextResponse.json( | ||
{ error: error.message ?? 'Bad request.' }, | ||
{ status: 400 } | ||
) | ||
console.error(error) | ||
return NextResponse.json( | ||
{ error: error.message ?? 'Bad request.' }, | ||
{ status: 400 } | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Suspense } from 'react' | ||
import { TweetSkeleton } from 'react-tweet' | ||
import TweetPage from './tweet-page' | ||
|
||
export const revalidate = 86400 | ||
|
||
const Page = ({ params }: { params: { tweet: string } }) => ( | ||
<Suspense fallback={<TweetSkeleton />}> | ||
<TweetPage id={params.tweet} /> | ||
</Suspense> | ||
) | ||
|
||
export default Page |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { unstable_cache } from 'next/cache' | ||
import { getTweet as _getTweet } from 'react-tweet/api' | ||
import { EmbeddedTweet, TweetNotFound } from 'react-tweet' | ||
|
||
const getTweet = unstable_cache( | ||
async (id: string) => _getTweet(id), | ||
['tweet'], | ||
{ revalidate: 3600 * 24 } | ||
) | ||
|
||
const TweetPage = async ({ id }: { id: string }) => { | ||
try { | ||
const tweet = await getTweet(id) | ||
return tweet ? <EmbeddedTweet tweet={tweet} /> : <TweetNotFound /> | ||
} catch (error) { | ||
console.error(error) | ||
return <TweetNotFound error={error} /> | ||
} | ||
} | ||
|
||
export default TweetPage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Suspense } from 'react' | ||
import { TweetSkeleton } from 'react-tweet' | ||
import TweetPage from './tweet-page' | ||
|
||
export const revalidate = 86400 | ||
|
||
const Page = ({ params }: { params: { tweet: string } }) => ( | ||
<Suspense fallback={<TweetSkeleton />}> | ||
<TweetPage id={params.tweet} /> | ||
</Suspense> | ||
) | ||
|
||
export default Page |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { fetchTweet, Tweet } from 'react-tweet/api' | ||
import { EmbeddedTweet, TweetNotFound } from 'react-tweet' | ||
import { kv } from '@vercel/kv' | ||
|
||
async function getTweet( | ||
id: string, | ||
fetchOptions?: RequestInit | ||
): Promise<Tweet | undefined> { | ||
try { | ||
const { data, tombstone, notFound } = await fetchTweet(id, fetchOptions) | ||
|
||
if (data) { | ||
await kv.set(`tweet:${id}`, data) | ||
return data | ||
} else if (tombstone || notFound) { | ||
// remove the tweet from the cache if it has been made private by the author (tombstone) | ||
// or if it no longer exists. | ||
await kv.del(`tweet:${id}`) | ||
} | ||
} catch (error) { | ||
console.error('fetching the tweet failed with:', error) | ||
} | ||
|
||
const cachedTweet = await kv.get<Tweet>(`tweet:${id}`) | ||
return cachedTweet ?? undefined | ||
} | ||
|
||
const TweetPage = async ({ id }: { id: string }) => { | ||
try { | ||
const tweet = await getTweet(id) | ||
return tweet ? <EmbeddedTweet tweet={tweet} /> : <TweetNotFound /> | ||
} catch (error) { | ||
console.error(error) | ||
return <TweetNotFound error={error} /> | ||
} | ||
} | ||
|
||
export default TweetPage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
261e72d
There was a problem hiding this comment.
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:
react-tweet-create-react-app – ./apps/create-react-app
create-react-app-gray-seven.vercel.app
react-tweet-create-react-app.vercel.rocks
react-tweet-create-react-app-git-main.vercel.rocks
261e72d
There was a problem hiding this comment.
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:
react-tweet-custom-tweet-dub – ./apps/custom-tweet-dub
react-tweet-dub.vercel.app
react-tweet-custom-tweet-dub.vercel.rocks
react-tweet-custom-tweet-dub-git-main.vercel.rocks
261e72d
There was a problem hiding this comment.
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:
react-tweet-next-app – ./apps/next-app
next-tweet-test-app-indol.vercel.app
react-tweet-next.vercel.app
react-tweet-next-app.vercel.rocks
react-tweet-next-app-git-main.vercel.rocks
261e72d
There was a problem hiding this comment.
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:
react-tweet-vite-app – ./apps/vite-app
react-tweet-vite-app.vercel.app
react-tweet-vite-app.vercel.rocks
react-tweet-vite-app-git-main.vercel.rocks