Next.js Discord

Discord Forum

Serving cache tags in the response from all API calls.

Unanswered
m0rfarpeter posted this in #help-forum
Open in Discord
Hi.

I need to be able to serve cache tags to a custom cloudfront solution we are creating. But it seems the only place I can write to the response header is inside middleware.ts. But middleware is run before an page is executed which gives me the problem that we are generating content from a lot of API calls througout the page. So inside the navigation we have a API call getting some of the cache tags. And then inside a campaignbar component - both of these comps is inside the layout file. And the page.tsx component itself fetches content to the page that also have cache tags.

I cant seem to find a good solution... had it been with pages directory I would have access to the response within the getServerSideProps.. Or I would be able to send them all with meta tags from the next/head <Head> tag. But all of this doesn't work with app dir.

Right now my best solution is to create a <span className="cache-tag hidden" data-cache-tags="... a lot of cache tags" /> and then let the server on the other side figure it out... or creating a custom node server that would extract them before serving to the client. But all of this seems like hacks. Does anybody have an idea? 🙂

Cheers

4 Replies

Madeiran sardinella
🍪 ?
is that possible? Isn't that very pr user kind of thing? hmmm... maybe it could be a solution... I have to test it out 🙂
Madeiran sardinella
I'd try. Let me know if it works
It doesn’t. Cookies is read only from inside components and pages. Needs to bare sat with server actions or route handlers. Same as with next headers. I read some about it have to do with react being streamed.