Skip to content

Instantly share code, notes, and snippets.

@prateekbh
Created August 25, 2020 21:59
Show Gist options
  • Select an option

  • Save prateekbh/10092e7a4b5b6d7bae354ddcc3112bcf to your computer and use it in GitHub Desktop.

Select an option

Save prateekbh/10092e7a4b5b6d7bae354ddcc3112bcf to your computer and use it in GitHub Desktop.
class DeferScripts extends NextScript {
makeScriptsDefer(el) {
return React.Children.map(el, child => {
if (!child) {
return child;
}
if (child.props.children) {
child.props.children = this.makeScriptsDefer(child.props.children)
} else {
const newProps = {...child.props || {}};
newProps.async = false;
newProps.defer = true;
return React.cloneElement(child, newProps);
}
return child;
});
}
render() {
const nextScripts = super.render();
return this.makeScriptsDefer(nextScripts);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment