Next.js Discord

Discord Forum

How to run dynamic side effects Async for a static page

Unanswered
Byte posted this in #help-forum
Open in Discord
Avatar
Hi all,

I'm using Next 15 (app router) and have a simple blog at say: example.com/blog/my-post

I want to use static generation for this route and with IVR as it's a good fit and performance.

However, I also want to run some code as a side effect on every request to my static pages. The side effect is to increment a number in the database to track the number of views the post is getting. It should be async and not block the serving of my static pages.

My goals:
- Keep using static generation with IVR
- avoid the client triggering this logic. I know I can address this issue by firing a request from client side to server API but that can be abused.

From my research it seems middleware might be able to do this but middleware runs quite early on and I'm running into issues using util functions that early I rely on.

Does anyone know if there is a better approach? Ideally a function I could export to tell Next to run this every request anyway even though this is a static page.

Thanks! 🙏

0 Replies