Unverified Commit 681429e7 authored by Eng Zer Jun's avatar Eng Zer Jun Committed by GitHub

docs: add TypeScript code blocks in ISR page (#71492)

Currently only the code block in the first section of the ISR page has
both TypeScript and JavaScript code. This PR updates the remaining code
blocks to include TypeScript code.

/cc original author of ISR page @leerob

---------
Co-authored-by: 's avatarLee Robinson <lee@leerob.com>
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 3cd1a749
...@@ -49,9 +49,9 @@ export async function generateStaticParams() { ...@@ -49,9 +49,9 @@ export async function generateStaticParams() {
} }
export default async function Page({ params }: { params: { id: string } }) { export default async function Page({ params }: { params: { id: string } }) {
const post = await fetch(`https://api.vercel.app/blog/${params.id}`).then( const post: Post = await fetch(
(res) => res.json() `https://api.vercel.app/blog/${params.id}`
) ).then((res) => res.json())
return ( return (
<main> <main>
<h1>{post.title}</h1> <h1>{post.title}</h1>
...@@ -61,10 +61,10 @@ export default async function Page({ params }: { params: { id: string } }) { ...@@ -61,10 +61,10 @@ export default async function Page({ params }: { params: { id: string } }) {
} }
``` ```
```jsx filename="app/blog/[id]/page.jsx" switcher ```jsx filename="app/blog/[id]/page.js" switcher
// Next.js will invalidate the cache when a // Next.js will invalidate the cache when a
// request comes in, at most once every 60 seconds. // request comes in, at most once every 60 seconds.
export const revalidate = 60; export const revalidate = 60
// We'll prerender only the params from `generateStaticParams` at build time. // We'll prerender only the params from `generateStaticParams` at build time.
// If a request comes in for a path that hasn't been generated, // If a request comes in for a path that hasn't been generated,
...@@ -72,20 +72,24 @@ export const revalidate = 60; ...@@ -72,20 +72,24 @@ export const revalidate = 60;
export const dynamicParams = true // or false, to 404 on unknown paths export const dynamicParams = true // or false, to 404 on unknown paths
export async function generateStaticParams() { export async function generateStaticParams() {
const posts: Post[] = await fetch('https://api.vercel.app/blog').then((res) => res.json()); const posts = await fetch('https://api.vercel.app/blog').then((res) =>
res.json()
)
return posts.map((post) => ({ return posts.map((post) => ({
id: String(post.id), id: String(post.id),
})); }))
} }
export default async function Page({ params }) { export default async function Page({ params }) {
const post = await fetch(`https://api.vercel.app/blog/${params.id}`).then((res) => res.json()); const post = await fetch(`https://api.vercel.app/blog/${params.id}`).then(
(res) => res.json()
)
return ( return (
<main> <main>
<h1>{post.title}</h1> <h1>{post.title}</h1>
<p>{post.content}</p> <p>{post.content}</p>
</main> </main>
); )
} }
``` ```
...@@ -94,7 +98,7 @@ export default async function Page({ params }) { ...@@ -94,7 +98,7 @@ export default async function Page({ params }) {
<PagesOnly> <PagesOnly>
```tsx filename="pages/blog/[id].tsx" switcher ```tsx filename="pages/blog/[id].tsx" switcher
import { GetStaticPaths, GetStaticProps } from 'next' import type { GetStaticPaths, GetStaticProps } from 'next'
interface Post { interface Post {
id: string id: string
...@@ -125,7 +129,7 @@ export const getStaticProps: GetStaticProps<Props> = async ({ ...@@ -125,7 +129,7 @@ export const getStaticProps: GetStaticProps<Props> = async ({
}: { }: {
params: { id: string } params: { id: string }
}) => { }) => {
let post = await fetch(`https://api.vercel.app/blog/${params.id}`).then( const post = await fetch(`https://api.vercel.app/blog/${params.id}`).then(
(res) => res.json() (res) => res.json()
) )
...@@ -228,7 +232,32 @@ Here's how this example works: ...@@ -228,7 +232,32 @@ Here's how this example works:
This fetches and displays a list of blog posts on `/blog`. After an hour, the cache for this page is invalidated on the next visit to the page. Then, in the background, a new version of the page is generated with the latest blog posts. This fetches and displays a list of blog posts on `/blog`. After an hour, the cache for this page is invalidated on the next visit to the page. Then, in the background, a new version of the page is generated with the latest blog posts.
```jsx ```tsx filename="app/blog/page.tsx" switcher
interface Post {
id: string
title: string
content: string
}
export const revalidate = 3600 // invalidate every hour
export default async function Page() {
const data = await fetch('https://api.vercel.app/blog')
const posts: Post[] = await data.json()
return (
<main>
<h1>Blog Posts</h1>
<ul>
{posts.map((post) => (
<li key={post.id}>{post.title}</li>
))}
</ul>
</main>
)
}
```
```jsx filename="app/blog/page.js" switcher
export const revalidate = 3600 // invalidate every hour export const revalidate = 3600 // invalidate every hour
export default async function Page() { export default async function Page() {
...@@ -255,7 +284,18 @@ For a more precise method of revalidation, invalidate pages on-demand with the ` ...@@ -255,7 +284,18 @@ For a more precise method of revalidation, invalidate pages on-demand with the `
For example, this Server Action would get called after adding a new post. Regardless of how you retrieve your data in your Server Component, either using `fetch` or connecting to a database, this will clear the cache for the entire route and allow the Server Component to fetch fresh data. For example, this Server Action would get called after adding a new post. Regardless of how you retrieve your data in your Server Component, either using `fetch` or connecting to a database, this will clear the cache for the entire route and allow the Server Component to fetch fresh data.
```jsx ```ts filename="app/actions.ts" switcher
'use server'
import { revalidatePath } from 'next/cache'
export async function createPost() {
// Invalidate the /posts route in the cache
revalidatePath('/posts')
}
```
```js filename="app/actions.js" switcher
'use server' 'use server'
import { revalidatePath } from 'next/cache' import { revalidatePath } from 'next/cache'
...@@ -272,7 +312,17 @@ export async function createPost() { ...@@ -272,7 +312,17 @@ export async function createPost() {
For most use cases, prefer revalidating entire paths. If you need more granular control, you can use the `revalidateTag` function. For example, you can tag individual `fetch` calls: For most use cases, prefer revalidating entire paths. If you need more granular control, you can use the `revalidateTag` function. For example, you can tag individual `fetch` calls:
```jsx ```tsx filename="app/blog/page.tsx" switcher
export default async function Page() {
const data = await fetch('https://api.vercel.app/blog', {
next: { tags: ['posts'] },
})
const posts = await data.json()
// ...
}
```
```jsx filename="app/blog/page.js" switcher
export default async function Page() { export default async function Page() {
const data = await fetch('https://api.vercel.app/blog', { const data = await fetch('https://api.vercel.app/blog', {
next: { tags: ['posts'] }, next: { tags: ['posts'] },
...@@ -284,7 +334,25 @@ export default async function Page() { ...@@ -284,7 +334,25 @@ export default async function Page() {
If you are using an ORM or connecting to a database, you can use `unstable_cache`: If you are using an ORM or connecting to a database, you can use `unstable_cache`:
```jsx ```tsx filename="app/blog/page.tsx" switcher
import { unstable_cache } from 'next/cache'
import { db, posts } from '@/lib/db'
const getCachedPosts = unstable_cache(
async () => {
return await db.select().from(posts)
},
['posts'],
{ revalidate: 3600, tags: ['posts'] }
)
export default async function Page() {
const posts = getCachedPosts()
// ...
}
```
```jsx filename="app/blog/page.js" switcher
import { unstable_cache } from 'next/cache' import { unstable_cache } from 'next/cache'
import { db, posts } from '@/lib/db' import { db, posts } from '@/lib/db'
...@@ -304,7 +372,18 @@ export default async function Page() { ...@@ -304,7 +372,18 @@ export default async function Page() {
You can then use `revalidateTag` in a [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) or [Route Handler](/docs/app/building-your-application/routing/route-handlers): You can then use `revalidateTag` in a [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) or [Route Handler](/docs/app/building-your-application/routing/route-handlers):
```jsx ```ts filename="app/actions.ts" switcher
'use server'
import { revalidateTag } from 'next/cache'
export async function createPost() {
// Invalidate all data tagged with 'posts' in the cache
revalidateTag('posts')
}
```
```js filename="app/actions.js" switcher
'use server' 'use server'
import { revalidateTag } from 'next/cache' import { revalidateTag } from 'next/cache'
...@@ -325,8 +404,8 @@ For a more precise method of revalidation, use `res.revalidate` to generate a ne ...@@ -325,8 +404,8 @@ For a more precise method of revalidation, use `res.revalidate` to generate a ne
For example, this API Route can be called at `/api/revalidate?secret=<token>` to revalidate a given blog post. Create a secret token only known by your Next.js app. This secret will be used to prevent unauthorized access to the revalidation API Route. For example, this API Route can be called at `/api/revalidate?secret=<token>` to revalidate a given blog post. Create a secret token only known by your Next.js app. This secret will be used to prevent unauthorized access to the revalidation API Route.
```ts filename="pages/api/revalidate.ts" ```ts filename="pages/api/revalidate.ts" switcher
import { NextApiRequest, NextApiResponse } from 'next' import type { NextApiRequest, NextApiResponse } from 'next'
export default async function handler( export default async function handler(
req: NextApiRequest, req: NextApiRequest,
...@@ -350,6 +429,26 @@ export default async function handler( ...@@ -350,6 +429,26 @@ export default async function handler(
} }
``` ```
```js filename="pages/api/revalidate.js" switcher
export default async function handler(req, res) {
// Check for secret to confirm this is a valid request
if (req.query.secret !== process.env.MY_SECRET_TOKEN) {
return res.status(401).json({ message: 'Invalid token' })
}
try {
// This should be the actual path not a rewritten path
// e.g. for "/posts/[id]" this should be "/posts/1"
await res.revalidate('/posts/1')
return res.json({ revalidated: true })
} catch (err) {
// If there was an error, Next.js will continue
// to show the last successfully generated page
return res.status(500).send('Error revalidating')
}
}
```
If you are using on-demand revalidation, you do not need to specify a `revalidate` time inside of `getStaticProps`. Next.js will use the default value of `false` (no revalidation) and only revalidate the page on-demand when `res.revalidate()` is called. If you are using on-demand revalidation, you do not need to specify a `revalidate` time inside of `getStaticProps`. Next.js will use the default value of `false` (no revalidation) and only revalidate the page on-demand when `res.revalidate()` is called.
</PagesOnly> </PagesOnly>
...@@ -366,13 +465,53 @@ If an error is thrown while attempting to revalidate data, the last successfully ...@@ -366,13 +465,53 @@ If an error is thrown while attempting to revalidate data, the last successfully
If there is an error inside `getStaticProps` when handling background regeneration, or you manually throw an error, the last successfully generated page will continue to show. On the next subsequent request, Next.js will retry calling `getStaticProps`. If there is an error inside `getStaticProps` when handling background regeneration, or you manually throw an error, the last successfully generated page will continue to show. On the next subsequent request, Next.js will retry calling `getStaticProps`.
```tsx filename="pages/blog/[id].tsx" switcher
import type { GetStaticProps } from 'next'
interface Post {
id: string
title: string
content: string
}
interface Props {
post: Post
}
export const getStaticProps: GetStaticProps<Props> = async ({
params,
}: {
params: { id: string }
}) => {
// If this request throws an uncaught error, Next.js will
// not invalidate the currently shown page and
// retry getStaticProps on the next request.
const res = await fetch(`https://api.vercel.app/blog/${params.id}`)
const post: Post = await res.json()
if (!res.ok) {
// If there is a server error, you might want to
// throw an error instead of returning so that the cache is not updated
// until the next successful request.
throw new Error(`Failed to fetch posts, received status ${res.status}`)
}
return {
props: { post },
// Next.js will invalidate the cache when a
// request comes in, at most once every 60 seconds.
revalidate: 60,
}
}
```
```jsx filename="pages/blog/[id].jsx" switcher ```jsx filename="pages/blog/[id].jsx" switcher
export async function getStaticProps({ params }) { export async function getStaticProps({ params }) {
// If this request throws an uncaught error, Next.js will // If this request throws an uncaught error, Next.js will
// not invalidate the currently shown page and // not invalidate the currently shown page and
// retry getStaticProps on the next request. // retry getStaticProps on the next request.
const res = await fetch(`https://api.vercel.app/blog/${params.id}`) const res = await fetch(`https://api.vercel.app/blog/${params.id}`)
const posts = await res.json() const post = await res.json()
if (!res.ok) { if (!res.ok) {
// If there is a server error, you might want to // If there is a server error, you might want to
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment