fix: inject client entry on islands-free pages for HMR (#3810)
Fixes denoland/fresh#3806 --------- Co-authored-by: fibibot <fibibot@users.noreply.github.com> Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
This commit is contained in:
co-authored by
fibibot
Bartek Iwańczuk
parent
1fcf2d9ad5
commit
072d09297e
@@ -17,6 +17,12 @@ export interface FileSnapshot {
|
|||||||
export interface BuildSnapshot<State> {
|
export interface BuildSnapshot<State> {
|
||||||
version: string;
|
version: string;
|
||||||
clientEntry: string;
|
clientEntry: string;
|
||||||
|
/**
|
||||||
|
* When defined, forces the boot script to be emitted in dev so HMR
|
||||||
|
* listeners attach even on island-free pages. The value itself is
|
||||||
|
* currently unused — only its presence matters. Undefined outside of dev.
|
||||||
|
*/
|
||||||
|
hmrClientEntry?: string;
|
||||||
fsRoutes: FsRouteFile<State>[];
|
fsRoutes: FsRouteFile<State>[];
|
||||||
staticFiles: Map<string, FileSnapshot>;
|
staticFiles: Map<string, FileSnapshot>;
|
||||||
islands: ServerIslandRegistry;
|
islands: ServerIslandRegistry;
|
||||||
@@ -51,6 +57,7 @@ export class ProdBuildCache<State> implements BuildCache<State> {
|
|||||||
#snapshot: BuildSnapshot<State>;
|
#snapshot: BuildSnapshot<State>;
|
||||||
islandRegistry: ServerIslandRegistry;
|
islandRegistry: ServerIslandRegistry;
|
||||||
clientEntry: string;
|
clientEntry: string;
|
||||||
|
hmrClientEntry: string | undefined;
|
||||||
features = { errorOverlay: false };
|
features = { errorOverlay: false };
|
||||||
|
|
||||||
constructor(public root: string, snapshot: BuildSnapshot<State>) {
|
constructor(public root: string, snapshot: BuildSnapshot<State>) {
|
||||||
@@ -58,6 +65,7 @@ export class ProdBuildCache<State> implements BuildCache<State> {
|
|||||||
this.#snapshot = snapshot;
|
this.#snapshot = snapshot;
|
||||||
this.islandRegistry = snapshot.islands;
|
this.islandRegistry = snapshot.islands;
|
||||||
this.clientEntry = snapshot.clientEntry;
|
this.clientEntry = snapshot.clientEntry;
|
||||||
|
this.hmrClientEntry = snapshot.hmrClientEntry;
|
||||||
}
|
}
|
||||||
|
|
||||||
getEntryAssets(): string[] {
|
getEntryAssets(): string[] {
|
||||||
|
|||||||
@@ -458,6 +458,7 @@ export async function generateSnapshotServer(
|
|||||||
outDir: string;
|
outDir: string;
|
||||||
buildId: string;
|
buildId: string;
|
||||||
clientEntry: string;
|
clientEntry: string;
|
||||||
|
hmrClientEntry?: string;
|
||||||
islands: IslandModChunk[];
|
islands: IslandModChunk[];
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
fsRoutesFiles: FsRouteFileNoMod<any>[];
|
fsRoutesFiles: FsRouteFileNoMod<any>[];
|
||||||
@@ -524,12 +525,17 @@ export async function generateSnapshotServer(
|
|||||||
const entryAssets = options.entryAssets.map((url) => JSON.stringify(url))
|
const entryAssets = options.entryAssets.map((url) => JSON.stringify(url))
|
||||||
.join(",\n");
|
.join(",\n");
|
||||||
|
|
||||||
|
const hmrClientEntryDecl = options.hmrClientEntry !== undefined
|
||||||
|
? `export const hmrClientEntry = ${JSON.stringify(options.hmrClientEntry)}`
|
||||||
|
: "";
|
||||||
|
|
||||||
return `${EDIT_WARNING}
|
return `${EDIT_WARNING}
|
||||||
import { IslandPreparer } from "fresh/internal";
|
import { IslandPreparer } from "fresh/internal";
|
||||||
${islandImports}
|
${islandImports}
|
||||||
${fsRouteImports}
|
${fsRouteImports}
|
||||||
|
|
||||||
export const clientEntry = ${JSON.stringify(options.clientEntry)}
|
export const clientEntry = ${JSON.stringify(options.clientEntry)}
|
||||||
|
${hmrClientEntryDecl}
|
||||||
export const version = ${JSON.stringify(options.buildId)}
|
export const version = ${JSON.stringify(options.buildId)}
|
||||||
|
|
||||||
export const islands = new Map();
|
export const islands = new Map();
|
||||||
|
|||||||
@@ -165,10 +165,19 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
|
|||||||
const staticFiles: PendingStaticFile[] = [];
|
const staticFiles: PendingStaticFile[] = [];
|
||||||
let islandMods: IslandModChunk[] = [];
|
let islandMods: IslandModChunk[] = [];
|
||||||
let clientEntry = "/@id/fresh:client-entry";
|
let clientEntry = "/@id/fresh:client-entry";
|
||||||
|
let hmrClientEntry: string | undefined;
|
||||||
let buildId = "";
|
let buildId = "";
|
||||||
const entryAssets: string[] = [];
|
const entryAssets: string[] = [];
|
||||||
|
|
||||||
if (isDev && server !== undefined) {
|
if (isDev && server !== undefined) {
|
||||||
|
// Set hmrClientEntry so the SSR runtime always emits a boot script
|
||||||
|
// in dev, even when a page has zero islands. Without this, edits
|
||||||
|
// to island-free routes never trigger a browser reload because the
|
||||||
|
// `fresh:reload` WebSocket listener is never attached. The value
|
||||||
|
// is used as a marker only — its presence is what matters, not
|
||||||
|
// what it points to.
|
||||||
|
hmrClientEntry = clientEntry;
|
||||||
|
|
||||||
for (const id of islands.keys()) {
|
for (const id of islands.keys()) {
|
||||||
const mod = server.environments.client.moduleGraph.getModuleById(
|
const mod = server.environments.client.moduleGraph.getModuleById(
|
||||||
id,
|
id,
|
||||||
@@ -380,6 +389,7 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
|
|||||||
staticFiles,
|
staticFiles,
|
||||||
buildId,
|
buildId,
|
||||||
clientEntry,
|
clientEntry,
|
||||||
|
hmrClientEntry,
|
||||||
entryAssets,
|
entryAssets,
|
||||||
fsRoutesFiles: result.routes,
|
fsRoutesFiles: result.routes,
|
||||||
islands: islandMods,
|
islands: islandMods,
|
||||||
|
|||||||
@@ -93,6 +93,23 @@ integrationTest("vite dev - starts without islands/ dir", async () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Issue: https://github.com/denoland/fresh/issues/3806
|
||||||
|
// Pages without islands must still load the client entry in dev so the
|
||||||
|
// HMR `fresh:reload` listener attaches and route edits trigger a refresh.
|
||||||
|
integrationTest(
|
||||||
|
"vite dev - injects client entry on islands-free pages for HMR",
|
||||||
|
async () => {
|
||||||
|
const fixture = path.join(FIXTURE_DIR, "no_islands");
|
||||||
|
await withDevServer(fixture, async (address) => {
|
||||||
|
const res = await fetch(`${address}/`);
|
||||||
|
const text = await res.text();
|
||||||
|
expect(text).toContain("ok");
|
||||||
|
expect(text).toContain("/@id/fresh:client-entry");
|
||||||
|
expect(text).toMatch(/import\s*\{\s*boot\s*\}/);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
integrationTest("vite dev - starts without routes/ dir", async () => {
|
integrationTest("vite dev - starts without routes/ dir", async () => {
|
||||||
const fixture = path.join(FIXTURE_DIR, "no_routes");
|
const fixture = path.join(FIXTURE_DIR, "no_routes");
|
||||||
await withDevServer(fixture, async (address) => {
|
await withDevServer(fixture, async (address) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user