The Dekimu site now prefetches the next page when a visitor hovers or presses on a link, using speculation rules served as an external file referenced from a response header. Because the browser treats that file as same-origin script, the content security policy did not need to change.
Speculation rules can be written as an inline script block. That is the obvious route, and it needs a dedicated keyword added to the script policy. For a site that works to keep that policy tight, a new allowance for the sake of faster navigation is a poor trade.
The alternative is a response header that points at a rules file. The file is fetched like any other same-origin script, so a policy that already allows its own origin covers it. A test asserts that the policy contains no new keyword and no wider script sources, so a later edit cannot loosen it by accident.
The rule covers same-site links at moderate eagerness, meaning the browser acts on hover or pointer-down rather than loading everything in view. It is prefetch only. Nothing prerenders, so no page script runs for a click that never happens.
API routes are excluded, and the header is not sent on them. Some of those are GET requests with side effects, such as confirming or cancelling a newsletter subscription, and a speculative fetch must never trigger one. Links marked nofollow and links that open in a new tab are skipped too.
A speculative request is still a real request. Decide what must never be fetched on a guess before deciding what to fetch.
The header and content type were verified on the production site. In headless Chrome, a hover triggered the prefetch and the later navigation used the prefetched document. A test pins the rule's shape: prefetch only, an explicit document source for older browsers, and the exclusions above.
Three things are not established. We have not measured the effect on real visitors. Client-side navigation through framework link components does not use speculation rules, so those clicks see no change. And browsers without support simply ignore the header, which is the intended fallback.
Other apps use different script policies, and some rely on per-request nonces with strict-dynamic, which can ignore a same-origin allowance. Each needs its own check before the same header is added. The rule is to read each app's policy and test the header first, not to copy the setting across.
This post was drafted by an AI system from Dekimu's public engineering record and published with automated checks, without per-post human editing.
← Back to blog