Install using bunx shadcn@latest add @remix-utils/respond-to and bunx shadcn@latest add @remix-utils/parse-accept-header.
If you're building a resource route and wants to send a different response based on what content type the client requested (e.g. send the same data as PDF or XML or JSON), you will need to implement content negotiation, this can be done with the respondTo header.
exportasyncfunctionloader({ request }: Route.LoaderArgs) { // do any work independent of the response type before respondTo letdata = awaitgetData(request);
letheaders = newHeaders({ vary:"accept" });
// Here we will decide how to respond to different content types returnrespondTo(request, { // The handler can be a subtype handler, in `text/html` html is the subtype html() { // We can call any function only really need to respond to this // content-type letbody = ReactDOMServer.renderToString(<UI {...data} />); headers.append("content-type", "text/html"); returnnewResponse(body, { headers }); }, // It can also be a highly specific type async"application/rss+xml"() { // we can do more async work inside this code if needed letbody = awaitgenerateRSSFeed(data); headers.append("content-type", "application/rss+xml"); returnnewResponse(body, { headers }); }, // Or a generic type asynctext() { // To respond to any text type, e.g. text/plain, text/csv, etc. letbody = generatePlain(data); headers.append("content-type", "text/plain"); returnnewResponse(body, { headers }); }, // The default will be used if the accept header doesn't match any of the // other handlers default() { // Here we could have a default type of response, e.g. use json by // default, or we can return a 406 which means the server can't respond // with any of the requested content types returnnewResponse("Not Acceptable", { status:406 }); }, }); }
Now, the respondTo function will check the Accept header and call the correct handler, to know which one to call it will use the parseAcceptHeader function also exported from Remix Utils
The result is an array with the type, subtype and extra params (e.g. the q value). The order will be the same encountered in the header, in the example aabove text/html will be the first, followed by application/xhtml+xml.
This means that the respondTo helper will prioritize any handler that match text/html, in our example above, that will be the html handler, but if we remove it then the text handler will be called instead.67
Install using
bunx shadcn@latest add @remix-utils/respond-toandbunx shadcn@latest add @remix-utils/parse-accept-header.If you're building a resource route and wants to send a different response based on what content type the client requested (e.g. send the same data as PDF or XML or JSON), you will need to implement content negotiation, this can be done with the
respondToheader.Now, the
respondTofunction will check theAcceptheader and call the correct handler, to know which one to call it will use theparseAcceptHeaderfunction also exported from Remix UtilsThe result is an array with the type, subtype and extra params (e.g. the
qvalue). The order will be the same encountered in the header, in the example aabovetext/htmlwill be the first, followed byapplication/xhtml+xml.This means that the
respondTohelper will prioritize any handler that matchtext/html, in our example above, that will be thehtmlhandler, but if we remove it then thetexthandler will be called instead.67Author
Sergio XalambrÃ