-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #200 from mobeigi/use-client-side-view-counting
Disable PPR, use client side request to register views
- Loading branch information
Showing
7 changed files
with
68 additions
and
48 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
31 changes: 31 additions & 0 deletions
31
app/src/app/(frontend)/api/custom/posts/[postId]/register-view/route.ts
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,31 @@ | ||
import { NextResponse } from 'next/server'; | ||
import { headers } from 'next/headers'; | ||
import { registerView } from '@/payload/utils/viewCounter'; | ||
|
||
export const POST = async (request: Request, { params: paramsPromise }: { params: Promise<{ postId: string }> }) => { | ||
try { | ||
const params = await paramsPromise; | ||
const { postId: postIdString } = params; | ||
|
||
const postId = Number(postIdString); | ||
|
||
if (isNaN(postId)) { | ||
return NextResponse.json({ error: 'Invalid postId' }, { status: 400 }); | ||
} | ||
|
||
const headerList = await headers(); | ||
const ipAddress = headerList.get('x-forwarded-for'); | ||
const userAgent = headerList.get('user-agent'); | ||
|
||
if (!ipAddress || !userAgent) { | ||
return NextResponse.json({ error: 'Missing headers' }, { status: 400 }); | ||
} | ||
|
||
// Fire async | ||
void registerView({ postId: postId, ipAddress, userAgent }); | ||
return new NextResponse(null, { status: 204 }); | ||
} catch (error) { | ||
console.error('Error registering view:', error); | ||
return NextResponse.json(error, { status: 500 }); | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use client'; | ||
|
||
import { useEffect } from 'react'; | ||
|
||
export const RegisterView = ({ postId }: { postId: number }) => { | ||
useEffect(() => { | ||
void fetch(`/api/custom/posts/${postId}/register-view/`, { | ||
method: 'POST', | ||
}); | ||
}, [postId]); | ||
|
||
return null; | ||
}; |
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 @@ | ||
export { RegisterView as default } from './RegisterView'; |