Skip to content

Consider Support Content Negotiation based on Accept header #32756

Description

@comfuture

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

Activity

  1. changed the title [-]Content Negotiation Middleware: return JSON-LD or HTML based on Accept header[/-] [+]Consider Support Content Negotiation based on Accept header[/+] on Jul 25, 2025
  2. added
    nitroNitro server engine integration
    upstreamIssue caused by a dependency or upstream project
    and removed on Jul 19, 2026
  3. huang-julien commented on Jul 19, 2026

    @huang-julien
    Member

    Hey 👋
    I think that's a nice idea, although, the implementation should be done in nitro. This could be an idea for nitro v3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    nitroNitro server engine integrationupstreamIssue caused by a dependency or upstream project

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions