Leaf and Kettle's proxy.ts gives each new visitor a visitor cookie, a random id that the shop's analytics counts page views with. The function is done. But its matcher, /:path*, matches every request, so every script, image, font, and API call gets the extra work and a Set-Cookie header, and many CDNs won't cache a response that sets a cookie. Rewrite config.matcher so proxy.ts runs for page visits only, and leave the function as it is.
It must run for every page, like /, /teas, /teas/mint?ref=mail, /account/orders/1002, and /apiary, the page about the honey from the shop's own bees.
It must not run for the API, which is /api and every path under /api/, or for Next.js's own files, every path under /_next/, like /_next/static/chunks/main.js and /_next/image?url=....
Nor for the metadata files /favicon.ico, /robots.txt, /sitemap.xml, and /manifest.webmanifest, or for images from public in any folder: paths that end in .png, .jpg, .jpeg, .webp, .avif, or .svg, like /images/leaf.png and /teas/mint/photo.jpg.
Nor for prefetches. When a <Link> comes into view, Next.js's router fetches its page ahead of time with a next-router-prefetch header, and some browsers' own prefetches send purpose: prefetch. A prefetch isn't a visit, so skip requests with either.
A matcher entry is a path pattern, or an object with a source pattern and has or missing conditions on headers, cookies, or the query. The tests ask proxyRuns(path, { headers }) whether Next.js would run proxy.ts for a request, using the regular expressions Next.js makes from your matcher.
proxyRuns("/teas/mint") → true
proxyRuns("/apiary") → true
proxyRuns("/api/teas") → false
proxyRuns("/images/leaf.png") → false
proxyRuns("/teas", { headers: { "next-router-prefetch": "1" } }) → false
for (const path of ["/", "/teas", "/teas/mint?ref=mail", "/account/orders/1002", "/sign-in", "/teas/jasmine-pearls"]) expect(proxyRuns(path)).toBe(true);
for (const path of ["/api", "/api/teas", "/api/orders/1002", "/api/webhooks/stock"]) expect(proxyRuns(path)).toBe(false);
expect(proxyRuns("/apiary")).toBe(true);
expect(proxyRuns("/apiary/wildflower-honey")).toBe(true);
for (const path of ["/_next/static/chunks/main.js", "/_next/static/media/leaf.woff2", "/_next/image?url=%2Fimages%2Fleaf.png&w=640&q=75"]) expect(proxyRuns(path)).toBe(false);
for (const path of ["/favicon.ico", "/robots.txt", "/sitemap.xml", "/manifest.webmanifest"]) expect(proxyRuns(path)).toBe(false);
for (const path of ["/images/leaf.png", "/teas/mint/photo.jpg", "/hero.jpeg", "/banners/spring.webp", "/promo.avif", "/logo.svg"]) expect(proxyRuns(path)).toBe(false);
expect(proxyRuns("/teas", { headers: { "next-router-prefetch": "1" } })).toBe(false);
expect(proxyRuns("/teas", { headers: { purpose: "prefetch" } })).toBe(false);
expect(proxyRuns("/teas", { headers: { rsc: "1" } })).toBe(true);
expect(proxyRuns("/teas")).toBe(true);
const first = await proxy(request("/teas"));
expect(first.cookies.get("visitor")?.value).toMatch(/^[0-9a-f-]{36}$/);
const again = await proxy(request("/teas", { headers: { cookie: "visitor=" + first.cookies.get("visitor").value } }));
expect(again.headers.getSetCookie()).toEqual([]);
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).
I'm here to help with Run proxy.ts for pages only. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.