Skip to content

Commit

Permalink
feat: add cacheDir options
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Nov 9, 2020
1 parent d2e1fe3 commit 449a005
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
19 changes: 7 additions & 12 deletions docs/content/en/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ Here is a sample to use `cloudinary`:
</code-block>
</code-group>

<!-- local provider -->
<!-- writing custom providers -->
See:
- [How to use provider](/nuxt-image#provider)
Expand Down Expand Up @@ -110,14 +109,13 @@ See:

## `defaultProvider`

If you want to use multiple providers in your project, you should pick one of them as the default provider. If you do not set `defaultProvider`, your first provider is picked as default.
If you want to use multiple providers in your project, you should pick one of them as the default provider. If you do not set `defaultProvider`, module uses `ipx` as the default provider.

```js{}[nuxt.config.js]
export default {
image: {
defaultProvider: 'twicpics',
providers: {
local: {},
twicpics: {
baseURL: 'https://i5acur1u.twic.pics/'
}
Expand All @@ -132,25 +130,22 @@ Internally nuxt image uses [ipx](https://github.com/nuxt-contrib/ipx) to modify

- `dir`: The root directory of the all images. By default nuxt image looks `static` dir to find original images,
- `clearCache`: The ipx has a caching stategy to clear cached images to reduce massive disk usages. You can schedule the cache cleaning job using `clearCache` option in provide options. By default this cron job is disabled.
- `cacheDir`: The directory to store the cached images.

```js{}[nuxt.config.js]
export default {
image: {
ipx: {
/**
* Public domain of your website
**/
baseURL: 'https://awesome.com/',
/**
* Internal address of your website
**/
internalBaseURL: 'http://192.168.1.100:3000/',
/**
* Input directory for images
**/
dir: '~/static',
/**
* Enable/Disabel cache cleaning cron job
* Cache directory for optimized images
**/
cacheDir: '~~/node_modules/.cache/nuxt-image',
/**
* Enable/Disable cache cleaning cron job
**/
clearCache: false
}
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,13 @@ function prepareLocalProvider ({ nuxt, options }, providerOptions) {

providerOptions = defu(providerOptions, {
baseURL: `http://${defaultHost}:${defaultPort}${prefix}`,
dir: path.resolve(nuxt.options.srcDir, nuxt.options.dir.static)
dir: path.join('~', nuxt.options.dir.static),
clearCache: false,
cacheDir: '~~/node_modules/.cache/nuxt-image'
})

providerOptions.dir = nuxt.resolver.resolvePath(providerOptions.dir)
providerOptions.cacheDir = nuxt.resolver.resolvePath(providerOptions.cacheDir)

return providerOptions
}
Expand Down
5 changes: 2 additions & 3 deletions src/providers/ipx/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import path from 'path'
import { ProviderFactory } from 'types'

export default <ProviderFactory> function (providerOptions) {
Expand Down Expand Up @@ -28,8 +27,8 @@ function createMiddleware (options) {
}
],
cache: {
dir: path.resolve('node_modules/.cache/nuxt-image'),
cleanCron: options.clearCache || false
dir: options.cacheDir,
cleanCron: options.clearCache
}
})
return IPXMiddleware(ipx)
Expand Down
2 changes: 1 addition & 1 deletion types/module.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export interface ModuleOptions {
defaultProvider: string;
presets: ImagePreset[];
ipx: {
baseURL: string;
dir?: string;
clearCache?: boolean | string;
cacheDir?: string;
}
sizes: number[],
internalUrl?: string
Expand Down

0 comments on commit 449a005

Please sign in to comment.