Skip to content

Commit

Permalink
feat: add ipxStatic provider (#878)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Sep 13, 2023
1 parent dc57349 commit 047d98a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 16 deletions.
8 changes: 4 additions & 4 deletions docs/content/1.get-started/4.configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export default defineNuxtConfig({

## `provider`

Default: `static`
Default: `ipx` (or `ipxStatic` if used with a static nitro preset, such as if you are running `nuxt generate`)

We can specify default provider to be used when not specified in component or when calling `$img`.

Expand Down Expand Up @@ -198,7 +198,7 @@ export default defineNuxtConfig({

Default: `public`

This option allows you to specify the location of the source images when using the `static` or `ipx` provider.
This option allows you to specify the location of the source images when using the `ipx` or `ipxStatic` provider.

For example you might want the source images in `assets/images` directory rather than the default `public` directory so the source images don't get copied into `dist` and deployed:

Expand All @@ -211,9 +211,9 @@ export default defineNuxtConfig({
```

**Notes:**
- For `static` provider, if images weren't crawled during generation (unreachable modals, pages or dynamic runtime size), changing `dir` from `static` causes 404 errors.
- For `ipxStatic` provider, if images weren't crawled during generation (unreachable modals, pages or dynamic runtime size), changing `dir` from `public` causes 404 errors.
- For `ipx` provider, make sure to deploy customized `dir` as well.
- For some providers (like vercel), using a directory other than `static/` for assets is not supported since resizing happens at runtime (instead of build/generate time) and source fetched from the `static/` directory (deployment URL)
- For some providers (like vercel), using a directory other than `public/` for assets is not supported since resizing happens at runtime (instead of build/generate time) and source fetched from the `public/` directory (deployment URL)

## `alias`

Expand Down
8 changes: 4 additions & 4 deletions src/ipx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface IPXRuntimeConfig {
alias: Record<string, string>
}

export const ipxSetup: ProviderSetup = async (providerOptions, moduleOptions) => {
export const ipxSetup: (setupOptions?: { isStatic: boolean }) => ProviderSetup = setupOptions => async (providerOptions, moduleOptions) => {
const nitro = useNitro()
const nuxt = useNuxt()

Expand Down Expand Up @@ -51,7 +51,9 @@ export const ipxSetup: ProviderSetup = async (providerOptions, moduleOptions) =>
route: '/_ipx/**',
handler: resolver.resolve('./runtime/ipx')
}
nitro.options.handlers.push(handler)
if (!setupOptions?.isStatic) {
nitro.options.handlers.push(handler)
}
// TODO: Workaround for prerender support
nitro.options._config.handlers!.push(handler)
return
Expand All @@ -73,8 +75,6 @@ export const ipxSetup: ProviderSetup = async (providerOptions, moduleOptions) =>
})
}
nitro.options.devHandlers.push(devHandler)
// TODO: Workaround for prerender support
nitro.options._config.devHandlers!.push(devHandler)
}

declare module 'nitropack' {
Expand Down
16 changes: 10 additions & 6 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default defineNuxtModule<ModuleOptions>({

// Run setup
for (const p of providers) {
if (typeof p.setup === 'function' && p.name !== 'ipx') {
if (typeof p.setup === 'function' && p.name !== 'ipx' && p.name !== 'ipxStatic') {
await p.setup(p, options, nuxt)
}
}
Expand Down Expand Up @@ -131,12 +131,16 @@ ${providers.map(p => ` ['${p.name}']: { provider: ${p.importName}, defaults: ${
})

nuxt.hook('nitro:init', async (nitro) => {
if (!options.provider || options.provider === 'ipx') {
imageOptions.provider = options.provider = nitro.options.node ? 'ipx' : 'none'
options[options.provider] = options[options.provider] || {}
if (!options.provider || options.provider === 'ipx' || options.provider === 'ipxStatic') {
const resolvedProvider = nitro.options.static || options.provider === 'ipxStatic'
? 'ipxStatic'
: nitro.options.node ? 'ipx' : 'none'

const p = await resolveProvider(nuxt, options.provider, options[options.provider])
if (!providers.some(p => p.name === options.provider)) {
imageOptions.provider = options.provider = resolvedProvider
options[resolvedProvider] = options[resolvedProvider] || {}

const p = await resolveProvider(nuxt, resolvedProvider, options[resolvedProvider])
if (!providers.some(p => p.name === resolvedProvider)) {
providers.push(p)
}
if (typeof p.setup === 'function') {
Expand Down
5 changes: 3 additions & 2 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const BuiltInProviders = [
'imagekit',
'imgix',
'ipx',
'ipxStatic',
'layer0',
'netlify',
'prepr',
Expand All @@ -41,8 +42,8 @@ const BuiltInProviders = [

export const providerSetup: Record<string, ProviderSetup> = {
// IPX
ipx: ipxSetup,
static: ipxSetup,
ipx: ipxSetup(),
ipxStatic: ipxSetup({ isStatic: true }),

// https://vercel.com/docs/more/adding-your-framework#images
vercel (_providerOptions, moduleOptions, nuxt: Nuxt) {
Expand Down
1 change: 1 addition & 0 deletions src/runtime/providers/ipxStatic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ipx'
1 change: 1 addition & 0 deletions test/unit/provider-coverage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { images } from '../providers'
import { providers as playgroundProviders } from '../../playground/providers'

const missingProviderTests = [
'ipxStatic', // build-time-only alias for ipx
'strapi', // covered in a unique test
'layer0' // backwards-compatible alias for edgio
]
Expand Down

0 comments on commit 047d98a

Please sign in to comment.