Caching and production safety
Encoded output must never reach the live site. Invisible characters in production HTML end up in search results, shares and copied text.
Rules
- Encode only in Draft Mode. Pass
context: { encodeSourceMaps: true }only whendraftMode().isEnabled. Never during ISR, static generation orgenerateStaticParams. - Never cache encoded reads. Skip
unstable_cache/"use cache"in Draft Mode, or put the draft state in the cache key. This also applies to platform caches such as OpenNext's incremental cache on Cloudflare. - Mount the overlay only in Draft Mode.
{draft && <VisualEditing />}.
ts
import { draftMode } from 'next/headers'
import { unstable_cache } from 'next/cache'
export async function getPage(slug: string) {
const { isEnabled: draft } = await draftMode()
if (draft) {
// Encoded: never cached
return findPage(slug, { draft: true, context: { encodeSourceMaps: true } })
}
return unstable_cache(() => findPage(slug, { draft: false }), ['page', slug])()
}Who may request encoding
- Local API: the
encodeSourceMapscontext flag is always trusted (server code). - REST:
?encodeSourceMaps=trueonly works on GET requests, and by default only for logged-in users. Change it with the plugin'ssourceMaps.canEncodeoption.
Check your published pages
A quick check that production HTML is clean:
sh
curl -s https://your-site.example/ | grep -cP '[\x{200B}-\x{200F}\x{FEFF}]|data-pve-'The result should be 0. The project's own e2e tests check exactly this.