From 9b66beb5844ceb6333c6a80d71e410ee8610631a Mon Sep 17 00:00:00 2001 From: Jan Hug Date: Fri, 15 Nov 2024 08:17:10 +0100 Subject: [PATCH] use serverDir for multiCache.serverOptions file --- docs/advanced/cache-backend.md | 4 +- docs/features/api.md | 2 +- docs/features/data-cache.md | 2 +- docs/features/route-cache.md | 2 +- docs/overview/server-options.md | 28 +++++--- package-lock.json | 2 + .../multiCache.serverOptions.ts | 0 src/module.ts | 67 +++++++++++-------- src/utils/index.ts | 2 +- 9 files changed, 67 insertions(+), 42 deletions(-) rename playground/{app => server}/multiCache.serverOptions.ts (100%) diff --git a/docs/advanced/cache-backend.md b/docs/advanced/cache-backend.md index 62db8cf..aec1b8d 100644 --- a/docs/advanced/cache-backend.md +++ b/docs/advanced/cache-backend.md @@ -16,7 +16,7 @@ provided by unstorage. ::: code-group -```typescript [~/app/multiCache.serverOptions.ts] +```typescript [~/server/multiCache.serverOptions.ts] import { defineMultiCacheOptions } from 'nuxt-multi-cache/dist/runtime/serverOptions' import redisDriver from 'unstorage/drivers/redis' @@ -43,7 +43,7 @@ object. ::: code-group -```typescript [~/app/multiCache.serverOptions.ts] +```typescript [~/server/multiCache.serverOptions.ts] import { defineMultiCacheOptions } from 'nuxt-multi-cache/dist/runtime/serverOptions' import { defineDriver } from 'unstorage' diff --git a/docs/features/api.md b/docs/features/api.md index 894e5f5..cfa1d0a 100644 --- a/docs/features/api.md +++ b/docs/features/api.md @@ -24,7 +24,7 @@ export default defineNuxtConfig({ ``` ```typescript [multiCache.serverOptions.ts] -// ~/app/multiCache.serverOptions.ts +// ~/server/multiCache.serverOptions.ts import { defineMultiCacheOptions } from 'nuxt-multi-cache/dist/runtime/serverOptions' import { isAuthenticated } from './somewhere' diff --git a/docs/features/data-cache.md b/docs/features/data-cache.md index fb522c3..9c2fb02 100644 --- a/docs/features/data-cache.md +++ b/docs/features/data-cache.md @@ -22,7 +22,7 @@ export default defineNuxtConfig({ ``` ```typescript [multiCache.serverOptions.ts] -// ~/app/multiCache.serverOptions.ts +// ~/server/multiCache.serverOptions.ts import { defineMultiCacheOptions } from 'nuxt-multi-cache/dist/runtime/serverOptions' import myCustomDriver from './somehwere' diff --git a/docs/features/route-cache.md b/docs/features/route-cache.md index f8d6aae..1e0139f 100644 --- a/docs/features/route-cache.md +++ b/docs/features/route-cache.md @@ -25,7 +25,7 @@ export default defineNuxtConfig({ ``` ```typescript [multiCache.serverOptions.ts] -// ~/app/multiCache.serverOptions.ts +// ~/server/multiCache.serverOptions.ts import { defineMultiCacheOptions } from 'nuxt-multi-cache/dist/runtime/serverOptions' import myCustomDriver from './somehwere' diff --git a/docs/overview/server-options.md b/docs/overview/server-options.md index 4982514..ee8f203 100644 --- a/docs/overview/server-options.md +++ b/docs/overview/server-options.md @@ -1,15 +1,25 @@ # Server Options All dynamic configuration is located in a special runtime file located at -`~/app/multiCache.serverOptions.ts`. This file is bundled together with the +`~/server/multiCache.serverOptions.ts`. This file is bundled together with the nitro build in the .output folder. -Create a file called `multiCache.serverOptions.ts` (or js/mjs) inside the `app` -folder in your Nuxt root. +Create a file called `multiCache.serverOptions.ts` (or js/mjs) inside the +`server` folder in your Nuxt root. + +::: info + +In previous versions the file was located in +`~/app/multiCache.serverOptions.ts`. For future compatibility with Nuxt 4 the +path has been changed to the _server dir_, which defaults to `/server`. +The legacy file location is still supported but will be removed in the next +major release. + +::: ::: code-group -```typescript [~/app/multiCache.serverOptions.ts] +```typescript [~/server/multiCache.serverOptions.ts] import { defineMultiCacheOptions } from 'nuxt-multi-cache/dist/runtime/serverOptions' export default defineMultiCacheOptions({ @@ -26,7 +36,7 @@ example a custom cache driver. ::: code-group -```typescript [~/app/multiCache.serverOptions.ts] +```typescript [~/server/multiCache.serverOptions.ts] import { defineMultiCacheOptions } from 'nuxt-multi-cache/dist/runtime/serverOptions' import redisDriver from 'unstorage/drivers/redis' @@ -63,7 +73,7 @@ granted by returning a Promise that resolves to `true` or `false`. ::: code-group -```typescript [~/app/multiCache.serverOptions.ts] +```typescript [~/server/multiCache.serverOptions.ts] import { defineMultiCacheOptions } from 'nuxt-multi-cache/dist/runtime/serverOptions' import { isAuthenticated } from './somewhere' @@ -84,7 +94,7 @@ export default defineMultiCacheOptions({ ::: code-group -```typescript [~/app/multiCache.serverOptions.ts] +```typescript [~/server/multiCache.serverOptions.ts] import { defineMultiCacheOptions } from 'nuxt-multi-cache/dist/runtime/serverOptions' export default defineMultiCacheOptions({ @@ -120,7 +130,7 @@ redis). ::: code-group -```typescript [~/app/multiCache.serverOptions.ts] +```typescript [~/server/multiCache.serverOptions.ts] import { defineMultiCacheOptions } from 'nuxt-multi-cache/dist/runtime/serverOptions' export default defineMultiCacheOptions({ @@ -138,7 +148,7 @@ is if your app responds differently based on the request headers, e.g. ::: code-group -```typescript [~/app/multiCache.serverOptions.ts] +```typescript [~/server/multiCache.serverOptions.ts] import { defineMultiCacheOptions } from 'nuxt-multi-cache/dist/runtime/serverOptions' import { H3Event, getHeader } from 'h3' diff --git a/package-lock.json b/package-lock.json index bb7e75a..d1d4dc3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2510,6 +2510,7 @@ "cpu": [ "x64" ], + "dev": true, "optional": true, "os": [ "linux" @@ -2522,6 +2523,7 @@ "cpu": [ "x64" ], + "dev": true, "optional": true, "os": [ "linux" diff --git a/playground/app/multiCache.serverOptions.ts b/playground/server/multiCache.serverOptions.ts similarity index 100% rename from playground/app/multiCache.serverOptions.ts rename to playground/server/multiCache.serverOptions.ts diff --git a/src/module.ts b/src/module.ts index 2f580ca..50bb874 100644 --- a/src/module.ts +++ b/src/module.ts @@ -21,7 +21,7 @@ import { DEFAULT_CDN_CONTROL_HEADER, DEFAULT_CDN_TAG_HEADER, } from './runtime/settings' -import { logger, fileExists, nonNullable } from './utils' +import { logger, fileExists } from './utils' // Nuxt needs this. export type ModuleOptions = NuxtMultiCacheOptions @@ -70,7 +70,8 @@ export default defineNuxtModule({ const { resolve } = createResolver(metaUrl) const rootDir = nuxt.options.rootDir const srcDir = nuxt.options.srcDir - const srcResolver = createResolver(srcDir).resolve + const srcResolver = createResolver(srcDir) + const rootResolver = createResolver(rootDir) const runtimeDir = fileURLToPath(new URL('./runtime', metaUrl)) nuxt.options.build.transpile.push(runtimeDir) @@ -160,39 +161,51 @@ export default defineNuxtModule({ addServerPlugin(resolve('./runtime/server/plugins/multiCache')) } - // Shamelessly copied and adapted from: - // https://github.com/nuxt-modules/prismic/blob/fd90dc9acaa474f79b8831db5b8f46a9a9f039ca/src/module.ts#L55 - // Creates the template with runtime server configuration. - const extensions = ['js', 'mjs', 'ts'] - - const candidates: string[] = [ - '~/multiCache.serverOptions', - '~/app/multiCache.serverOptions', - ] - .map((aliasPath) => { - return aliasPath - .replace(/^(~~|@@)/, nuxt.options.rootDir) - .replace(/^(~|@)/, nuxt.options.srcDir) - }) - .map((fullPath) => fileExists(fullPath)) - .filter(nonNullable) - - const resolvedPath = candidates[0] as string | undefined - const moduleTypesPath = relative( nuxt.options.buildDir, resolve('./runtime/types.ts'), ) - const template = (() => { - const maybeUserFile = resolvedPath && fileExists(resolvedPath, extensions) + const serverResolver = createResolver(nuxt.options.serverDir) + + const findServerOptions = () => { + // Look for the file in the server directory. + const newPath = serverResolver.resolve('multiCache.serverOptions') + const serverPath = fileExists(newPath) - if (!maybeUserFile) { - logger.warn('No multiCache.serverOptions file found.') + if (serverPath) { + return serverPath } - const serverOptionsLine = maybeUserFile - ? `import serverOptions from '${relative(nuxt.options.buildDir, srcResolver(resolvedPath))}'` + // Possible locations for backwards compatibility. + const candidates: string[] = [ + rootResolver.resolve('multiCache.serverOptions'), + rootResolver.resolve('app/multiCache.serverOptions'), + srcResolver.resolve('multiCache.serverOptions'), + ] + + for (let i = 0; i < candidates.length; i++) { + const path = candidates[i] + const filePath = fileExists(path) + + if (filePath) { + logger.warn( + `The multiCache.serverOptions file should be placed in Nuxt's ("${nuxt.options.serverDir}/multiCache.serverOptions.ts"). The new path will be enforced in the next major release.`, + ) + return filePath + } + } + + logger.info('No multiCache.serverOptions file found.') + } + + const template = (() => { + const resolvedPath = findServerOptions() + const resolvedPathRelative = resolvedPath + ? relative(nuxt.options.buildDir, resolvedPath) + : null + const serverOptionsLine = resolvedPathRelative + ? `import serverOptions from '${resolvedPathRelative}'` : `const serverOptions = {}` return addTemplate({ diff --git a/src/utils/index.ts b/src/utils/index.ts index 54dfb15..b47bc89 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -5,7 +5,7 @@ export const logger = useLogger('nuxt-multi-cache') export const fileExists = ( path?: string, - extensions = ['js', 'ts'], + extensions = ['js', 'ts', 'mts'], ): string | null => { if (!path) { return null