Skip to content

Commit

Permalink
feat: support prerendering static images (nuxt#614)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Nov 24, 2022
1 parent f4bf8a5 commit bedf8cb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/ipx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const ipxSetup: ProviderSetup = async (_providerOptions, moduleOptions) =
route: '/_ipx/**',
handler: resolver.resolve('./runtime/ipx')
})
return
}

// Add as dev handler for development
Expand Down
11 changes: 10 additions & 1 deletion src/runtime/components/nuxt-img.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { h, defineComponent, ref, computed, onMounted } from 'vue'
import { appendHeader } from 'h3'
import { useImage } from '../composables'
import { parseSize } from '../utils'
import { baseImageProps, useBaseImage } from './_base'
import { useHead } from '#imports'
import { useHead, useRequestEvent } from '#imports'

export const imgProps = {
...baseImageProps,
Expand Down Expand Up @@ -85,6 +86,14 @@ export default defineComponent({
})
}

if (process.env.prerender) {
const sources = [
src.value,
...(sizes.value.srcset || '').split(',').map(s => s.split(' ')[0])
].filter(s => s && s.includes('/_ipx/'))
appendHeader(useRequestEvent(), 'X-Nitro-Prerender', sources.join(','))
}

const imgEl = ref<HTMLImageElement>(null)

onMounted(() => {
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/ipx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export default lazyEventHandler(() => {
const ipxOptions = {
...(useRuntimeConfig().ipx || {}),
// TODO: Switch to storage API when ipx supports it
dir: fileURLToPath(new URL('../public', import.meta.url))
// TODO: Using relative paths for POC only
dir: fileURLToPath(new URL(process.env.prerender ? '../../.output/public' : '../public', import.meta.url))
}

const ipx = createIPX(ipxOptions)
Expand Down

0 comments on commit bedf8cb

Please sign in to comment.