Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 4 additions & 45 deletions packages/react/src/plugin-islands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ import type { PageScanResult } from 'void/pages';
import {
resolveSpecifier,
needsIslandClientBundle,
fileHasCssImports,
extractCssImports,
rebuildIslandCssMap,
computePagesNeedingClientJS,
configureIslandCssServer,
watchPagesForRescan,
rewriteCssImportForVirtualModule,
ISLANDS_RENDERER_ID,
Expand All @@ -34,17 +31,8 @@ export function islandsPlugin(pagesDir: string): Array<Plugin> {
const islandManifest = new Map<string, IslandEntry>();
const islandCssFiles = new Set<string>();
let viteRoot = '';
const islandCssMap = new Map<string, Array<string>>();
let resolvedConfig: ResolvedConfig | null = null;

/** Rebuild islandCssMap from current file contents (re-runs fileHasCssImports). */
function rebuildCssMapLocal(): void {
if (!pageScan) {
return;
}
rebuildIslandCssMap(pageScan, pagesDir, islandCssMap, fileHasCssImports);
}

function computePagesNeedingClientJSLocal(): Set<string> {
if (!pageScan) {
return new Set();
Expand All @@ -62,14 +50,9 @@ export function islandsPlugin(pagesDir: string): Array<Plugin> {
isDev = config.command === 'serve';
},

// Serve virtual island CSS as raw text/css in dev. The client transform
// produces a JS module with `__vite__css = "..."` for HMR — we extract
// the CSS string and serve it directly for render-blocking <link> tags.
configureServer(server) {
configureIslandCssServer(server, islandCssFiles, viteRoot, rebuildCssMapLocal);
watchPagesForRescan(server, pagesDir, root, (scan: PageScanResult) => {
pageScan = scan;
rebuildCssMapLocal();
});
},

Expand Down Expand Up @@ -131,9 +114,6 @@ export function islandsPlugin(pagesDir: string): Array<Plugin> {
islandCssFiles.add(normalizePath(resolve(pagesDir, layout.filePath)));
}
}

// Build CSS map for dev FOUC prevention (only files with CSS imports)
rebuildCssMapLocal();
}
},

Expand Down Expand Up @@ -175,15 +155,7 @@ export function islandsPlugin(pagesDir: string): Array<Plugin> {
mdPlugin?.api?.getClientScripts?.()?.keys() ?? [],
);
const needsJS = computePagesNeedingClientJSLocal();
return generateIslandRenderer(
pageScan,
pagesDir,
isDev,
islandCssMap,
viteRoot,
mdScriptIds,
needsJS,
);
return generateIslandRenderer(pageScan, pagesDir, isDev, mdScriptIds, needsJS);
}
if (id === '\0' + ISLANDS_CLIENT_ENTRY_ID) {
if (!pageScan) {
Expand Down Expand Up @@ -301,8 +273,6 @@ function generateIslandRenderer(
scan: PageScanResult,
pagesDir: string,
isDev: boolean,
islandCssMap: Map<string, Array<string>>,
viteRoot: string,
mdScriptIds: Set<string>,
pagesNeedingClientJS: Set<string>,
): string {
Expand All @@ -317,16 +287,6 @@ function generateIslandRenderer(
</script>`
: '';

// Build CSS link map for dev FOUC prevention
let cssMapCode = '';
if (isDev && islandCssMap.size > 0) {
const cssMapObj: Record<string, Array<string>> = {};
for (const [compId, paths] of islandCssMap) {
cssMapObj[compId] = paths.map((p) => '/' + normalizePath(relative(viteRoot, p)) + '.css');
}
cssMapCode = `const __cssPaths = ${JSON.stringify(cssMapObj)};`;
}

const mdScriptIdsCode = `const __mdClientScriptIds = new Set(${JSON.stringify([...mdScriptIds])});`;
const needsJSCode = `const __pagesNeedingClientJS = new Set(${JSON.stringify([...pagesNeedingClientJS])});`;

Expand All @@ -337,7 +297,6 @@ import { SharedContext, ErrorsContext, ParamsContext } from "@void/react";
import { renderHeadToString, renderHtmlAttrs, renderBodyAttrs } from "void/pages-head";

${generateComponentManifest(scan, pagesDir)}
${cssMapCode}
${mdScriptIdsCode}
${needsJSCode}

Expand Down Expand Up @@ -382,7 +341,7 @@ export async function renderIslandPage(pageObj, assetTags) {

return \`<!doctype html>
<html\${htmlAttrs}>
<head>\${headHtml}\${typeof __cssPaths !== "undefined" ? (__cssPaths[pageObj.component] || []).map(url => '<link rel="stylesheet" href="' + url + '">').join("\\n") : ""}\${PREAMBLE_SCRIPT}\${assetTags.css}\${__pagesNeedingClientJS.has(pageObj.component) ? assetTags.preloads : ""}</head>
<head>\${headHtml}\${PREAMBLE_SCRIPT}\${assetTags.css}\${__pagesNeedingClientJS.has(pageObj.component) ? assetTags.preloads : ""}</head>
<body\${bodyAttrs}>
<div id="app"\${__mdScriptAttr}>\${html}</div>
\${__pagesNeedingClientJS.has(pageObj.component) ? assetTags.body : ""}
Expand All @@ -407,8 +366,8 @@ function generateIslandClientEntry(

lines.push('import { initIslands } from "void/pages-client";');

// In dev, page/layout CSS is served via render-blocking <link> tags
// injected in the SSR HTML. Skip CSS imports here to avoid duplicate injection.
// In dev, first-paint CSS is provided by Void's shared dev CSS collector.
// Skip CSS imports here to avoid duplicate client injection.
// In production, the <link> tags aren't used — the imports below ensure
// the CSS is included in the bundled output.
if (!isDev) {
Expand Down
50 changes: 4 additions & 46 deletions packages/solid/src/plugin-islands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ import {
resolveSpecifier,
needsIslandClientBundle,
rewriteCssImportForVirtualModule,
fileHasCssImports,
extractCssImports,
rebuildIslandCssMap as rebuildIslandCssMapShared,
computePagesNeedingClientJS as computePagesNeedingClientJSShared,
configureIslandCssServer,
watchPagesForRescan,
ISLANDS_RENDERER_ID,
ISLANDS_CLIENT_ENTRY_ID,
Expand Down Expand Up @@ -51,17 +48,8 @@ export function islandsPlugin(pagesDir: string): Array<Plugin> {
const wrapperMeta = new Map<string, WrapperMeta>();
let wrapperId = 0;
let viteRoot = '';
const islandCssMap = new Map<string, Array<string>>();
let resolvedConfig: ResolvedConfig | null = null;

/** Rebuild islandCssMap from current file contents (re-runs fileHasCssImports). */
function rebuildIslandCssMap(): void {
if (!pageScan) {
return;
}
rebuildIslandCssMapShared(pageScan, pagesDir, islandCssMap, fileHasCssImports);
}

function computePagesNeedingClientJS(): Set<string> {
if (!pageScan) {
return new Set();
Expand All @@ -80,14 +68,9 @@ export function islandsPlugin(pagesDir: string): Array<Plugin> {
isDev = config.command === 'serve';
},

// Serve virtual island CSS as raw text/css in dev. The client transform
// produces a JS module with `__vite__css = "..."` for HMR — we extract
// the CSS string and serve it directly for render-blocking <link> tags.
configureServer(server) {
configureIslandCssServer(server, islandCssFiles, viteRoot, rebuildIslandCssMap);
watchPagesForRescan(server, pagesDir, root, (scan: PageScanResult) => {
pageScan = scan;
rebuildIslandCssMap();
});
},

Expand Down Expand Up @@ -149,9 +132,6 @@ export function islandsPlugin(pagesDir: string): Array<Plugin> {
islandCssFiles.add(normalizePath(resolve(pagesDir, layout.filePath)));
}
}

// Build CSS map for dev FOUC prevention (only files with CSS imports)
rebuildIslandCssMap();
}
},

Expand Down Expand Up @@ -202,15 +182,7 @@ export function islandsPlugin(pagesDir: string): Array<Plugin> {
mdPlugin?.api?.getClientScripts?.()?.keys() ?? [],
);
const needsJS = computePagesNeedingClientJS();
return generateIslandRenderer(
pageScan,
pagesDir,
isDev,
islandCssMap,
viteRoot,
mdScriptIds,
needsJS,
);
return generateIslandRenderer(pageScan, pagesDir, mdScriptIds, needsJS);
}
if (id === '\0' + ISLANDS_CLIENT_ENTRY_ID) {
if (!pageScan) {
Expand Down Expand Up @@ -330,22 +302,9 @@ export default function _IslandWrap(props) {
function generateIslandRenderer(
scan: PageScanResult,
pagesDir: string,
isDev: boolean,
islandCssMap: Map<string, Array<string>>,
viteRoot: string,
mdScriptIds: Set<string>,
pagesNeedingClientJS: Set<string>,
): string {
// Build CSS link map for dev FOUC prevention
let cssMapCode = '';
if (isDev && islandCssMap.size > 0) {
const cssMapObj: Record<string, Array<string>> = {};
for (const [compId, paths] of islandCssMap) {
cssMapObj[compId] = paths.map((p) => '/' + normalizePath(relative(viteRoot, p)) + '.css');
}
cssMapCode = `const __cssPaths = ${JSON.stringify(cssMapObj)};`;
}

const mdScriptIdsCode = `const __mdClientScriptIds = new Set(${JSON.stringify([...mdScriptIds])});`;
const needsJSCode = `const __pagesNeedingClientJS = new Set(${JSON.stringify([...pagesNeedingClientJS])});`;

Expand All @@ -356,7 +315,6 @@ import { SharedContext, ErrorsContext, ParamsContext } from "@void/solid";
import { renderHeadToString, renderHtmlAttrs, renderBodyAttrs } from "void/pages-head";

${generateComponentManifest(scan, pagesDir)}
${cssMapCode}
${mdScriptIdsCode}
${needsJSCode}

Expand Down Expand Up @@ -407,7 +365,7 @@ export async function renderIslandPage(pageObj, assetTags) {

return \`<!doctype html>
<html\${htmlAttrs}>
<head>\${headHtml}\${typeof __cssPaths !== "undefined" ? (__cssPaths[pageObj.component] || []).map(url => '<link rel="stylesheet" href="' + url + '">').join("\\n") : ""}\${assetTags.css}\${__pagesNeedingClientJS.has(pageObj.component) ? assetTags.preloads : ""}\${__pagesNeedingClientJS.has(pageObj.component) ? generateHydrationScript() : ""}</head>
<head>\${headHtml}\${assetTags.css}\${__pagesNeedingClientJS.has(pageObj.component) ? assetTags.preloads : ""}\${__pagesNeedingClientJS.has(pageObj.component) ? generateHydrationScript() : ""}</head>
<body\${bodyAttrs}>
<div id="app"\${__mdScriptAttr}>\${html}</div>
\${__pagesNeedingClientJS.has(pageObj.component) ? assetTags.body : ""}
Expand All @@ -431,8 +389,8 @@ function generateIslandClientEntry(
const lines: Array<string> = [];
lines.push('import { initIslands } from "void/pages-client";');

// In dev, page/layout CSS is served via render-blocking <link> tags
// injected in the SSR HTML. Skip CSS imports here to avoid duplicate injection.
// In dev, first-paint CSS is provided by Void's shared dev CSS collector.
// Skip CSS imports here to avoid duplicate client injection.
// In production, the <link> tags aren't used — the imports below ensure
// the CSS is included in the bundled output.
if (!isDev) {
Expand Down
50 changes: 4 additions & 46 deletions packages/svelte/src/plugin-islands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ import {
resolveSpecifier,
needsIslandClientBundle,
rewriteCssImportForVirtualModule,
fileHasCssImports,
extractCssImports,
rebuildIslandCssMap as rebuildIslandCssMapShared,
computePagesNeedingClientJS as computePagesNeedingClientJSShared,
configureIslandCssServer,
watchPagesForRescan,
ISLANDS_RENDERER_ID,
ISLANDS_CLIENT_ENTRY_ID,
Expand Down Expand Up @@ -41,17 +38,8 @@ export function islandsPlugin(pagesDir: string): Array<Plugin> {
const islandCssFiles = new Set<string>();
let isDev = false;
let viteRoot = '';
const islandCssMap = new Map<string, Array<string>>();
let resolvedConfig: ResolvedConfig | null = null;

/** Rebuild islandCssMap from current file contents (re-runs fileHasCssImports). */
function rebuildIslandCssMap(): void {
if (!pageScan) {
return;
}
rebuildIslandCssMapShared(pageScan, pagesDir, islandCssMap, fileHasCssImports);
}

function computePagesNeedingClientJS(): Set<string> {
if (!pageScan) {
return new Set();
Expand All @@ -70,14 +58,9 @@ export function islandsPlugin(pagesDir: string): Array<Plugin> {
isDev = config.command === 'serve';
},

// Serve virtual island CSS as raw text/css in dev. The client transform
// produces a JS module with `__vite__css = "..."` for HMR — we extract
// the CSS string and serve it directly for render-blocking <link> tags.
configureServer(server) {
configureIslandCssServer(server, islandCssFiles, viteRoot, rebuildIslandCssMap);
watchPagesForRescan(server, pagesDir, root, (scan: PageScanResult) => {
pageScan = scan;
rebuildIslandCssMap();
});
},

Expand Down Expand Up @@ -139,9 +122,6 @@ export function islandsPlugin(pagesDir: string): Array<Plugin> {
islandCssFiles.add(normalizePath(resolve(pagesDir, layout.filePath)));
}
}

// Build CSS map for dev FOUC prevention (only files with CSS imports)
rebuildIslandCssMap();
}
},

Expand Down Expand Up @@ -192,15 +172,7 @@ export function islandsPlugin(pagesDir: string): Array<Plugin> {
mdPlugin?.api?.getClientScripts?.()?.keys() ?? [],
);
const needsJS = computePagesNeedingClientJS();
return generateIslandRenderer(
pageScan,
pagesDir,
isDev,
islandCssMap,
viteRoot,
mdScriptIds,
needsJS,
);
return generateIslandRenderer(pageScan, pagesDir, mdScriptIds, needsJS);
}
if (id === '\0' + ISLANDS_CLIENT_ENTRY_ID) {
if (!pageScan) {
Expand Down Expand Up @@ -365,22 +337,9 @@ function generateWrapperComponent(id: string): string {
function generateIslandRenderer(
scan: PageScanResult,
pagesDir: string,
isDev: boolean,
islandCssMap: Map<string, Array<string>>,
viteRoot: string,
mdScriptIds: Set<string>,
pagesNeedingClientJS: Set<string>,
): string {
// Build CSS link map for dev FOUC prevention
let cssMapCode = '';
if (isDev && islandCssMap.size > 0) {
const cssMapObj: Record<string, Array<string>> = {};
for (const [compId, paths] of islandCssMap) {
cssMapObj[compId] = paths.map((p) => '/' + normalizePath(relative(viteRoot, p)) + '.css');
}
cssMapCode = `const __cssPaths = ${JSON.stringify(cssMapObj)};`;
}

const mdScriptIdsCode = `const __mdClientScriptIds = new Set(${JSON.stringify([...mdScriptIds])});`;
const needsJSCode = `const __pagesNeedingClientJS = new Set(${JSON.stringify([...pagesNeedingClientJS])});`;

Expand All @@ -390,7 +349,6 @@ import App from "@void/svelte/App.svelte";
import { renderHeadToString, renderHtmlAttrs, renderBodyAttrs } from "void/pages-head";

${generateComponentManifest(scan, pagesDir)}
${cssMapCode}
${mdScriptIdsCode}
${needsJSCode}

Expand Down Expand Up @@ -425,7 +383,7 @@ export async function renderIslandPage(pageObj, assetTags) {

return \`<!doctype html>
<html\${htmlAttrs}>
<head>\${headHtml}\${typeof __cssPaths !== "undefined" ? (__cssPaths[pageObj.component] || []).map(url => '<link rel="stylesheet" href="' + url + '">').join("\\n") : ""}\${head}\${assetTags.css}\${__pagesNeedingClientJS.has(pageObj.component) ? assetTags.preloads : ""}</head>
<head>\${headHtml}\${head}\${assetTags.css}\${__pagesNeedingClientJS.has(pageObj.component) ? assetTags.preloads : ""}</head>
<body\${bodyAttrs}>
<div id="app"\${__mdScriptAttr}>\${html}</div>
\${__pagesNeedingClientJS.has(pageObj.component) ? assetTags.body : ""}
Expand All @@ -450,8 +408,8 @@ function generateIslandClientEntry(

lines.push('import { initIslands } from "void/pages-client";');

// In dev, page/layout CSS is served via render-blocking <link> tags
// injected in the SSR HTML. Skip CSS imports here to avoid duplicate injection.
// In dev, first-paint CSS is provided by Void's shared dev CSS collector.
// Skip CSS imports here to avoid duplicate client injection.
// In production, the <link> tags aren't used — the imports below ensure
// the CSS is included in the bundled output.
if (!isDev) {
Expand Down
2 changes: 1 addition & 1 deletion packages/void/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@
"drizzle-orm": "^0.45.2",
"drizzle-valibot": "^0.4.2",
"drizzle-zod": "^0.8.3",
"es-module-lexer": "^2.1.0",
"hono": "^4.12.18",
"ignore": "^7.0.5",
"jsonc-parser": "3.3.1",
Expand All @@ -319,7 +320,6 @@
"@types/pg": "^8.20.0",
"@typescript/native-preview": "catalog:",
"arktype": "^2.2.0",
"es-module-lexer": "^2.1.0",
"estree-walker": "^3.0.3",
"magic-string": "^0.30.21",
"ofetch": "2.0.0-alpha.3",
Expand Down
Loading