Skip to the code

A request id for the page and the browser

When a customer writes to Leaf and Kettle's support, the team wants to find that customer's request in the server logs. So every request gets an id: pages and Route Handlers log it and print it as a support code, and the browser gets it in the response's x-request-id header. Write proxy.ts to hand the ids out.

The shop's load balancer already gives most requests an id in their x-request-id header. Keep that id when it's 8 to 64 characters, each a letter from A to Z in either case, a digit, or a dash. Anything else, or no header at all, gets a new id from crypto.randomUUID(): the header comes from outside, and pages print it.

Hand the id to the app as the request's x-request-id header with NextResponse.next({ request: { headers } }), so a page reads it with (await headers()).get("x-request-id"). Start from a copy of the request's headers, because the pages still need the visitor's cookie and accept-language. Then set the same id on the response, for the browser. The browser must get that header only, never the request's own headers like its cookie.

Next.js carries the request headers you give NextResponse.next() to the app in the response's x-middleware-request-<name> headers, with their names listed in x-middleware-override-headers, so that's where the tests look for what a page will see.

GET /account/orders   Cookie: cart=mint            (no x-request-id)
→ the page's headers():   x-request-id: 9b2c1d7e-4f0a-4c3e-8a51-2d6f0e9b7c41, cookie: cart=mint
  the response:           x-request-id: 9b2c1d7e-4f0a-4c3e-8a51-2d6f0e9b7c41

What the tests check

  1. gives a request a new id, for the app and for the browser
    const response = await proxy(request("/account/orders"));
    const id = response.headers.get("x-request-id");
    expect(id).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);
    expect(response.headers.get("x-middleware-next")).toBe("1");
    expect(response.headers.get("x-middleware-request-x-request-id")).toBe(id);
    
  2. keeps the visitor's other request headers for the app
    const response = await proxy(request("/account/orders", { headers: { cookie: "cart=mint", "accept-language": "fr-FR" } }));
    expect(response.headers.get("x-middleware-request-cookie")).toBe("cart=mint");
    expect(response.headers.get("x-middleware-request-accept-language")).toBe("fr-FR");
    expect(response.headers.get("x-middleware-override-headers").split(",").sort()).toEqual(["accept-language", "cookie", "x-request-id"]);
    
  3. keeps a well-formed id from the load balancer
    for (const id of ["lb-7f3a9c2e", "A1B2C3D4", "x".repeat(64)]) {
      const response = await proxy(request("/teas", { headers: { "x-request-id": id } }));
      expect(response.headers.get("x-request-id")).toBe(id);
      expect(response.headers.get("x-middleware-request-x-request-id")).toBe(id);
    }
    
  4. replaces an id that's too short, too long, or has other characters
    for (const id of ["abc1234", "x".repeat(65), "lb 7f3a9c2e", "<b>hello</b>", "lb_7f3a9c2e"]) {
      const response = await proxy(request("/teas", { headers: { "x-request-id": id } }));
      const given = response.headers.get("x-request-id");
      expect(given).not.toBe(id);
      expect(given).toMatch(/^[0-9a-f-]{36}$/);
      expect(response.headers.get("x-middleware-request-x-request-id")).toBe(given);
    }
    
  5. every request gets its own id
    const ids = new Set();
    for (let n = 0; n < 5; n++) ids.add((await proxy(request("/teas"))).headers.get("x-request-id"));
    expect(ids.size).toBe(5);
    
  6. sends the browser the id, not the request's headers
    const response = await proxy(request("/account", { headers: { cookie: "session=c2Vzc2lvbi0x", "accept-language": "en" } }));
    expect(response.headers.get("x-request-id")).toMatch(/^[0-9a-f-]{36}$/);
    expect(response.headers.get("cookie")).toBeNull();
    expect(response.headers.get("accept-language")).toBeNull();
    

Learn more

proxy.tsSaved in this browser

Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.

Ctrl + Enter runs the tests (⌘ + Enter on a Mac).