Repository navigation
Open
Labels
nitroNitro server engine integrationNitro server engine integrationupstreamIssue caused by a dependency or upstream projectIssue caused by a dependency or upstream project
Description
Describe the feature
Nuxt currently does not support content negotiation for routes where both API (e.g., JSON-LD for ActivityPub) and HTML (Nuxt page rendering) responses are needed based on the client's Accept header. Middleware in server/middleware must not return anything, while event handlers return a value. This makes it difficult to serve both formats from the same route and avoid route duplication.
I would like server route handlers to:
- Return JSON-LD or other content types for API clients (e.g., Accept: application/ld+json)
- If the handler does not return anything or returns undefined, automatically fall back to rendering the corresponding Nuxt page
- Avoid duplicating server and page routes for the same path
Alternatives considered:
- Creating separate API and page routes (leads to duplication and path conflicts)
- Using custom middleware, but this breaks the default event handler model and complicates routing
Example use case: ActivityPub implementation, where /activities/12345 should return JSON-LD for API requests and HTML for browser requests.
Example code:
export default defineEventHandler(async (event) => {
const accept = event.node.req.headers['accept'] || '';
const wantsJson = accept.includes('application/json') ||
accept.includes('application/ld+json')
if (wantsJson) {
// XXX: fetch the data from the server
return {
"@context": ["https://www.w3.org/ns/activitystreams",
{ "@language": "en-US" }],
"id": "https://myserver.com/activities/12345",
"type": "Article",
"name": "Minimal ActivityPub Activity",
"content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
}
}
})Target Nuxt version: Nuxt 3
Scope: Generic for all content-type negotiation, not limited to ActivityPub.
Additional information
- Would you be willing to help implement this feature?
- Could this feature be implemented as a module?
Final checks
- Read the contribution guide.
- Check existing discussions and issues.
Reacted by Nicholai Nissen
Metadata
Metadata
Assignees
Labels
nitroNitro server engine integrationNitro server engine integrationupstreamIssue caused by a dependency or upstream projectIssue caused by a dependency or upstream project
Activity
[-]Content Negotiation Middleware: return JSON-LD or HTML based on Accept header[/-][+]Consider Support Content Negotiation based on Accept header[/+]huang-julien commentedon Jul 19, 2026
Hey 👋
I think that's a nice idea, although, the implementation should be done in nitro. This could be an idea for nitro v3