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
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,26 @@ jobs:
pnpm vitest run --project e2e "$test_file"
done

windows-cache-driver:
name: Windows cache driver
runs-on: windows-latest
steps:
- uses: actions/checkout@v6
- uses: pnpm/action-setup@v6
- uses: actions/setup-node@v6
with:
node-version: lts/*
cache: pnpm
- run: pnpm i
- run: pnpm --filter @nuxt/scripts dev:prepare
- run: pnpm exec nuxt prepare
- run: pnpm --filter @nuxt/scripts build
- run: pnpm exec vitest run --project unit test/unit/cached-upstream-lifecycle.test.ts
- name: Check dev startup
run: pnpm exec vitest run --project e2e test/e2e/cache-driver-dev.test.ts
- name: Check production build
run: pnpm exec vitest run --project e2e test/e2e/proxy-alias.test.ts -t "runs Nitro runtime helpers locally"

nitro-3:
runs-on: ubuntu-24.04-arm
name: Nitro 3 compatibility
Expand Down
17 changes: 14 additions & 3 deletions packages/script/src/runtime/server/utils/cache-config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { pathToFileURL } from 'node:url'

/** Dedicated Nitro storage mount used by Nuxt Scripts' upstream caches. */
export const NUXT_SCRIPTS_CACHE_BASE = 'nuxt-scripts-cache'

Expand All @@ -11,12 +13,21 @@ export const NUXT_SCRIPTS_CACHE_MAX_ENTRY_SIZE = 8 * 1024 * 1024

interface NitroOptionsWithStorage {
storage?: Record<string, unknown>
alias?: Record<string, string>
}

export function ensureNuxtScriptsCacheStorage(nitroOptions: NitroOptionsWithStorage, driver: string): void {
nitroOptions.storage ||= {}
nitroOptions.storage[NUXT_SCRIPTS_CACHE_BASE] ||= {
driver,
const mounts = (nitroOptions.storage ||= {})
if (mounts[NUXT_SCRIPTS_CACHE_BASE])
return

// Nitro imports custom drivers with the native ESM loader during setup.
const driverURL = pathToFileURL(driver).href
// Rollup needs a local path to bundle the driver into the server output.
nitroOptions.alias ||= {}
nitroOptions.alias[driverURL] = driver
mounts[NUXT_SCRIPTS_CACHE_BASE] = {
driver: driverURL,
max: NUXT_SCRIPTS_CACHE_MAX_ENTRIES,
maxSize: NUXT_SCRIPTS_CACHE_MAX_SIZE,
maxEntrySize: NUXT_SCRIPTS_CACHE_MAX_ENTRY_SIZE,
Expand Down
14 changes: 14 additions & 0 deletions test/e2e/cache-driver-dev.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createResolver } from '@nuxt/kit'
import { $fetch, setup } from '@nuxt/test-utils/e2e'
import { expect, it } from 'vitest'

const { resolve } = createResolver(import.meta.url)

await setup({
rootDir: resolve('../fixtures/proxy-alias'),
dev: true,
})

it('serves requests after loading the default cache driver in development', async () => {
expect(await $fetch('/api/nitro-runtime')).toEqual({ app: true, cached: 'ok', config: true })
})
52 changes: 50 additions & 2 deletions test/unit/cached-upstream-lifecycle.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { execFileSync } from 'node:child_process'
import { mkdtemp, rm, symlink } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
import { createResolver, resolveAlias } from '@nuxt/kit'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import {
ensureNuxtScriptsCacheStorage,
Expand All @@ -19,15 +25,57 @@ describe('upstream cache storage ownership', () => {

it('installs a bounded cache mount without registry server endpoints', () => {
const nitroOptions: { storage?: Record<string, unknown> } = {}
const driver = fileURLToPath(new URL('../../packages/script/src/runtime/server/utils/lru-cache-driver.ts', import.meta.url))

ensureNuxtScriptsCacheStorage(nitroOptions, '/nuxt-scripts/lru-cache-driver')
ensureNuxtScriptsCacheStorage(nitroOptions, driver)

expect(nitroOptions.storage?.[NUXT_SCRIPTS_CACHE_BASE]).toEqual(expect.objectContaining({
driver: '/nuxt-scripts/lru-cache-driver',
max: NUXT_SCRIPTS_CACHE_MAX_ENTRIES,
}))
})

it('loads the cache driver through native ESM from a path with special characters', async () => {
const root = await mkdtemp(join(tmpdir(), 'nuxt-scripts-cache-'))
try {
const driverDir = join(root, 'cache # % ü')
const sourceDir = fileURLToPath(new URL('../../packages/script/src/runtime/server/utils/', import.meta.url))
await symlink(sourceDir, driverDir, 'junction')
const nitroOptions: { storage?: Record<string, { driver: string }> } = {}
ensureNuxtScriptsCacheStorage(nitroOptions, join(driverDir, 'lru-cache-driver.ts'))
const driver = nitroOptions.storage![NUXT_SCRIPTS_CACHE_BASE]!.driver

const value = execFileSync(process.execPath, ['--input-type=module', '-e', `
const { default: createDriver } = await import(process.argv[1])
const cache = createDriver({ max: 2 })
cache.setItem('key', 'value')
process.stdout.write(cache.getItem('key'))
`, driver], { encoding: 'utf8' })

expect(value).toBe('value')
}
finally {
await rm(root, { recursive: true, force: true })
}
})

it('resolves the driver URL to a local file for server bundling', () => {
const driver = createResolver(import.meta.url).resolve('../../packages/script/src/runtime/server/utils/lru-cache-driver.ts')
const nitroOptions: { alias?: Record<string, string> } = {}

ensureNuxtScriptsCacheStorage(nitroOptions, driver)

expect(resolveAlias(pathToFileURL(driver).href, nitroOptions.alias)).toBe(driver)
})

it('preserves an application cache mount', () => {
const mount = { driver: 'redis', url: 'redis://localhost:6379' }
const nitroOptions = { storage: { [NUXT_SCRIPTS_CACHE_BASE]: mount } }

ensureNuxtScriptsCacheStorage(nitroOptions, '/nuxt-scripts/lru-cache-driver')

expect(nitroOptions.storage[NUXT_SCRIPTS_CACHE_BASE]).toBe(mount)
})

it('puts binary proxy payloads in the bounded Nuxt Scripts cache mount', () => {
createCachedBinaryFetch('binary-test', 60)

Expand Down
Loading