Skip to content
Limited timeStarter is now $9 a month instead of $20.See plans
charmotion

How to Optimize Rive WASM Bundle Size, Self-Host rive.wasm & Lazy-Load Animations

By the Charmotion team · Updated · How we write guides

Cover illustration for the guide: How to Optimize Rive WASM Bundle Size, Self-Host rive.wasm & Lazy-Load Animations, with Charmotion demo characters

To fix Rive WASM bundle size, self-host rive.wasm from your own origin, match its version to your @rive-app package, and load each Rive component only when it nears the viewport. Serve the file as application/wasm with compression so the browser takes the fast path. Rive's migration guide puts the v2.0.0 request at about 261 KB brotli-compressed.

Key takeaways

  • Self-host rive.wasm from your own origin so you control caching and startup timing.
  • Keep rive.wasm on exactly the same version as your @rive-app package, because the two are built as a pair.
  • Serve the file as application/wasm with brotli or gzip compression.
  • Lazy-load each Rive component with a dynamic import and an IntersectionObserver.
  • Use @rive-app/canvas-lite only if your files use none of Rive Text, Layouts, Audio or Scripting.

Why does the rive.wasm file slow down a page?

If you want to know how to fix Rive WASM bundle size and self-host rive.wasm for faster load, start with the file the browser actually downloads. Rive's web runtime has two parts: a JavaScript package your code imports, and a WebAssembly module, rive.wasm, that does the drawing and runs the animation loop. The browser has to download, compile and start that module before the first frame of any .riv animation can appear.

Rive's migration guide says the WASM request for v2.0.0 and later is about 261 KB brotli-compressed. Use that figure as a reference point for your own build, not a fixed promise, because file sizes change between releases. The download sits on the critical path of any page that shows an animation early, so it is worth reducing before you touch the animations themselves.

By default, Rive's low-level API docs say the WASM file can be loaded from unpkg, which makes a network call to a third-party CDN. The rive-wasm repository overview describes unpkg as the default with a jsDelivr fallback. Hosting the file yourself removes that dependency. Rive's preloading docs list the benefits: control over the WASM origin, faster initialization, and control over the caching policy for rive.wasm.

How do you measure the rive.wasm download?

Measure before you change anything. Open DevTools, go to the Network panel, tick Disable cache, reload the page, and filter the list by wasm. The Size column shows transferred bytes and the Content column shows the uncompressed size. A large gap means compression is working. Two requests to unpkg.com in that list mean the file is still coming from the CDN.

From a terminal, check the headers your server returns:

curl -sI https://yourdomain.com/rive.wasm | grep -iE 'content-type|content-encoding|cache-control'

The table shows what a healthy setup returns.

CheckWhat you wantWhy it matters
Request hostYour own domain, not unpkg.comRemoves a third-party request and gives you control over caching
Content-Typeapplication/wasmRive's preloading docs say the browser otherwise falls back to a slower path
Content-Encodingbr or gzipThe 261 KB figure in Rive's migration guide is the brotli size
Cache-ControlA policy that matches your deploy scheduleCaching is the lever for repeat visits
VersionMatches the @rive-app package in package.jsonThe runtime and the WASM file are built as a pair

Run the same check after each Rive upgrade. A header that was correct last quarter can change when a CDN or hosting plan changes.

How do you self-host rive.wasm in your build?

Copy the file from the same npm package version your code imports. The npm @rive-app/canvas package ships rive.wasm at the root of the package, so a copy step keeps the runtime and the WASM file in sync. Install the package first, then copy:

npm install @rive-app/canvas
mkdir -p public
cp node_modules/@rive-app/canvas/rive.wasm public/rive.wasm

Run the copy step again whenever you upgrade the package. If you put it in a build script, the WASM file can never drift from the JavaScript version.

Next, point the runtime at your copy. Rive's preloading docs describe an optional WASM_URL setting that points to a self-hosted file, and they advise starting the load as early as possible on page load. Set it to /rive.wasm, then add a preload hint in the document head so the browser starts the download early:

<link rel='preload' href='/rive.wasm' as='fetch' type='application/wasm' crossorigin>

The hint only helps when the runtime requests the same URL, so check the Network panel to confirm one request for /rive.wasm and none for unpkg.com.

How do you lazy-load Rive animations?

Lazy loading delays the work of starting an animation until it is needed. It does not change the file. Split the Rive code out of your first bundle with a dynamic import, and start it when the canvas gets close to the viewport. Animations below the fold then stop competing with the content a visitor needs first.

const target = document.querySelector('#mascot')
const io = new IntersectionObserver(async (entries) => {
  if (!entries[0].isIntersecting) return
  io.disconnect()
  const { Rive } = await import('@rive-app/canvas')
  new Rive({
    src: '/animations/mascot.riv',
    canvas: target,
    autoplay: true,
  })
}, { rootMargin: '200px' })
io.observe(target)

The rootMargin gives the import a head start so the animation is ready when the canvas scrolls into view. The canvas element in your markup should have a fixed size in CSS, so the layout does not shift when the animation draws. Animations in the first screen should skip the observer and start after first paint.

Framework setups follow the same idea with their own tools. In Astro, the islands model gives you a directive for visibility-based hydration, and the Astro guide walks through it in Rive in Astro. For React and Next.js component setup, see How to Use Rive in React and Next.js.

Should you use @rive-app/canvas or @rive-app/canvas-lite?

The lite package is the right choice when your files do not need every feature. Rive's migration guide says @rive-app/canvas-lite is the option for a version without Rive Text, Layouts, Audio and Scripting. Open each .riv file in the Rive editor and check what it uses. If an animation depends on text, a layout, sound or a script, keep the full package.

  • Icon loops, logo intros and character states built from shapes, bones and state machine inputs usually fit the lite package.
  • Responsive layouts, rendered text, audio cues and scripted behavior need the full package.
  • Build both versions once and compare the transferred size in DevTools before you decide.

The size difference is worth measuring because the uncompressed file is much larger than the brotli figure. The Rive Chrome extension guide on this site records the uncompressed rive.wasm in @rive-app/canvas 2.47.1 at 2,165,388 bytes, so compression is not optional. See Rive in a Chrome Extension (Manifest V3) for how that file is bundled in an extension.

How should you cache and version rive.wasm?

Cache the file for a long time, and change its URL when the version changes. Two patterns work well.

  1. Put the version in the path, such as /rive/2.47.1/rive.wasm, and set a long max-age on it. The URL changes with each upgrade, so old copies never serve new code.
  2. Keep the name rive.wasm and use a shorter cache policy with revalidation, so a deploy reaches visitors within a window you control.

For the versioned path, a header like this works:

Cache-Control: public, max-age=31536000, immutable

Whichever pattern you choose, the version in the URL must still match the @rive-app package. A stale rive.wasm left behind after an upgrade is an easy mistake, and it can break animations that worked in development. Rive's preloading docs note that caching is something you control when you self-host, so set the policy on purpose rather than inheriting a default from your host.

How do you confirm the fix worked?

Test the page the way a first-time visitor will see it. Use a throttled connection, such as Slow 4G in DevTools, and clear the cache before each run. Then check four things in order.

  • The Network panel shows one request for /rive.wasm and no request to unpkg.com.
  • The response has the application/wasm content type and a brotli or gzip encoding.
  • The first animation draws earlier in the Performance panel than it did before the change.
  • On a second visit, the file comes from cache rather than the network.

If the page still feels slow, the .riv file may be the cost rather than the runtime. The runtime is one fixed download, while a large animation file grows with its content. The comparison in Rive vs Lottie Performance and Runtime Size covers how file size and runtime size trade off. Hosted site builders may limit the headers and files you can set, so check that before planning a self-hosted setup; the guide on adding Rive and Lottie animations to Framer shows what a builder handles for you.

How to self-host rive.wasm and lazy-load Rive animations

  1. Install the matching package. Install @rive-app/canvas at the version listed in your package.json, so the runtime and the WASM file come from the same release.
  2. Copy rive.wasm into your site. Copy node_modules/@rive-app/canvas/rive.wasm into your public folder, for example as public/rive.wasm, and repeat the copy after every upgrade.
  3. Serve the file from your own origin. Serve the file with the application/wasm content type and brotli or gzip compression, then point the runtime at it using the WASM_URL setting in Rive's preloading docs.
  4. Set the caching policy. Use a long max-age on a versioned path, or a shorter revalidation policy on a fixed name, so caching matches how often you deploy.
  5. Lazy-load each animation. Start each Rive component with a dynamic import inside an IntersectionObserver callback, with a rootMargin so it is ready as it scrolls into view.
  6. Verify in DevTools. Confirm one request for /rive.wasm, no request to unpkg.com, the correct content type and encoding, and a cache hit on the second visit.

Frequently asked questions

Will a CDN-hosted rive.wasm still work with lazy loading?

Yes. Lazy loading controls when your code starts a Rive animation, not where the WASM file comes from. You can keep the default unpkg source and still defer the import. Self-hosting removes the third-party request from the critical path and gives you control over caching, which is the main reason to make the change.

Do I need to self-host rive.wasm if my animations are small?

Not always. Self-hosting is mainly about control over the origin, startup timing and caching. If your animation is not visible early on the page and your host already caches static files well, the gain may be small. Measure the download in DevTools first, then decide.

Is rive.wasm one file for every animation on the page?

Yes. The WASM module is the runtime that draws and controls animations, and each .riv file is the animation data it loads. Your page downloads the runtime once and then loads each .riv file that a component needs, so the size of the animation files matters separately.

How do I check that caching works after a deploy?

Load the page, then reload it with the Network panel open. On the second load, the rive.wasm row should show that it came from cache rather than the network. If it does not, check the Cache-Control header on the response and confirm that the file name or version path changed as expected.

Sources

Related guides