Skip to content

Commit

Permalink
use serverDir for multiCache.serverOptions file
Browse files Browse the repository at this point in the history
  • Loading branch information
dulnan committed Nov 15, 2024
1 parent ed2c6af commit 9b66beb
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 42 deletions.
4 changes: 2 additions & 2 deletions docs/advanced/cache-backend.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion docs/features/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion docs/features/data-cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion docs/features/route-cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
28 changes: 19 additions & 9 deletions docs/overview/server-options.md
Original file line number Diff line number Diff line change
@@ -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 `<root>/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({
Expand All @@ -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'

Expand Down Expand Up @@ -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'

Expand All @@ -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({
Expand Down Expand Up @@ -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({
Expand All @@ -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'

Expand Down
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
67 changes: 40 additions & 27 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -70,7 +70,8 @@ export default defineNuxtModule<ModuleOptions>({
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)
Expand Down Expand Up @@ -160,39 +161,51 @@ export default defineNuxtModule<ModuleOptions>({
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 <serverDir> ("${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({
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9b66beb

Please sign in to comment.