withPayload: scope the client-hint header injection to the admin route instead of '/:path*'
withPayload adds Accept-CH / Vary / Critical-CH: Sec-CH-Prefers-Color-Scheme on source: '/:path*' — every route of the host app (observed in @payloadcms/[email protected], dist/withPayload/withPayload.js). The injection exists so the admin panel can server-render the correct theme without a flash (getRequestTheme reads the hint), which is right — but Critical-CH on public routes makes every cold Chromium navigation discard the response and retry it (an internal 307 to the same URL). For sites that pin a colour scheme and never read the preference, that is a wasted document round trip on every visitor's first page view, and it silently corrupts headless-browser TTFB measurement (the retried response is served from local cache, so the final hop reads ~0 ms).
Since withPayload doesn't currently know the admin route, could the entry be scoped via an option — e.g. accept the admin base path, or a clientHintHeaders: 'admin-only' | 'all' | false — defaulting to today's behaviour for compatibility?
Workaround we ship today: post-process the returned config, moving the injected entry from '/:path*' to '/:path(admin|admin/.*)':
const payloadConfig = withPayload(nextConfig, options)
export default {
...payloadConfig,
headers: async () => scopeClientHintHeadersToAdmin((await payloadConfig.headers?.()) ?? []),
}
Happy to PR the option if the approach is agreeable.
+1 for this. It causes a second request to non-admin routes...
Workaround:
admin: {
theme: "dark", // or any fixed theme
},
/**
* withPayload sends a colour-scheme client hint on every route so the admin
* can follow the OS theme. The admin is pinned to dark, so nothing reads it,
* and its Critical-CH makes Chromium request a visitor's first page twice.
* Upstream: https://github.com/payloadcms/payload/issues/17906
*
* @param {import("next").NextConfig} nextConfig
* @returns {import("next").NextConfig}
*/
function withoutClientHints(nextConfig) {
const { headers } = nextConfig;
return {
...nextConfig,
headers: async () =>
((await headers?.()) ?? []).flatMap((rule) => {
const kept = rule.headers.filter(
({ value }) => value !== "Sec-CH-Prefers-Color-Scheme",
);
return kept.length ? [{ ...rule, headers: kept }] : [];
}),
};
}
// next-intl outermost: it reads basePath and trailingSlash off the config it
// is handed, so it should see the fully assembled one.
export default withNextIntl(
withoutClientHints(
withPayload(config, {
devBundleServerPackages: false,
}),
),
);
We hit this too (@payloadcms/next 3.85.0), on a storefront where most traffic comes from paid-search landings. Every one of those is a first visit, so every Chromium / Android visitor was affected. It showed up in Lighthouse as a 307 Internal Redirect from the landing URL to itself. That was hard to trace back to a response header.
We scoped the three headers to the admin by wrapping the config after withPayload, the same approach as the workaround above:
const COLOR_SCHEME_HINT = /sec-ch-prefers-color-scheme/i;
function scopeColorSchemeHintToAdmin(config) {
const withPayloadHeaders = config.headers;
return {
...config,
async headers() {
const rules = await withPayloadHeaders();
return rules.flatMap((rule) => {
const isHint = (header) => COLOR_SCHEME_HINT.test(header.value);
if (rule.source !== '/:path*' || !rule.headers.some(isHint)) return [rule];
const others = rule.headers.filter((header) => !isHint(header));
return [
...(others.length ? [{ ...rule, headers: others }] : []),
{ ...rule, source: '/admin/:path*', headers: rule.headers.filter(isHint) },
];
});
},
};
}
export default scopeColorSchemeHintToAdmin(withPayload(nextConfig));
The admin keeps its no-flash theme, and public routes stop sending Critical-CH.
Results (mobile Lighthouse, simulated throttling, median of 5 runs):
Production, before and after deploying the change:
| Page | Score | FCP | LCP | Redirect cost (Lighthouse estimate) |
|---|---|---|---|---|
| Landing page A | 65 → 78 | 2.4s → 1.8s | 5.7s → 3.9s | 840ms → 0 |
| Landing page B | 68 → 79 | 2.3s → 1.6s | 5.4s → 3.9s | 870ms → 0 |
Local production builds (next build + next start), same comparison:
| Page | Score | LCP |
|---|---|---|
| Landing page A | 77 → 84 | 4.7s → 4.2s |
| Landing page B | 80 → 88 | 4.3s → 3.8s |
It was the largest single improvement in a round of performance work that also included CSS deduplication, font fallbacks and third-party deferral.
+1 for an option on withPayload. Since the admin route is known from the Payload config, scoping to the admin by default seems safe: public routes never read the hint, so sending Critical-CH there only costs a round trip.
Same here on @payloadcms/next 3.90.2 (and the header is still on /:path* in 3.92.1). It hits our signup landing pages, where nearly every visit is a first visit. Lighthouse puts the extra round trip at about 1 s on mobile and 0.4 s on desktop.
We shipped the same workaround: wrap the config withPayload returns and move the three headers to /admin/:path*. The tests run the real withPayload output through the wrapper, so we'll notice if the rule shape changes. An admin-only default, or the clientHintHeaders option proposed here, would let us drop it.
Hi! I'd like to investigate this issue and contribute a solution.
I understand that the client-hint headers are currently being added to all routes, even though they are primarily needed for the Payload admin panel.
I'd like to explore how this behavior can be scoped to the admin route while maintaining backward compatibility, and add appropriate tests.
Before proceeding, could you please let me know if anyone is currently working on this issue or planning to submit a PR?
Thank you!
Another data point for this, from a small Payload 3.93 + Next 16 site behind Caddy.
Lighthouse mobile LCP was 2.17 s with the withPayload headers. It dropped to 1.15 s with only Critical-CH and Accept-CH stripped from public routes. The static build of the same pages scored 1.08 s. On a cold Chromium navigation the retry costs a whole document round trip.
We're shipping the same workaround as in the description: post-process headers() and move the hint headers (and the Vary entry that names the hint) to the admin route only. A scoped option in withPayload would remove the need for it.