Resource preloading
Improves application startup time
CheerpJ runs Java applications and applets in the browser, with no local Java installation. To do this, it downloads parts of the Java runtime, such as the OpenJDK class library, while the application runs. By default, these runtime resources are loaded on demand, when the application first needs them.
Loading on demand keeps the download small, since only the parts of the runtime the application uses are fetched, and the browser caches them for later visits. The trade-off is that CheerpJ cannot know in advance which resources a particular application needs, so on a first visit it fetches them one after another as the application reaches them.
To reduce startup time in production, CheerpJ can preload the resources your application uses, so that they download in parallel as the application starts.
What resource preloading does
- Without preloading: CheerpJ fetches runtime resources one at a time, as they become necessary.
- With preloading: CheerpJ starts downloading a predefined set of resources immediately, in parallel, so the application starts faster.
When to use it
- You are deploying an application to production and want a faster first load
- Your users often start with an empty cache (first visit, cleared cache, new device)
- You want to avoid pauses caused by fetching resources one at a time
Overview of the workflow
- Record which runtime resources your application uses, with
cjGetRuntimeResources - Pass that list to
cheerpjInitwith thepreloadResourcesoption
Step 1: Profile used runtime resources
- Run your application with CheerpJ, and use the parts of it that your users need at startup.
- Open the browser’s developer tools console (often
Ctrl+Shift+I). - Run:
cjGetRuntimeResources();The output looks like this:
{"/lts/file1.jar":[int, int, ...], "/lts/file2.jar":[int,int, ...]}If the console does not show the full output, use:
document.write(cjGetRuntimeResources());cjGetRuntimeResources returns a string, so the console may show the output in quotes ("). Copy the output without the quotes, or parse the string with JSON.parse.
Step 2: Enable preloading in your integration
Add the preloadResources option to your cheerpjInit call, with the object from step 1.
Example:
cheerpjInit({ preloadResources: { "/lts/file1.jar": [int, int, ...], "/lts/file2.jar": [int, int, ...] }});Profiling the resources with cjGetRuntimeResources and passing the result to preloadResources happen in separate sessions. First, profile the application in one session to generate the list of resources. Then pass this list to cheerpjInit in your page, so that later sessions preload it.
To show the progress of the preload, for example in a loading bar, add the preloadProgress callback.
What changes after enabling preloading
With preloadResources, CheerpJ downloads several resources in parallel while the program runs, which can reduce startup time considerably, especially on a first visit.