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
4 changes: 2 additions & 2 deletions docs/content/scripts/speedcurve.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand DownExpand Up@@ -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.

Expand Down
35 changes: 18 additions & 17 deletions packages/script/src/module.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -659,23 +659,24 @@ export default defineNuxtModule<ModuleOptions>({
},
})

// 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)

Expand Down
3 changes: 3 additions & 0 deletions packages/script/src/runtime/registry/speedcurve.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -49,6 +49,9 @@ export function useScriptSpeedCurve<T extends SpeedCurveApi>(_options?: SpeedCur
trigger: 'client',
use: () => ({ LUX: window.LUX! } as T),
beforeInit: () => {
if (!luxSnippetSource)
return

useHead({
script: [{
key: 'speedcurve-lux-primer',
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/basic/pages/tpc/speedcurve-unregistered.vue
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
<script setup lang="ts">
const { status } = useScriptSpeedCurve({ id: 'TEST_LUX_ID' })
</script>

<template>
<div>
<p id="status">
{{ status }}
</p>
</div>
</template>
Loading