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
+2
View File
@@ -35,6 +35,8 @@ export interface BuildCache<State = any> {
root: string;
islandRegistry: ServerIslandRegistry;
clientEntry: string;
/** Pathname for the HMR-only chunk (development only). Undefined in production. */
hmrClientEntry?: string;
features: {
errorOverlay: boolean;
};
+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;
+14
View File
@@ -314,6 +314,13 @@ export class Builder<State = any> {
"fresh-runtime": new URL(runtimePath, import.meta.url).href,
};
if (dev) {
entryPoints["fresh-hmr"] = new URL(
"../runtime/client/dev_hmr.ts",
import.meta.url,
).href;
}
const namer = new UniqueNamer();
for (const spec of this.#islandSpecifiers) {
const specName = specToName(spec);
@@ -353,6 +360,13 @@ export class Builder<State = any> {
buildCache.islandModNameToChunk.get(name)!.browser = pathname;
}
if (dev) {
const hmrChunkName = output.entryToChunk.get("fresh-hmr");
if (hmrChunkName !== undefined) {
buildCache.hmrClientEntry = `${prefix}${hmrChunkName}`;
}
}
for (let i = 0; i < output.files.length; i++) {
const file = output.files[i];
const pathname = `${prefix}${file.path}`;
@@ -63,6 +63,7 @@ export class MemoryBuildCache<State> implements DevBuildCache<State> {
root: string;
islandRegistry: ServerIslandRegistry = new Map();
clientEntry: string;
hmrClientEntry: string | undefined = undefined;
features = { errorOverlay: false };
constructor(
@@ -236,6 +237,7 @@ export class DiskBuildCache<State> implements DevBuildCache<State> {
root: string;
islandRegistry: ServerIslandRegistry = new Map();
clientEntry: string = "";
hmrClientEntry: string | undefined = undefined;
features = { errorOverlay: false };
constructor(
+1 -134
View File
@@ -1,136 +1,3 @@
import "preact/debug";
export * from "./mod.ts";
import { IS_BROWSER } from "../shared.ts";
let ws: WebSocket;
let revision = 0;
let reconnectTimer: number;
const backoff = [
// Wait 100ms initially, because we could also be
// disconnected because of a form submit.
100,
150,
200,
250,
300,
350,
400,
450,
500,
500,
605,
750,
1000,
1250,
1500,
1750,
2000,
];
let backoffIdx = 0;
function reconnect() {
if (ws.readyState !== ws.CLOSED) return;
reconnectTimer = setTimeout(() => {
if (backoffIdx === 0) {
// deno-lint-ignore no-console
console.log(
`%c Fresh %c Connection closed. Trying to reconnect...`,
"background-color: #86efac; color: black",
"color: inherit",
);
}
backoffIdx++;
try {
connect();
clearTimeout(reconnectTimer);
} catch (_err) {
reconnect();
}
}, backoff[Math.min(backoffIdx, backoff.length - 1)]);
}
function onOpenWs() {
backoffIdx = 0;
}
function onCloseWs() {
disconnect();
reconnect();
}
function connect() {
const url = new URL("/_frsh/alive", location.origin.replace("http", "ws"));
ws = new WebSocket(
url,
);
ws.addEventListener("open", onOpenWs);
ws.addEventListener("close", onCloseWs);
ws.addEventListener("message", handleMessage);
ws.addEventListener("error", handleError);
}
function disconnect() {
ws.removeEventListener("open", onOpenWs);
ws.removeEventListener("close", onCloseWs);
ws.removeEventListener("message", handleMessage);
ws.removeEventListener("error", handleError);
ws.close();
}
function handleMessage(e: MessageEvent) {
const data = JSON.parse(e.data);
switch (data.type) {
case "initial-state": {
if (revision === 0) {
// deno-lint-ignore no-console
console.log(
`%c Fresh %c Connected to development server.`,
"background-color: #86efac; color: black",
"color: inherit",
);
}
if (revision === 0) {
revision = data.revision;
} else if (revision < data.revision) {
disconnect();
// Needs reload
location.reload();
}
}
}
}
function handleError(e: Event) {
// TODO
// deno-lint-ignore no-explicit-any
if (e && (e as any).code === "ECONNREFUSED") {
setTimeout(connect, 1000);
}
}
if (IS_BROWSER) {
connect();
addEventListener("message", (ev) => {
if (ev.origin !== location.origin) return;
if (typeof ev.data !== "string" || ev.data !== "close-error-overlay") {
return;
}
document.querySelector("#fresh-error-overlay")?.remove();
});
// Disconnect when the tab becomes inactive and re-connect when it
// becomes active again
addEventListener("visibilitychange", () => {
if (document.hidden) {
disconnect();
} else {
connect();
}
});
}
export * from "./dev_hmr.ts";
@@ -0,0 +1,134 @@
import { IS_BROWSER } from "../shared.ts";
let ws: WebSocket;
let revision = 0;
let reconnectTimer: number;
const backoff = [
// Wait 100ms initially, because we could also be
// disconnected because of a form submit.
100,
150,
200,
250,
300,
350,
400,
450,
500,
500,
605,
750,
1000,
1250,
1500,
1750,
2000,
];
let backoffIdx = 0;
function reconnect() {
if (ws.readyState !== ws.CLOSED) return;
reconnectTimer = setTimeout(() => {
if (backoffIdx === 0) {
// deno-lint-ignore no-console
console.log(
`%c Fresh %c Connection closed. Trying to reconnect...`,
"background-color: #86efac; color: black",
"color: inherit",
);
}
backoffIdx++;
try {
connect();
clearTimeout(reconnectTimer);
} catch (_err) {
reconnect();
}
}, backoff[Math.min(backoffIdx, backoff.length - 1)]);
}
function onOpenWs() {
backoffIdx = 0;
}
function onCloseWs() {
disconnect();
reconnect();
}
function connect() {
const url = new URL("/_frsh/alive", location.origin.replace("http", "ws"));
ws = new WebSocket(
url,
);
ws.addEventListener("open", onOpenWs);
ws.addEventListener("close", onCloseWs);
ws.addEventListener("message", handleMessage);
ws.addEventListener("error", handleError);
}
function disconnect() {
ws.removeEventListener("open", onOpenWs);
ws.removeEventListener("close", onCloseWs);
ws.removeEventListener("message", handleMessage);
ws.removeEventListener("error", handleError);
ws.close();
}
function handleMessage(e: MessageEvent) {
const data = JSON.parse(e.data);
switch (data.type) {
case "initial-state": {
if (revision === 0) {
// deno-lint-ignore no-console
console.log(
`%c Fresh %c Connected to development server.`,
"background-color: #86efac; color: black",
"color: inherit",
);
}
if (revision === 0) {
revision = data.revision;
} else if (revision < data.revision) {
disconnect();
// Needs reload
location.reload();
}
}
}
}
function handleError(e: Event) {
// TODO
// deno-lint-ignore no-explicit-any
if (e && (e as any).code === "ECONNREFUSED") {
setTimeout(connect, 1000);
}
}
if (IS_BROWSER) {
connect();
addEventListener("message", (ev) => {
if (ev.origin !== location.origin) return;
if (typeof ev.data !== "string" || ev.data !== "close-error-overlay") {
return;
}
document.querySelector("#fresh-error-overlay")?.remove();
});
// Disconnect when the tab becomes inactive and re-connect when it
// becomes active again
addEventListener("visibilitychange", () => {
if (document.hidden) {
disconnect();
} else {
connect();
}
});
}
@@ -82,6 +82,26 @@ export class RenderState {
renderedHtmlBody = false;
renderedHtmlHead = false;
hasRuntimeScript = false;
/** Set to true when any element in the tree renders f-client-nav="true". */
clientNavEnabled = false;
/**
* True when the page needs Fresh's client runtime (islands, client nav, or
* `<Partial>` regions on a full document). Partial subresponses omit boot;
* `encounteredPartials` must not force runtime for those requests.
*/
get needsClientRuntime(): boolean {
if (this.islands.size > 0 || this.clientNavEnabled) {
return true;
}
if (
!this.ctx.url.searchParams.has(PARTIAL_SEARCH_PARAM) &&
this.encounteredPartials.size > 0
) {
return true;
}
return false;
}
constructor(
// deno-lint-ignore no-explicit-any
@@ -419,6 +439,22 @@ options[OptionsType.DIFF] = (vnode) => {
break;
}
// Detect f-client-nav="true" on any element in the rendered tree.
// We check here in the diff hook (not the vnode hook) so we catch both
// VNodes created inside component functions during rendering AND those
// pre-created in route handlers before setRenderState was called.
//
// The === "true" check relies on the vnode hook having normalized boolean
// f-client-nav on string elements via String(...) (see OptionsType.VNODE).
// Preact invokes the vnode hook before diff for a given VNode, so e.g.
// <html f-client-nav> becomes the string "true" before we run here.
if (
CLIENT_NAV_ATTR in (vnode.props as Record<string, unknown>) &&
(vnode.props as Record<string, unknown>)[CLIENT_NAV_ATTR] === "true"
) {
RENDER_STATE!.clientNavEnabled = true;
}
if (
vnode.key !== undefined &&
(RENDER_STATE!.partialDepth > 0 || hasIslandOwner(RENDER_STATE!, vnode))
@@ -623,6 +659,9 @@ function FreshRuntimeScript() {
const islandArr = Array.from(islands);
// Partial responses only embed __FRSH_STATE__ JSON for the swapped fragment.
// We do not gate on needsClientRuntime: the parent full-document response is
// responsible for loading the client boot when islands or client nav require it.
if (ctx.url.searchParams.has(PARTIAL_SEARCH_PARAM)) {
const islands = islandArr.map((island) => {
return {
@@ -647,7 +686,12 @@ function FreshRuntimeScript() {
},
})
);
} else {
} else if (
RENDER_STATE!.needsClientRuntime ||
buildCache.hmrClientEntry !== undefined
) {
// Full-document boot: islands / partials / client nav, or Vite/HMR dev
// (client entry must load so e.g. CSS side-effect imports run).
const islandImports = islandArr.map((island) => {
const named = island.exportName === "default"
? island.name
@@ -689,6 +733,9 @@ function FreshRuntimeScript() {
)
);
}
// Production static page: no client JS at all.
return buildCache.features.errorOverlay ? h(ShowErrorOverlay, null) : null;
}
export function ShowErrorOverlay() {
+181
View File
@@ -0,0 +1,181 @@
/**
* Tests for the "zero client JS by default" feature.
*
* Fresh should not ship any client JavaScript when a page renders no islands
* and has no element with `f-client-nav="true"`. The full Fresh client runtime
* (fresh-runtime.js + inline boot script) must only appear when actually needed.
*/
import { App } from "fresh";
import { signal } from "@preact/signals";
import { Counter } from "./fixtures_islands/Counter.tsx";
import { expect } from "@std/expect";
import { FakeServer } from "../src/test_utils.ts";
import {
ALL_ISLAND_DIR,
assertNotSelector,
assertSelector,
buildProd,
Doc,
parseHtml,
} from "./test_utils.tsx";
// Build once so all island tests share the same compiled output.
const allIslandCache = await buildProd({ islandDir: ALL_ISLAND_DIR });
function islandApp(): App<unknown> {
const app = new App().get("/", (ctx) => {
const sig = signal(0);
return ctx.render(
<Doc>
<Counter count={sig} />
</Doc>,
);
});
allIslandCache(app);
return app;
}
// ---------------------------------------------------------------------------
// Static pages (no islands, no f-client-nav)
// ---------------------------------------------------------------------------
Deno.test("no-client-js - static page emits no module boot script", async () => {
const app = new App().get("/", (ctx) =>
ctx.render(
<Doc>
<h1>Hello</h1>
</Doc>,
));
const server = new FakeServer(app.handler());
const res = await server.get("/");
const html = await res.text();
const doc = parseHtml(html);
// No inline module script should be present.
assertNotSelector(doc, 'script[type="module"]');
// Confirm the page itself rendered fine.
assertSelector(doc, "h1");
expect(doc.querySelector("h1")?.textContent).toEqual("Hello");
});
Deno.test("no-client-js - static page does not reference fresh-runtime.js", async () => {
const app = new App().get("/", (ctx) =>
ctx.render(
<Doc>
<p>Hi</p>
</Doc>,
));
const server = new FakeServer(app.handler());
const res = await server.get("/");
const html = await res.text();
expect(html).not.toContain("fresh-runtime");
expect(html).not.toContain("boot(");
});
Deno.test("no-client-js - static page has no Link modulepreload header", async () => {
const app = new App().get("/", (ctx) =>
ctx.render(
<Doc>
<p>Hi</p>
</Doc>,
));
const server = new FakeServer(app.handler());
const res = await server.get("/");
expect(res.headers.get("Link")).toBeNull();
});
// ---------------------------------------------------------------------------
// Pages with f-client-nav="true" must still load the client runtime.
// f-client-nav is typically placed in an appWrapper or layout component,
// so the VNode is created INSIDE a component that runs during renderToString
// (and therefore sees an active RENDER_STATE).
// ---------------------------------------------------------------------------
Deno.test("no-client-js - f-client-nav='true' page includes boot script", async () => {
const app = new App()
.appWrapper(({ Component }) => (
<html f-client-nav>
<head>
<meta charset="utf-8" />
</head>
<body>
<Component />
</body>
</html>
))
.get("/", (ctx) => ctx.render(<a href="/other">Link</a>));
const server = new FakeServer(app.handler());
const res = await server.get("/");
const html = await res.text();
const doc = parseHtml(html);
// A module script importing boot must be present.
assertSelector(doc, 'script[type="module"]');
expect(html).toContain("boot(");
});
Deno.test("no-client-js - f-client-nav='false' page emits no boot script", async () => {
const app = new App()
.appWrapper(({ Component }) => (
<html f-client-nav={false}>
<head>
<meta charset="utf-8" />
</head>
<body>
<Component />
</body>
</html>
))
.get("/", (ctx) => ctx.render(<p>Static</p>));
const server = new FakeServer(app.handler());
const res = await server.get("/");
const html = await res.text();
expect(html).not.toContain("boot(");
expect(html).not.toContain("fresh-runtime");
});
// ---------------------------------------------------------------------------
// Pages with islands must still load the client runtime
// ---------------------------------------------------------------------------
Deno.test("no-client-js - island page includes boot script", async () => {
const app = islandApp();
const server = new FakeServer(app.handler());
const res = await server.get("/");
const html = await res.text();
const doc = parseHtml(html);
assertSelector(doc, 'script[type="module"]');
expect(html).toContain("boot(");
});
Deno.test("no-client-js - island page references fresh-runtime.js", async () => {
const app = islandApp();
const server = new FakeServer(app.handler());
const res = await server.get("/");
const html = await res.text();
expect(html).toContain("fresh-runtime");
});
Deno.test("no-client-js - island page has Link modulepreload header", async () => {
const app = islandApp();
const server = new FakeServer(app.handler());
const res = await server.get("/");
const link = res.headers.get("Link");
expect(link).not.toBeNull();
expect(link).toContain("modulepreload");
expect(link).toContain("fresh-runtime");
});
+1
View File
@@ -302,6 +302,7 @@ function isJsMediaType(media: MediaType): boolean {
case MediaType.Unknown:
return false;
}
return false;
}
export type DenoSpecifier = string & { __deno: string };
@@ -203,7 +203,10 @@ Deno.test({
waitUntil: "networkidle2",
});
await page.locator("style[data-vite-dev-id$='style.css']").wait();
// Vite 6: data-vite-dev-id; Vite 7+: vite-module-id on injected style.
await page.locator(
"style[data-vite-dev-id$='style.css'], style[vite-module-id]",
).wait();
});
});
},
@@ -221,7 +224,9 @@ Deno.test({
waitUntil: "networkidle2",
});
await page.locator("style[data-vite-dev-id$='style.css']").wait();
await page.locator(
"style[data-vite-dev-id$='style.css'], style[vite-module-id]",
).wait();
});
});
},