Repository navigation
perf(motion): lazy-load framer-motion so non-animated routes pay zero… - #706
Merged
Merged
Conversation
|
@zintarh is attempting to deploy a commit to the zintarh Team on Vercel. A member of the Team first needs to authorize it. |
|
@AnnieIj Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
… cost Problem ------- The app shipped two animation libraries (framer-motion and motion/react). The full animation runtime loaded on every route regardless of whether the route used any animation at all (zintarh#651). Changes ------- 1. Consolidate to one library Replace motion/react imports in ShareCard.tsx and ShareButtons.tsx with framer-motion. Both packages are the same code at v12; one canonical import makes bundle-analyser output meaningful. 2. LazyMotion provider (app/components/MotionProvider.tsx) Wraps children in framer-motion's LazyMotion, moving the domAnimation feature-set (~50 kB) into a separate async chunk fetched on demand. Reduced-motion shortcut: when prefers-reduced-motion is set the features loader returns Promise.resolve({}) so the animation chunk is never fetched at all for those users. 3. Central re-export (app/utils/motionExports.ts) Re-exports m and AnimatePresence from framer-motion so components participating in LazyMotion import from one place. 4. useReducedMotion hook (app/hooks/useReducedMotion.ts) Pure, SSR-safe hook that tracks prefers-reduced-motion. Drives the skip-loading decision in MotionProvider. 5. optimizePackageImports in next.config.ts Tells Next.js to tree-shake framer-motion per chunk. Effect ------ - Routes importing no animated components pay zero animation runtime cost. - Animated routes share a single async domAnimation chunk loaded once per session, not once per route. - Users with prefers-reduced-motion: reduce never download the chunk. Tests ----- 9/9 MotionProvider unit tests pass. Closes zintarh#651
AnnieIj
force-pushed
the
perf/651-lazy-motion-v2
branch
from
September 27, 2026 23:07
a26965d to
a370084
Compare
Collaborator
|
Thanks for the contribution! This PR conflicted with recent changes on
Resolved and merged with PRmaster. Your commits are included in the merge. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Solution
Consolidate to one library Replace all 7 motion/react imports with ramer-motion. Both packages are the same code; one canonical import makes the bundle-analyser measurement meaningful and avoids duplicate chunks.
LazyMotion provider (app/components/MotionProvider.tsx) Wraps the locale layout in framer-motion's LazyMotion, which moves the domAnimation feature-set (~50 kB) into a separate async chunk that is fetched on demand rather than inlined into every route.
Reduced-motion shortcut: when prefers-reduced-motion is set the features loader is replaced with () => Promise.resolve({}), so the animation chunk is never fetched at all for those users.
Central re-export (app/utils/motionExports.ts) Re-exports m and AnimatePresence from framer-motion so components that want to participate in LazyMotion import from one place.
Switch 5 route pages to m.* API withdraw, governance, export-csv, offers, loading These pages used full motion.* components. Switching to m.* means the feature bundle is shared via LazyMotion rather than each page independently pulling it in.
optimizePackageImports in next.config.ts Tells Next.js to tree-shake framer-motion per chunk so only the symbols actually imported end up in each route's JS.
Effect
Tests
9/9 MotionProvider unit tests pass:
MOTION_FEATURES_LOADER (2 tests) reduced-motion skip path (3 tests) getPrefersReducedMotion (4 tests)
Closes #651