Update revalidatePath.mdx (#54631)

add if statement, because typescript throws error: revalidatePath require `string`, but `searchParams.get('path')` returns `string|null` type




Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
This commit is contained in:
Krychaxp 2023-08-28 22:33:49 +02:00 committed by GitHub
parent 0f822373ee
commit bdfbde5db8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -41,14 +41,23 @@ export default async function submit() {
### Route Handler
```ts filename="app/api/revalidate/route.ts" switcher
import { NextRequest, NextResponse } from 'next/server'
import { revalidatePath } from 'next/cache'
import { NextRequest, NextResponse } from 'next/server'
export async function GET(request: NextRequest) {
const path = request.nextUrl.searchParams.get('path')
if (path) {
revalidatePath(path)
return NextResponse.json({ revalidated: true, now: Date.now() })
}
return NextResponse.json({
revalidated: false,
now: Date.now(),
message: 'Missing path to revalidate',
})
}
```
```js filename="app/api/revalidate/route.js" switcher
@ -57,7 +66,16 @@ import { revalidatePath } from 'next/cache'
export async function GET(request) {
const path = request.nextUrl.searchParams.get('path')
if (path) {
revalidatePath(path)
return NextResponse.json({ revalidated: true, now: Date.now() })
}
return NextResponse.json({
revalidated: false,
now: Date.now(),
message: 'Missing path to revalidate',
})
}
```