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:
parent
0f822373ee
commit
bdfbde5db8
1 changed files with 23 additions and 5 deletions
|
@ -41,14 +41,23 @@ export default async function submit() {
|
||||||
### Route Handler
|
### Route Handler
|
||||||
|
|
||||||
```ts filename="app/api/revalidate/route.ts" switcher
|
```ts filename="app/api/revalidate/route.ts" switcher
|
||||||
import { NextRequest, NextResponse } from 'next/server'
|
|
||||||
import { revalidatePath } from 'next/cache'
|
import { revalidatePath } from 'next/cache'
|
||||||
|
import { NextRequest, NextResponse } from 'next/server'
|
||||||
|
|
||||||
export async function GET(request: NextRequest) {
|
export async function GET(request: NextRequest) {
|
||||||
const path = request.nextUrl.searchParams.get('path')
|
const path = request.nextUrl.searchParams.get('path')
|
||||||
|
|
||||||
|
if (path) {
|
||||||
revalidatePath(path)
|
revalidatePath(path)
|
||||||
return NextResponse.json({ revalidated: true, now: Date.now() })
|
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
|
```js filename="app/api/revalidate/route.js" switcher
|
||||||
|
@ -57,7 +66,16 @@ import { revalidatePath } from 'next/cache'
|
||||||
|
|
||||||
export async function GET(request) {
|
export async function GET(request) {
|
||||||
const path = request.nextUrl.searchParams.get('path')
|
const path = request.nextUrl.searchParams.get('path')
|
||||||
|
|
||||||
|
if (path) {
|
||||||
revalidatePath(path)
|
revalidatePath(path)
|
||||||
return NextResponse.json({ revalidated: true, now: Date.now() })
|
return NextResponse.json({ revalidated: true, now: Date.now() })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
revalidated: false,
|
||||||
|
now: Date.now(),
|
||||||
|
message: 'Missing path to revalidate',
|
||||||
|
})
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
Loading…
Reference in a new issue