Skip to content

Commit

Permalink
fix!: prepend router base (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
btkostner authored Jun 30, 2021
1 parent 58dd744 commit 2d7a04d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
})

Expand Down
4 changes: 2 additions & 2 deletions src/runtime/providers/ipx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 : ''))
}
}

Expand Down
11 changes: 11 additions & 0 deletions test/unit/providers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '/'
Expand Down

0 comments on commit 2d7a04d

Please sign in to comment.