Motion
Back to framer.com
DocumentationGuides
Reduce bundle size
Motion
Back to framer.com
Design and publish your first free site today.
Getting started
  • Introduction
  • Examples
Animation
  • Overview
  • Layout
  • Gestures
  • Scroll
  • Transition
Components
  • motion
  • AnimatePresence
  • LayoutGroup
  • LazyMotion
  • MotionConfig
  • Reorder
Motion values
  • Overview
  • useMotionValueEvent
  • useMotionTemplate
  • useScroll
  • useSpring
  • useTime
  • useTransform
  • useVelocity
  • useWillChange
Utilities
  • animate
  • transform
  • useAnimationControls
  • useAnimationFrame
  • useDragControls
  • useInView
  • useReducedMotion
3D
  • Introduction
  • LayoutCamera
  • LayoutOrthographicCamera
  • MotionCanvas
Guides
  • Accessibility
  • Reduce bundle size
  • Upgrade guides
Community
  • GitHub
  • Discord

Reduce bundle size

Learn what makes up Motion's bundle size, and how to reduce it.

A great web experience doesn't just look and move beautifully, it should load quickly, too.

When measuring the gzipped and minified size of Framer Motion using a bundle analysis website like Bundlephobia, you might see big numbers like 50kb or more!

This is misleading. Framer Motion exports many functions, most of which you won't import. JavaScript bundlers like Rollup and Webpack are capable of "tree shaking", which means that only the code you import is shipped to consumers.

You may only use a tiny, single hook from Framer Motion, like useReducedMotion. So in that case the size would be closer to 1kb.

Regardless, the core API is the motion component. Because of its declarative, props-driven API, it's impossible for bundlers to tree shake it any smaller than 29kb.

However, by using the m and LazyMotion components, you can bring this down significantly, to just under 4.6kb for the initial render.

Then, with lazy-loading, you can defer the loading of animations and interactions until after your site has rendered.

Note: All sizes quoted in this guide are from Rollup -generated bundles. Webpack is less effective at tree-shaking and should generate slightly larger bundles.

#How to reduce the size of the motion component

Instead of importing motion, import the slimmer m component.

import { m } from "framer-motion"

m is used in the exact same way as motion, but unlike motion, the m component doesn't come preloaded with features like animations, layout animations, or the drag gesture.

Instead, we load these in manually via the LazyMotion component. This lets you choose which features you load in, and whether you load them as part of the main bundle, or lazy load them.

import { LazyMotion, domAnimation } from "framer-motion"
// Load only the domAnimation package
function App({ children }) {
return (
<LazyMotion features={domAnimation}>
{children}
</LazyMotion>
)
}

#Available features

There are currently two feature packages you can load:

  • domAnimation: This provides support for animations, variants, exit animations, and tap/hover/focus gestures. (+15kb)
  • domMax: This provides support for all of the above, plus pan/drag gestures and layout animations. (+25kb)

In the future it might be possible to offer more granular feature packages, but for now these were chosen to reduce the amount of duplication between features, which could result in much more data being downloaded ultimately.

#Synchronous loading

By passing one of these feature packages to LazyMotion, they'll be bundled into your main JavaScript bundle.

import { LazyMotion, domAnimation } from "framer-motion"
function App({ children }) {
return (
<LazyMotion features={domAnimation}>
{children}
</LazyMotion>
)
}

#Lazy loading

If you're using a bundler like Webpack or Rollup, we can pass a dynamic import function to features that will fetch features only after we've performed our initial render.

First, create a file that exports only the features you want to load.

// features.js
import { domMax } from "framer-motion"
export default domMax

Then, pass features a function that will dynamically load that file.

import { LazyMotion, m } from "framer-motion"
// Make sure to return the specific export containing the feature bundle.
const loadFeatures = () =>
import("./features.js").then(res => res.default)
// This animation will run when loadFeatures resolves.
function App() {
return (
<LazyMotion features={loadFeatures}>
<m.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
/>
</LazyMotion>
)
}

#Developer tools

Because the normal motion component still preloads all of its functionality, including it anywhere will break the benefits of using LazyMotion.

To help prevent this, the strict prop can be set on LazyMotion. If a motion component is loaded anywhere within, it will throw with a reminder to render the m component instead.

function App() {
// This will throw!
return (
<LazyMotion strict>
<motion.div />
</LazyMotion>
)
}
PreviousAccessibilityNextUpgrade guides
On this page
  • How to reduce the size of the motion component
  • Available features
  • Synchronous loading
  • Lazy loading
  • Developer tools

Copyright © 2022 Framer B.V.

  • Security
  • Terms of Service
  • Privacy Statement