<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://wiki.socialstack.dev/index.php?action=history&amp;feed=atom&amp;title=Lazy_loading</id>
	<title>Lazy loading - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.socialstack.dev/index.php?action=history&amp;feed=atom&amp;title=Lazy_loading"/>
	<link rel="alternate" type="text/html" href="https://wiki.socialstack.dev/index.php?title=Lazy_loading&amp;action=history"/>
	<updated>2026-10-16T08:58:18Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.36.2</generator>
	<entry>
		<id>https://wiki.socialstack.dev/index.php?title=Lazy_loading&amp;diff=273&amp;oldid=prev</id>
		<title>Luke: Created page with &quot;Sometimes you&#039;ll want to use a large javascript library inline in your project. Inline is usually recommended anyway as it eliminates external dependencies which some projects...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.socialstack.dev/index.php?title=Lazy_loading&amp;diff=273&amp;oldid=prev"/>
		<updated>2022-08-31T14:31:41Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;Sometimes you&amp;#039;ll want to use a large javascript library inline in your project. Inline is usually recommended anyway as it eliminates external dependencies which some projects...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Sometimes you&amp;#039;ll want to use a large javascript library inline in your project. Inline is usually recommended anyway as it eliminates external dependencies which some projects don&amp;#039;t permit or don&amp;#039;t support, such as an app build. But because of its size, it may have a negative impact on page load times. That page load time penalty is even worse if the library itself isn&amp;#039;t used frequently, or not at all during the majority of page visits. This is the situation where lazy loading is useful - load a .js file on demand rather than bundling it always.&lt;br /&gt;
&lt;br /&gt;
= Avoiding the js file from being bundled =&lt;br /&gt;
&lt;br /&gt;
Firstly, the SocialStack UI compiler will collect all .js files in your project and bundle them together. In this case that&amp;#039;s not what we want. It can be avoided (this is true for .css files as well) by creating a folder called &amp;#039;&amp;#039;&amp;#039;static&amp;#039;&amp;#039;&amp;#039; and placing your .js library file in there. For example &amp;#039;&amp;#039;UI/Thing/static/thingLibrary.js&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
= Loading the file =&lt;br /&gt;
&lt;br /&gt;
Next, you&amp;#039;ll want to load the file on demand. A simple fetch is often enough, but webRequest does also have a built in convenience function called lazyLoad which has a cache internally to avoid loading the same thing repeatedly. Here&amp;#039;s how to use it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;js&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
import {lazyLoad} from &amp;#039;UI/Functions/WebRequest&amp;#039;;&lt;br /&gt;
import thingLibraryRef from &amp;#039;./static/thingLibrary.js&amp;#039;;&lt;br /&gt;
import getRef from &amp;#039;UI/Functions/GetRef&amp;#039;;&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
// When it comes time to require your library e.g. because some function is called, do this:&lt;br /&gt;
&lt;br /&gt;
lazyLoad(getRef(thingLibraryRef, {url:1})).then(imported =&amp;gt; {&lt;br /&gt;
    // If the file exported anything, it will be available in imported.&lt;br /&gt;
    // Otherwise it may just be in the window scope, i.e. window.thingLibrary.&lt;br /&gt;
})&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Luke</name></author>
	</entry>
</feed>