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:
co-authored by
Bartek Iwańczuk
Claude Opus 4.6
parent
8b0d42b82a
commit
6dd2c97fb2
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user