feat:Actually ship no JS by default (#3696)

In many places, Fresh marketing claims the framework ships no JS by
default, **this was false**.

This PR reworks core parts of the framework to check whether JS is
needed on the client with these criteria:
1. Use of islands
2. Use of Partials (`f-client-nav`)

If either of those those criteria are met, JavaScript will be loaded as
normal, but if both are false, then the `client-entry` script is omitted
from the page.

Fixes https://github.com/denoland/fresh/issues/3662

---------

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jeroen Akkerman
2026-03-29 20:15:35 +00:00
committed by GitHub
co-authored by Bartek Iwańczuk Claude Opus 4.6
parent 8b0d42b82a
commit 6dd2c97fb2
10 changed files with 416 additions and 154 deletions
+26 -17
View File
@@ -357,25 +357,34 @@ export class Context<State> {
}
throw err;
} finally {
// Add preload headers
// Add preload headers only when client JS is actually emitted.
const basePath = this.config.basePath;
const runtimeUrl = state.buildCache.clientEntry.startsWith(".")
? state.buildCache.clientEntry.slice(1)
: state.buildCache.clientEntry;
let link = `<${
encodeURI(`${basePath}${runtimeUrl}`)
}>; rel="modulepreload"; as="script"`;
state.islands.forEach((island) => {
const specifier = `${basePath}${
island.file.startsWith(".") ? island.file.slice(1) : island.file
}`;
link += `, <${
encodeURI(specifier)
}>; rel="modulepreload"; as="script"`;
});
const linkParts: string[] = [];
if (link !== "") {
headers.append("Link", link);
if (
state.needsClientRuntime ||
state.buildCache.hmrClientEntry !== undefined
) {
const runtimeUrl = state.buildCache.clientEntry.startsWith(".")
? state.buildCache.clientEntry.slice(1)
: state.buildCache.clientEntry;
linkParts.push(
`<${
encodeURI(`${basePath}${runtimeUrl}`)
}>; rel="modulepreload"; as="script"`,
);
state.islands.forEach((island) => {
const specifier = `${basePath}${
island.file.startsWith(".") ? island.file.slice(1) : island.file
}`;
linkParts.push(
`<${encodeURI(specifier)}>; rel="modulepreload"; as="script"`,
);
});
}
if (linkParts.length > 0) {
headers.append("Link", linkParts.join(", "));
}
renderNonce = state.nonce;