Skip to content
This repository was archived by the owner on Dec 20, 2023. It is now read-only.

Commit f9687b3

Browse files
fix: check user provided ipx after module loaded
1 parent c112e72 commit f9687b3

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

src/client/components/MediaPreview.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ watchEffect(async () => {
4242
previewState.snippetColors = previewState.stats?.colors?.reduce((acc, color) => {
4343
acc[color] = color
4444
return acc
45-
}, {}) ?? {}
45+
}, {} as Record<string, string>) ?? {}
4646
previewState.alt = ''
4747
previewState.snippetType = 'html'
4848
})

src/client/pages/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const filterExtension = ref('')
7272
const hasFilter = computed(() => Boolean(filterDirectoryKey.value || filterExtension.value))
7373
const assetsKeysFiltered = computed(() => {
7474
if (!hasFilter.value) {
75-
return []
75+
return assets?.value ?? []
7676
}
7777
7878
return (assets?.value ?? [])

src/module.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ type ModuleOptions = {
2424
*/
2525
installIpxMiddleware?: boolean
2626

27+
/**
28+
* @default undefined
29+
*/
30+
hasIpx?: boolean
31+
2732
/**
2833
* @default '/_ipx'
2934
*/
@@ -38,28 +43,31 @@ export default defineNuxtModule<ModuleOptions>({
3843
},
3944
defaults: <ModuleOptions>{
4045
installIpxMiddleware: false,
46+
hasIpx: undefined,
4147
ipxMiddlewarePrefix: '/_ipx'
4248
},
4349
setup (options, nuxt) {
4450
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
4551
const logger = consola.withScope('nuxt:media-viewer')
4652

47-
const hasUserProvidedIPX =
48-
nuxt.options.serverHandlers.find(handler => options.ipxMiddlewarePrefix && handler.route?.startsWith(options.ipxMiddlewarePrefix)) ||
49-
nuxt.options.devServerHandlers.find(handler => options.ipxMiddlewarePrefix && handler.route?.startsWith(options.ipxMiddlewarePrefix))
53+
nuxt.hook('modules:done', () => {
54+
const hasUserProvidedIPX =
55+
nuxt.options.serverHandlers.find(handler => options.ipxMiddlewarePrefix && handler.route?.startsWith(options.ipxMiddlewarePrefix)) ||
56+
nuxt.options.devServerHandlers.find(handler => options.ipxMiddlewarePrefix && handler.route?.startsWith(options.ipxMiddlewarePrefix))
5057

51-
nuxt.options.runtimeConfig.mediaViewer = {
52-
publicRoot: resolve(nuxt.options.rootDir, 'public'),
53-
hasIpx: Boolean(hasUserProvidedIPX || options.installIpxMiddleware),
54-
ipxMiddlewarePrefix: options.ipxMiddlewarePrefix ?? ''
55-
}
58+
nuxt.options.runtimeConfig.mediaViewer = {
59+
publicRoot: resolve(nuxt.options.rootDir, 'public'),
60+
hasIpx: options.hasIpx ?? Boolean(hasUserProvidedIPX || options.installIpxMiddleware),
61+
ipxMiddlewarePrefix: options.ipxMiddlewarePrefix ?? ''
62+
}
5663

57-
if (options.installIpxMiddleware && !hasUserProvidedIPX) {
58-
// @todo: check if ipx middleware is already installed
59-
addServerHandler({
60-
handler: resolve(runtimeDir, 'server/middlewares/ipx')
61-
})
62-
}
64+
if (options.installIpxMiddleware && !hasUserProvidedIPX) {
65+
// @todo: check if ipx middleware is already installed
66+
addServerHandler({
67+
handler: resolve(runtimeDir, 'server/middlewares/ipx')
68+
})
69+
}
70+
})
6371

6472
// for now only inject viewer on dev mode
6573
if (nuxt.options.dev) {

0 commit comments

Comments
 (0)