@badrap/preload
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -1,2 +0,2 @@ | ||
function r(){}var t={functional:!0,props:["status","error"],render:function(r,t){return r("div",{},[t.props.status+" "+t.props.error.message])}},n=Symbol(),e=Symbol();function o(r,t){return void 0===r&&(r=404),void 0===t&&(t="Not found"),{$type:n,status:r,error:t instanceof Error?t:{message:t}}}function u(r){return{$type:e,to:r}}module.exports=function(i,c){void 0===c&&(c={});var a=c.context;void 0===a&&(a={});var s=c.beforePreload;void 0===s&&(s=r);var f=c.afterPreload;void 0===f&&(f=r);var l=c.errorComponent;void 0===l&&(l=t);var d=null,h=Symbol();function p(r){return new Promise(function(t,i){var c,p,v=function(r){return function(t){try{return f(),r&&r.call(this,t)}catch(r){return i(r)}}.bind(this)}.bind(this);c=null,p={},s();var m=function(){try{if(c){if(c.$type!==n)return c.$type===e?t(c.to):i(new Error("unknown action"));d={render:function(r){return r(l,{props:{status:c.status,error:c.error}})}}}else d={provide:function(){var r;return(r={})[h]=p,r},render:function(r){return r("router-view",{attrs:this.$attrs})}};return t()}catch(r){return i(r)}},y=function(r){try{throw r}catch(r){return v(i)(r)}};try{var b,$;function P(){var r=$();return g.bind(this,r[0],r[1])}function g(t,i){var s,f,l,d;return $=function(){return[t,i]},!(i[1]=i[0].next()).done&&(t=i[1].value,1)?(s=t.meta[h])?s().then(function(t){var i;try{return f=(i=t).key,(l=i.preload)?Promise.resolve(l(Object.assign({},{route:r,redirect:u,error:o},a))).then(function(r){try{return!(d=r)||d.$type!==e&&d.$type!==n?(p[f]=d,P):(c=d,[1])}catch(r){return y(r)}},y):P}catch(r){return y(r)}},y):P:[1]}return(b=function(r){for(;r;){if(r.then)return void r.then(b,y);try{if(r.pop){if(r.length)return r.pop()?w.call(this):r;r=P}else r=r.call(this)}catch(r){return y(r)}}}.bind(this))(g.bind(this,void 0,[r.matched[Symbol.iterator]()]));function w(){return v(m)()}}catch(r){y(r)}})}return[{path:"",component:{beforeRouteEnter:function(r,t,n){p(r).then(n,n)},beforeRouteUpdate:function(r,t,n){p(r).then(n,n)},render:function(r){return r(d,{key:this.$route.fullPath,attrs:this.$attrs})}},children:function r(t,n){return t.map(function(t){return n(Object.assign({},t,{children:t.children&&r(t.children,n)}))})}(i,function(r){var t,n=function(){return new Promise(function(t,n){var e,o,u;return(u=r.component,"function"!=typeof u?Promise.resolve(u):new Promise(function(r,t){Promise.resolve(u(r,t)).then(r,t)})).then(function(r){try{return(e=r).preload?(o=Symbol(),t({key:o,preload:e.preload,component:{extends:e,inject:{$preload:h},data:function(){return Object.assign({},this.$preload[o])}}})):t({key:null,preload:null,component:e})}catch(r){return n(r)}},n)})},e=null,o=function(){return e||(e=n()),e};return Object.assign({},r,{meta:Object.assign({},r.meta,(t={},t[h]=o,t)),component:function(){return new Promise(function(r,t){return o().then(function(n){try{return r(n.component)}catch(r){return t(r)}},t)})}})})}]}; | ||
var e={functional:!0,props:["status","error"],render:function(e,r){return e("div",{},[r.props.status+" "+r.props.error.message])}},r={},n={};function t(e,n){return void 0===e&&(e=404),void 0===n&&(n="Not found"),{$type:r,status:e,error:n instanceof Error?n:{message:n}}}function o(e){return{$type:n,to:e}}module.exports=function(u,i){void 0===i&&(i={});var s=i.context;void 0===s&&(s={});var a=i.errorComponent;void 0===a&&(a=e);var c,f=i.beforePreload,l=i.afterPreload,p=Symbol();function d(e,u,i){var d=null,v={};Promise.resolve().then(function(){return f&&f(),function e(r,n,t){return n>=r.length?Promise.resolve():Promise.resolve(t(r[n])).then(function(){return e(r,n+1,t)})}(e.matched,0,function(u){var i=u.meta[p];if(!d&&i)return i().then(function(u){var i=u.key,a=u.preload;if(a)return Promise.resolve(a(Object.assign({},{route:e,redirect:o,error:t},s))).then(function(e){!e||e.$type!==n&&e.$type!==r?v[i]=e:d=e})})}).then(function(){l&&l()},function(e){throw l&&l(),e}).then(function(){if(d){if(d.$type!==r){if(d.$type===n)return d.to;throw new Error("unknown action")}c={render:function(e){return e(a,{props:{status:d.status,error:d.error}})}}}else c={provide:function(){var e;return(e={})[p]=v,e},render:function(e){return e("router-view",{attrs:this.$attrs})}}})}).then(i,i)}return[{path:"",component:{beforeRouteEnter:d,beforeRouteUpdate:d,render:function(e){return e(c,{key:this.$route.fullPath,attrs:this.$attrs})}},children:function e(r,n){return r.map(function(r){return n(Object.assign({},r,{children:r.children&&e(r.children,n)}))})}(u,function(e){var r,n=null,t=function(){var r;return n||(n=(r=e.component,"function"!=typeof r?Promise.resolve(r):new Promise(function(e,n){Promise.resolve(r(e,n)).then(e,n)})).then(function(e){if(!e.preload)return{key:null,preload:null,component:e};var r=Symbol();return{key:r,preload:e.preload,component:{extends:e,inject:{$preload:p},data:function(){return Object.assign({},this.$preload[r])}}}})),n};return Object.assign({},e,{meta:Object.assign({},e.meta,(r={},r[p]=t,r)),component:function(){return t().then(function(e){return e.component})}})})}]}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@badrap/preload", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Data preloading for vue-router, similar to Sapper's preload or Nuxt.js's asyncData", | ||
@@ -19,4 +19,4 @@ "main": "dist/index.js", | ||
"eslint-config-prettier": "^3.3.0", | ||
"microbundle": "^0.7.0", | ||
"prettier": "^1.15.2" | ||
"microbundle": "^0.8.3", | ||
"prettier": "^1.15.3" | ||
}, | ||
@@ -23,0 +23,0 @@ "keywords": [ |
203
src/index.js
@@ -10,4 +10,2 @@ function mapRoutes(routes, func) { | ||
function defaultNoopHook() {} | ||
const defaultErrorComponent = { | ||
@@ -23,4 +21,4 @@ functional: true, | ||
const ACTION_ERROR = Symbol(); | ||
const ACTION_REDIRECT = Symbol(); | ||
const ACTION_ERROR = {}; | ||
const ACTION_REDIRECT = {}; | ||
@@ -55,36 +53,33 @@ function error(status = 404, message = "Not found") { | ||
context = {}, | ||
beforePreload = defaultNoopHook, | ||
afterPreload = defaultNoopHook, | ||
errorComponent = defaultErrorComponent | ||
errorComponent = defaultErrorComponent, | ||
beforePreload, | ||
afterPreload | ||
} = {} | ||
) { | ||
let component = null; | ||
const preloadKey = Symbol(); | ||
let component; | ||
const newRoutes = mapRoutes(routes, route => { | ||
const prepare = async () => { | ||
const resolved = await componentPromise(route.component); | ||
if (!resolved.preload) { | ||
return { key: null, preload: null, component: resolved }; | ||
} | ||
const key = Symbol(); | ||
return { | ||
key, | ||
preload: resolved.preload, | ||
component: { | ||
extends: resolved, | ||
inject: { | ||
$preload: preloadKey | ||
}, | ||
data() { | ||
return { ...this.$preload[key] }; | ||
} | ||
} | ||
}; | ||
}; | ||
let cached = null; | ||
const cachedPrepare = () => { | ||
if (!cached) { | ||
cached = prepare(); | ||
cached = componentPromise(route.component).then(resolved => { | ||
if (!resolved.preload) { | ||
return { key: null, preload: null, component: resolved }; | ||
} | ||
const key = Symbol(); | ||
return { | ||
key, | ||
preload: resolved.preload, | ||
component: { | ||
extends: resolved, | ||
inject: { | ||
$preload: preloadKey | ||
}, | ||
data() { | ||
return { ...this.$preload[key] }; | ||
} | ||
} | ||
}; | ||
}); | ||
} | ||
@@ -100,5 +95,4 @@ return cached; | ||
}, | ||
async component() { | ||
const { component } = await cachedPrepare(); | ||
return component; | ||
component() { | ||
return cachedPrepare().then(({ component }) => component); | ||
} | ||
@@ -108,64 +102,91 @@ }; | ||
async function runPreload(to) { | ||
function iterate(array, index, func) { | ||
if (index >= array.length) { | ||
return Promise.resolve(); | ||
} else { | ||
return Promise.resolve(func(array[index])).then(() => { | ||
return iterate(array, index + 1, func); | ||
}); | ||
} | ||
} | ||
function beforeRoute(to, _from, next) { | ||
let action = null; | ||
const datas = {}; | ||
beforePreload(); | ||
try { | ||
for (const route of to.matched) { | ||
const prepare = route.meta[preloadKey]; | ||
if (!prepare) { | ||
continue; | ||
Promise.resolve() | ||
.then(() => { | ||
if (beforePreload) { | ||
beforePreload(); | ||
} | ||
const { key, preload } = await prepare(); | ||
if (!preload) { | ||
continue; | ||
} | ||
const data = await Promise.resolve( | ||
preload({ route: to, redirect, error, ...context }) | ||
); | ||
if ( | ||
data && | ||
(data.$type === ACTION_REDIRECT || data.$type === ACTION_ERROR) | ||
) { | ||
action = data; | ||
break; | ||
} | ||
datas[key] = data; | ||
} | ||
} catch (err) { | ||
throw err; | ||
} finally { | ||
afterPreload(); | ||
} | ||
if (!action) { | ||
component = { | ||
provide() { | ||
return { | ||
[preloadKey]: datas | ||
}; | ||
}, | ||
render(h) { | ||
return h("router-view", { | ||
attrs: this.$attrs | ||
return iterate(to.matched, 0, route => { | ||
const prepare = route.meta[preloadKey]; | ||
if (action || !prepare) { | ||
return; | ||
} | ||
return prepare().then(({ key, preload }) => { | ||
if (preload) { | ||
return Promise.resolve( | ||
preload({ route: to, redirect, error, ...context }) | ||
).then(data => { | ||
if ( | ||
data && | ||
(data.$type === ACTION_REDIRECT || | ||
data.$type === ACTION_ERROR) | ||
) { | ||
action = data; | ||
} else { | ||
datas[key] = data; | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
}; | ||
} else if (action.$type === ACTION_ERROR) { | ||
component = { | ||
render(h) { | ||
return h(errorComponent, { | ||
props: { | ||
status: action.status, | ||
error: action.error | ||
}) | ||
.then( | ||
() => { | ||
if (afterPreload) { | ||
afterPreload(); | ||
} | ||
}, | ||
err => { | ||
if (afterPreload) { | ||
afterPreload(); | ||
} | ||
throw err; | ||
} | ||
) | ||
.then(() => { | ||
if (!action) { | ||
component = { | ||
provide() { | ||
return { | ||
[preloadKey]: datas | ||
}; | ||
}, | ||
render(h) { | ||
return h("router-view", { | ||
attrs: this.$attrs | ||
}); | ||
} | ||
}; | ||
} else if (action.$type === ACTION_ERROR) { | ||
component = { | ||
render(h) { | ||
return h(errorComponent, { | ||
props: { | ||
status: action.status, | ||
error: action.error | ||
} | ||
}); | ||
} | ||
}; | ||
} else if (action.$type === ACTION_REDIRECT) { | ||
return action.to; | ||
} else { | ||
throw new Error("unknown action"); | ||
} | ||
}); | ||
} | ||
}; | ||
} else if (action.$type === ACTION_REDIRECT) { | ||
return action.to; | ||
} else { | ||
throw new Error("unknown action"); | ||
} | ||
}) | ||
.then(next, next); | ||
} | ||
@@ -177,8 +198,4 @@ | ||
component: { | ||
beforeRouteEnter(to, from, next) { | ||
runPreload(to).then(next, next); | ||
}, | ||
beforeRouteUpdate(to, from, next) { | ||
runPreload(to).then(next, next); | ||
}, | ||
beforeRouteEnter: beforeRoute, | ||
beforeRouteUpdate: beforeRoute, | ||
render(h) { | ||
@@ -185,0 +202,0 @@ return h(component, { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31070
212