Skip to content

Commit

Permalink
feat: support remote urls
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Sep 25, 2020
1 parent ba239a4 commit fd4184b
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 8 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
},
"dependencies": {
"hasha": "^5.2.0",
"ipx": "latest"
"ipx": "latest",
"node-fetch": "^2.6.1"
},
"devDependencies": {
"@babel/preset-env": "latest",
Expand Down
2 changes: 1 addition & 1 deletion playground/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default {
return {
images: [
{
src: "/images/2000px-Aconcagua2016.jpg",
src: "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4e/Aconcagua2016.jpg/600px-Aconcagua2016.jpg",
alt: "Aconcagua Argentina"
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/providers/local/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default <ProviderFactory>function(providerOptions) {
function createMiddleware(options) {
const ipx = new IPX({
input: {
adapter: 'fs',
adapter: 'hybrid',
dir: options.dir
},
cache: {
Expand Down
3 changes: 1 addition & 2 deletions src/providers/local/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ export default <RuntimeProvider> {
operations.push(`h_${modifiers.height}${size}`)
}


const operationsString = operations.length ? operations.join(',') : '_'
return {
url: `/_image/local/${modifiers.format || '_'}/${operationsString}${src}`,
url: `/_image/local/${modifiers.format || '_'}/${operationsString}/${src.replace(/^\//, '')}`,
isStatic: true
};
}
Expand Down
7 changes: 4 additions & 3 deletions src/runtime/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface CreateImageOptions {
}

function processSource(src: string) {
if (!src.includes(':')) {
if (!src.includes(':') || src.match('^https?:\/\/')) {
return { src }
}

Expand All @@ -43,7 +43,7 @@ export function createImage(context, { providers, defaultProvider, presets }: Cr
const provider = providers[sourceProvider || options.provider || defaultProvider]
const preset = sourcePreset || options.preset

if (!src || src[0] !== '/') {
if (!src.match(/^(https?:\/\/|\/.*)/)) {
throw new Error('Unsupported image src "' + src + '", src path must be absolute. like: `/awesome.png`')
}

Expand All @@ -61,7 +61,8 @@ export function createImage(context, { providers, defaultProvider, presets }: Cr
{ ...provider.defaults, ...options }
)

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

let url = providerUrl
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9207,6 +9207,11 @@ node-fetch@^2.6.0:
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.0.tgz#e633456386d4aa55863f676a7ab0daa8fdecb0fd"
integrity sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==

node-fetch@^2.6.1:
version "2.6.1"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==

node-gyp@^3.8.0:
version "3.8.0"
resolved "https://registry.yarnpkg.com/node-gyp/-/node-gyp-3.8.0.tgz#540304261c330e80d0d5edce253a68cb3964218c"
Expand Down

0 comments on commit fd4184b

Please sign in to comment.