Skip to content

Commit

Permalink
fix(full-static): find hashed images from page payload
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Sep 25, 2020
1 parent fd4184b commit f7512c7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/runtime/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,33 @@ export function createImage(context, { providers, defaultProvider, presets }: Cr
presetMap[options.preset] ? presetMap[options.preset].modifiers : modifiers,
{ ...provider.defaults, ...options }
)

const { data } = context.nuxtState || context.ssrContext.nuxt
const nuxtState = data[0]
nuxtState.images = nuxtState.images || {}

if (typeof window !== "undefined" && window.__NUXT_JSONP_CACHE__) {
const jsonPData = window.__NUXT_JSONP_CACHE__[context.route.path].data[0]
if (jsonPData.images[providerUrl]) {
// Hydration with hash
return jsonPData.images[providerUrl]
}
// return original source on cache fail in full static mode
return src
}

const nuxtState = context.nuxtState || context.ssrContext.nuxt
const data = nuxtState.data[0]

data.images = data.images || {}

let url = providerUrl
if (nuxtState.images[url]) {
if (data.images[url]) {
// Hydration with hash
url = nuxtState.images[url]
url = data.images[url]
} else if (context.ssrContext && typeof context.ssrContext.mapImage === 'function') {
// Full Static
const originalURL = url
url = context.ssrContext.mapImage({ url, isStatic, format: modifiers.format, src })

if (url) {
nuxtState.images[providerUrl] = url
data.images[providerUrl] = url
} else {
url = originalURL
}
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
declare global {
interface Window {
__NUXT_JSONP_CACHE__: { [key: string]: any }
}
}

export interface ModuleOptions {
defaultProvider: string;
presets: ImagePreset[],
Expand Down

0 comments on commit f7512c7

Please sign in to comment.