Skip to content

Commit

Permalink
add test to check for Nuxt 3 compatibility (old folder structure)
Browse files Browse the repository at this point in the history
  • Loading branch information
dulnan committed Oct 19, 2024
1 parent 7030fcb commit ac19511
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@
"prepack": "nuxt-module-build build",
"dev": "nuxi dev playground",
"dev:playground-disk": "nuxi dev playground-disk",
"dev:playground-nuxt3": "nuxi dev playground-nuxt3",
"dev:build": "nuxi build playground",
"dev:serve": "node playground/.output/server/index.mjs",
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground-disk && nuxi prepare playground",
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground-disk && nuxi prepare playground-nuxt3 && nuxi prepare playground",
"dev:inspect": "nuxi dev playground --inspect",
"typecheck": "nuxi typecheck",
"docs:dev": "vitepress dev docs --port 5000",
Expand Down
5 changes: 5 additions & 0 deletions playground-nuxt3/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div class="app">
<NuxtPage />
</div>
</template>
7 changes: 7 additions & 0 deletions playground-nuxt3/app/multiCache.serverOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineMultiCacheOptions } from './../../src/runtime/serverOptions/defineMultiCacheOptions'

export default defineMultiCacheOptions({
cacheKeyPrefix: (): Promise<string> => {
return Promise.resolve('MY_CACHE_PREFIX_')
},
})
39 changes: 39 additions & 0 deletions playground-nuxt3/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { defineNuxtConfig } from 'nuxt/config'
import NuxtMultiCache from './../src/module'

export default defineNuxtConfig({
modules: [NuxtMultiCache],

imports: {
autoImport: true,
},

multiCache: {
debug: true,
component: {
enabled: true,
},
route: {
enabled: true,
},

data: {
enabled: true,
},

cdn: {
enabled: true,
},
api: {
enabled: true,
cacheTagInvalidationDelay: 5000,
authorization: false,
},
},

compatibilityDate: '2024-10-19',

future: {
compatibilityVersion: 3,
},
})
11 changes: 11 additions & 0 deletions playground-nuxt3/pages/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template>
<div>This is a cached page.</div>
</template>

<script lang="ts" setup>
import { useRouteCache } from '#imports'
useRouteCache((helper) => {
helper.setCacheable()
})
</script>
3 changes: 3 additions & 0 deletions playground-nuxt3/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./.nuxt/tsconfig.json"
}
11 changes: 9 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export default defineNuxtModule<ModuleOptions>({
// Creates the template with runtime server configuration.
const extensions = ['js', 'mjs', 'ts']

const resolvedPath = [
const candidates: string[] = [

Check failure on line 168 in src/module.ts

View workflow job for this annotation

GitHub Actions / build (19.x)

Type '(string | null)[]' is not assignable to type 'string[]'.

Check failure on line 168 in src/module.ts

View workflow job for this annotation

GitHub Actions / build (19.x)

Type '(string | null)[]' is not assignable to type 'string[]'.
'~/multiCache.serverOptions',
'~/app/multiCache.serverOptions',
]
Expand All @@ -174,7 +174,10 @@ export default defineNuxtModule<ModuleOptions>({
.replace(/^(~~|@@)/, nuxt.options.rootDir)
.replace(/^(~|@)/, nuxt.options.srcDir)
})
.map((fullPath) => fileExists(fullPath))[0]
.map((fullPath) => fileExists(fullPath))
.filter(Boolean)

const resolvedPath = candidates[0] as string | undefined

const moduleTypesPath = relative(
nuxt.options.buildDir,
Expand All @@ -184,6 +187,10 @@ export default defineNuxtModule<ModuleOptions>({
const template = (() => {
const maybeUserFile = resolvedPath && fileExists(resolvedPath, extensions)

if (!maybeUserFile) {
logger.warn('No multiCache.serverOptions file found.')
}

const serverOptionsLine = maybeUserFile
? `import serverOptions from '${relative(nuxt.options.buildDir, srcResolver(resolvedPath))}'`
: `const serverOptions = {}`
Expand Down
23 changes: 23 additions & 0 deletions test/nuxt3.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import path from 'path'
import { setup, $fetch } from '@nuxt/test-utils/e2e'
import { describe, expect, test } from 'vitest'
import purgeAll from './__helpers__/purgeAll'

describe('With Nuxt 3 "classic" folder structure', async () => {
await setup({
rootDir: path.resolve(__dirname, './../playground-nuxt3'),
})
test('the serverOptions file is included correctly', async () => {
await purgeAll()

// First call puts it into cache.
await $fetch('/', {
method: 'get',
})

const stats: any = await $fetch('/__nuxt_multi_cache/stats/route')

// If the serverOptions was included correctly, it will set a cache prefix.
expect(stats.rows[0].key).toContain('MY_CACHE_PREFIX_')
})
})

0 comments on commit ac19511

Please sign in to comment.