@envelop/preload-assets
Version:
Inject a function for registering assets that should be preloaded on the client. The registered assets will be added under the `extensions.preloadAssets` key on the execution result.
29 lines (28 loc) • 1.03 kB
JavaScript
import { handleStreamOrSingleExecutionResult } from '@envelop/core';
export const usePreloadAssets = (opts) => ({
onExecute: ({ extendContext, args }) => {
const assets = new Set();
extendContext({
registerPreloadAsset: (assetUrl) => assets.add(assetUrl),
});
if (opts?.shouldPreloadAssets?.(args.contextValue) ?? true) {
return {
onExecuteDone: payload => {
if (!assets.size) {
return;
}
return handleStreamOrSingleExecutionResult(payload, ({ result, setResult }) => {
setResult({
...result,
extensions: {
...result.extensions,
preloadAssets: Array.from(assets),
},
});
});
},
};
}
return undefined;
},
});