fix(dev): serve the HMR client as an external script
A document's CSP nonce is fixed at load, so an inline script delivered by a later response can never carry a nonce that document accepts. The HMR client is now served at /__wrnexus/hmr-client.js, which script-src 'self' already covers and which needs no nonce at all. This removes one of the two inline scripts CSP was blocking in development. The i18n data script is still blocked and needs the same treatment; it is shared with the CSR navigation and HMR parsers, so moving it spans @wrnexus/i18n, csr, and dev-server. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -34,6 +34,7 @@ import type { AssetServer } from "./runtime.ts";
|
|||||||
import { servePublicAsset } from "./public.ts";
|
import { servePublicAsset } from "./public.ts";
|
||||||
import { servePluginAsset, type ServedPluginAsset } from "./plugin-assets.ts";
|
import { servePluginAsset, type ServedPluginAsset } from "./plugin-assets.ts";
|
||||||
import { serveIslandArtifact, serveWrnBrowserArtifact } from "./pipeline.ts";
|
import { serveIslandArtifact, serveWrnBrowserArtifact } from "./pipeline.ts";
|
||||||
|
import { HMR_CLIENT_HREF, HMR_CLIENT_JS } from "./runtime.ts";
|
||||||
|
|
||||||
/** Style inputs the dev asset server needs to build `/__wrnexus/styles.css`. */
|
/** Style inputs the dev asset server needs to build `/__wrnexus/styles.css`. */
|
||||||
export interface DevStyles {
|
export interface DevStyles {
|
||||||
@@ -99,6 +100,7 @@ export function createDevAssetServer(
|
|||||||
if (pathname.startsWith("/__wrnexus/island/")) {
|
if (pathname.startsWith("/__wrnexus/island/")) {
|
||||||
return serveIslandArtifact(pathname) ?? new Response("Not Found", { status: 404 });
|
return serveIslandArtifact(pathname) ?? new Response("Not Found", { status: 404 });
|
||||||
}
|
}
|
||||||
|
if (pathname === HMR_CLIENT_HREF) return jsResponse(HMR_CLIENT_JS);
|
||||||
if (pathname === "/__wrnexus/islands.js") return jsResponse(getIslandRuntime(true));
|
if (pathname === "/__wrnexus/islands.js") return jsResponse(getIslandRuntime(true));
|
||||||
if (pathname === "/__wrnexus/reactive.js") return jsResponse(getReactiveRuntime(true));
|
if (pathname === "/__wrnexus/reactive.js") return jsResponse(getReactiveRuntime(true));
|
||||||
if (pathname === "/__wrnexus/controllers.js")
|
if (pathname === "/__wrnexus/controllers.js")
|
||||||
|
|||||||
@@ -878,8 +878,19 @@ function randomNonce(): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** The dev HMR client as a nonce-tagged inline script (strict-CSP friendly). */
|
/** The dev HMR client as a nonce-tagged inline script (strict-CSP friendly). */
|
||||||
function hmrClientTag(nonce: string): string {
|
/** Path the dev asset server publishes the HMR client on. */
|
||||||
return `<script nonce="${nonce}">${HMR_CLIENT_JS}</script>`;
|
export const HMR_CLIENT_HREF = "/__wrnexus/hmr-client.js";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The HMR client is served as an external module rather than inlined.
|
||||||
|
*
|
||||||
|
* A document's CSP nonce is fixed at load, so an inline script arriving from a
|
||||||
|
* later response — which is exactly what an HMR reload produces — can never
|
||||||
|
* carry a nonce this document accepts. An external file is covered by
|
||||||
|
* script-src 'self' and needs no nonce at all.
|
||||||
|
*/
|
||||||
|
function hmrClientTag(_nonce: string): string {
|
||||||
|
return `<script src="${HMR_CLIENT_HREF}"></script>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 403 for a rejected cross-site WebSocket handshake. */
|
/** 403 for a rejected cross-site WebSocket handshake. */
|
||||||
|
|||||||
Reference in New Issue
Block a user