From 2d7a04daefdc00abf692d7b8efb27c7f30e24e15 Mon Sep 17 00:00:00 2001 From: Blake Kostner Date: Wed, 30 Jun 2021 02:12:46 -0700 Subject: [PATCH] fix!: prepend router base (#339) --- src/generate.ts | 4 ++-- src/runtime/providers/ipx.ts | 4 ++-- test/unit/providers.test.ts | 11 +++++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/generate.ts b/src/generate.ts index cd848ad7c..1386e65ca 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -25,12 +25,12 @@ export function setupStaticGeneration (nuxt: any, options: ModuleOptions) { ext: (format && `.${format}`) || guessExt(input), hash: hash(url), // TODO: pass from runtimeConfig to mapStatic as param - publicPath: withoutTrailingSlash(nuxt.options.build.publicPath) + publicPath: nuxt.options.app.cdnURL ? '/' : withoutTrailingSlash(nuxt.options.build.publicPath) } staticImages[url] = options.staticFilename.replace(/\[(\w+)]/g, (match, key) => params[key] || match) } - return staticImages[url] + return joinURL(nuxt.options.app.cdnURL || nuxt.options.app.basePath, staticImages[url]) } }) diff --git a/src/runtime/providers/ipx.ts b/src/runtime/providers/ipx.ts index eecac935d..c2cba7811 100644 --- a/src/runtime/providers/ipx.ts +++ b/src/runtime/providers/ipx.ts @@ -16,7 +16,7 @@ const operationsGenerator = createOperationsGenerator({ formatter: (key, val) => encodeQueryItem(key, val) }) -export const getImage: ProviderGetImage = (src, { modifiers = {}, baseURL = '/_ipx' } = {}) => { +export const getImage: ProviderGetImage = (src, { modifiers = {}, baseURL = '/_ipx' } = {}, { nuxtContext: { base: nuxtBase = '/' } = {} }) => { if (modifiers.width && modifiers.height) { modifiers.resize = `${modifiers.width}_${modifiers.height}` delete modifiers.width @@ -26,7 +26,7 @@ export const getImage: ProviderGetImage = (src, { modifiers = {}, baseURL = '/_i const params = operationsGenerator(modifiers) return { - url: joinURL(baseURL, encodePath(src) + (params ? '?' + params : '')) + url: joinURL(nuxtBase, baseURL, encodePath(src) + (params ? '?' + params : '')) } } diff --git a/test/unit/providers.test.ts b/test/unit/providers.test.ts index ec88b4dde..c7f59229a 100644 --- a/test/unit/providers.test.ts +++ b/test/unit/providers.test.ts @@ -26,6 +26,17 @@ describe('Providers', () => { } }) + test('ipx router base', () => { + const context = { ...emptyContext, nuxtContext: { base: '/app/' } } + + const src = '/images/test.png' + const generated = ipx.getImage(src, { modifiers: {} }, context) + generated.url = cleanDoubleSlashes(generated.url) + expect(generated).toMatchObject({ + url: '/app/_ipx/images/test.png' + }) + }) + test('cloudinary', () => { const providerOptions = { baseURL: '/'