diff --git a/docs/content/scripts/speedcurve.md b/docs/content/scripts/speedcurve.md index b5809cddb..7affadd4d 100644 --- a/docs/content/scripts/speedcurve.md +++ b/docs/content/scripts/speedcurve.md @@ -22,7 +22,7 @@ The composable comes with the following defaults: ## Setup -SpeedCurve LUX is opt-in. You **must** register it in `scripts.registry.speedcurve` before calling `useScriptSpeedCurve`, including for per-page usage. Registration triggers the module to resolve and inline the LUX primer at build time. Install the `@speedcurve/lux` peer dep alongside: +SpeedCurve LUX is opt-in. Register it in `scripts.registry.speedcurve` to resolve and inline the LUX primer at build time. Install the `@speedcurve/lux` peer dep alongside: ```bash pnpm add -D @speedcurve/lux @@ -52,7 +52,7 @@ export default defineNuxtConfig({ }) ``` -If `speedcurve` isn't registered, builds fail with an unresolved `#build/nuxt-scripts-speedcurve-snippet` import. If it's registered but `@speedcurve/lux` is missing, the build fails with an install hint. Pinning your own `@speedcurve/lux` version means you control when the primer snippet updates. +If `speedcurve` isn't registered, `useScriptSpeedCurve` builds with an empty primer fallback. If it's registered but `@speedcurve/lux` is missing, the build fails with an install hint. Pinning your own `@speedcurve/lux` version means you control when the primer snippet updates. You can access the `LUX` object as a proxy directly, or await `$script` to get the loaded instance. diff --git a/packages/script/src/module.ts b/packages/script/src/module.ts index 5cef0e34a..12916a98a 100644 --- a/packages/script/src/module.ts +++ b/packages/script/src/module.ts @@ -659,23 +659,24 @@ export default defineNuxtModule({ }, }) - // SpeedCurve requires opt-in via `scripts.registry.speedcurve` plus the - // `@speedcurve/lux` peer dep so the user controls the snippet version. - // Template only emitted on registration; non-registered consumers of - // useScriptSpeedCurve hit a build error from the unresolved virtual. - if (config.registry?.speedcurve) { - addTemplate({ - filename: 'nuxt-scripts-speedcurve-snippet.mjs', - async getContents() { - const snippetPath = await resolvePath('@speedcurve/lux/dist/lux-snippet.js') - if (!existsSync(snippetPath)) { - throw new Error('[nuxt-scripts] useScriptSpeedCurve requires the @speedcurve/lux package. Install it with: npm i -D @speedcurve/lux') - } - const source = readFileSync(snippetPath, 'utf-8') - return `export const luxSnippetSource = ${JSON.stringify(source)}\n` - }, - }) - } + // SpeedCurve's runtime composable statically imports this virtual module. + // Emit an empty fallback unless `scripts.registry.speedcurve` is configured; + // registered users still control the primer version through @speedcurve/lux. + const speedcurveRegistered = !!config.registry?.speedcurve + addTemplate({ + filename: 'nuxt-scripts-speedcurve-snippet.mjs', + async getContents() { + if (!speedcurveRegistered) + return 'export const luxSnippetSource = ""\n' + + const snippetPath = await resolvePath('@speedcurve/lux/dist/lux-snippet.js') + if (!existsSync(snippetPath)) { + throw new Error('[nuxt-scripts] useScriptSpeedCurve requires the @speedcurve/lux package. Install it with: npm i -D @speedcurve/lux') + } + const source = readFileSync(snippetPath, 'utf-8') + return `export const luxSnippetSource = ${JSON.stringify(source)}\n` + }, + }) logger.debug('[nuxt-scripts] Proxy prefix:', proxyPrefix) diff --git a/packages/script/src/runtime/registry/speedcurve.ts b/packages/script/src/runtime/registry/speedcurve.ts index 1013075e7..bfd99acb2 100644 --- a/packages/script/src/runtime/registry/speedcurve.ts +++ b/packages/script/src/runtime/registry/speedcurve.ts @@ -49,6 +49,9 @@ export function useScriptSpeedCurve(_options?: SpeedCur trigger: 'client', use: () => ({ LUX: window.LUX! } as T), beforeInit: () => { + if (!luxSnippetSource) + return + useHead({ script: [{ key: 'speedcurve-lux-primer', diff --git a/test/fixtures/basic/pages/tpc/speedcurve-unregistered.vue b/test/fixtures/basic/pages/tpc/speedcurve-unregistered.vue new file mode 100644 index 000000000..9d1267110 --- /dev/null +++ b/test/fixtures/basic/pages/tpc/speedcurve-unregistered.vue @@ -0,0 +1,11 @@ + + +