Skip to content

Commit

Permalink
fix: ensure static image map is available on first load (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Apr 27, 2021
1 parent 9561daf commit 466d5a8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/runtime/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ export function createImage (globalOptions: CreateImageOptions, nuxtContext: any

if (process.server) {
const { ssrContext } = ctx.nuxtContext
const ssrState = ssrContext.nuxt
const ssrData = ssrContext.nuxt.data[0]
const staticImages = ssrData._img = ssrData._img || {}
const staticImages = ssrState._img = ssrData._img = ssrData._img || {}
const mapToStatic: MapToStatic = ssrContext.image?.mapToStatic
if (typeof mapToStatic === 'function') {
const mappedURL = mapToStatic(image)
Expand Down
5 changes: 5 additions & 0 deletions src/runtime/utils/static-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ const staticImageMap = {}

function updateImageMap () {
if (typeof window.$nuxt !== 'undefined') {
// Client-side navigation
const pageImages = (window.$nuxt as any)._pagePayload?.data?.[0]?._img || {}
Object.assign(staticImageMap, pageImages)
} else if (typeof (window as any).__NUXT__ !== 'undefined') {
// Initial load
const pageImages = (window as any).__NUXT__?._img || {}
Object.assign(staticImageMap, pageImages)
}
}

Expand Down

0 comments on commit 466d5a8

Please sign in to comment.