Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Significant bundlesize increase with framer-motion@5 while using LazyMotion #1331

Closed
Tracked by #1200 ...
paales opened this issue Nov 3, 2021 · 11 comments
Closed
Tracked by #1200 ...
Labels
bug Something isn't working

Comments

@paales
Copy link

paales commented Nov 3, 2021

1. Read the FAQs 馃憞

2. Describe the bug

With the upgrade to framer/motion@5 we're seeing a significant bundle size increase. I understand that the new version offers lots of cool functionality so that there is an increase in bundle size could be expected. I wouldn't expect his however when using LazyMotion,

Page Size old Size new Size diff First load old First load new First load diff
/ 2.1kB 2.1kB 242kB 249.0kB +7kB馃毃
/_app 152kB 164.0kB +12kB馃毃
/[...url] 4.3kB 4.3kB 260kB 266.0kB +6kB馃毃
/404 6.4kB 6.3kB -0.1kB 228kB 235.0kB +7kB馃毃
/about/[url] 2.7kB 2.7kB 223kB 230.0kB +7kB馃毃
/account 15.0kB 13.6kB -1.4kB 210kB 220.0kB +10kB馃毃
/account/addresses 12.5kB 9.8kB -2.7kB 220kB 224.0kB +4kB鈿狅笍

We're already using the LazyMotion feature and I would thus expect for the bundle size not too increase significantly.

4. Steps to reproduce

Steps to reproduce the behavior:

  1. Upgrade to framer motion 5
  2. See the difference in bundlesize
  3. See error

5. Expected behavior

Bundle size is not increased, and the additional features are loaded lazily

6. Video or screenshots

Not applicable

7. Environment details

Not applicable

@paales paales added the bug Something isn't working label Nov 3, 2021
@mattgperry
Copy link
Collaborator

I agree, shouldn't be this much. How have you configured LazyMotion ?

@paales
Copy link
Author

paales commented Nov 3, 2021

I'm using nextjs and have it configured like this, should be correct, right?

// _app.tsx
import { FramerNextPages } from '@graphcommerce/framer-next-pages'
import { LazyMotion } from 'framer-motion'
import { AppPropsType } from 'next/dist/shared/lib/utils'
import React, { useEffect } from 'react'
import { AppProps } from './types'

export default function App(props: AppProps & AppPropsType) {
  useEffect(() => document.getElementById('jss-server-side')?.remove())

  return (
    <LazyMotion features={async () => (await import('./framerFeatures')).default} strict>
      <FramerNextPages {...props} />
    </LazyMotion>
  )
}
// framerFeatures.tsx
import { domMax } from 'framer-motion'
export default domMax

@mattgperry
Copy link
Collaborator

Hmm yeah looks good to me. I'll take a look, we do have things that track this and there was a 1.5kb bump to the core package but this is obviously much bigger.

@paales
Copy link
Author

paales commented Nov 3, 2021

The TreeView links are also including all sorts of preload scripts, but the main bundle is /_next/static/chunks/pages/_app-****.js

Framer motion 4: TreeView
Schermafbeelding 2021-11-03 om 23 55 30

Framer motion 5: TreeView
Schermafbeelding 2021-11-03 om 23 54 29

@mattgperry
Copy link
Collaborator

Ah thanks that's helpful - it's weird though, that projection script should only be included by the domMax bundle and not the main bundle. When building this locally using https://github.com/framer/motion/blob/main/webpack.size.config.js they get split into a 6.1kb and a 30.3kb bundle but I suppose this isn't quite the same setup as importing from motion and then using a dynamic import.

@paales
Copy link
Author

paales commented Nov 5, 2021

Alright, I'm waiting for nextjs 12.0.3 before moving on with this. maybe it will be better if i use the mjs files. Running into some issues if I switch to esmExternals: vercel/next.js#30750

If you know how to fix it, that is great of course :)

@paales
Copy link
Author

paales commented Nov 9, 2021

@mattgperry I've upgraded to nextjs 12.0.3, but the issue with using esmExternals isn't fixed (running into issues unrelated to framer motion though). The previous version of framer motion also didn't use the esmExternals feature though, so I still believe there is something going on with the latest release.

Anything I can do to help you debug this further?

When building this locally using https://github.com/framer/motion/blob/main/webpack.size.config.js they get split into a 6.1kb and a 30.3kb bundle but I suppose this isn't quite the same setup as importing from motion and then using a dynamic import.

This 'just' builds the different entry points, or am I mistaken? But maybe there is some import in the minimal bundle that is triggering the inclusion of the complete projection bundle?

@paales
Copy link
Author

paales commented Dec 23, 2021

Update: upgraded to the latest version, confirming that the issue is still present:
Schermafbeelding 2021-12-23 om 14 57 40

@paales
Copy link
Author

paales commented Jan 14, 2022

@mattgperry I've got a minimal reproduction:

https://stackblitz.com/edit/nextjs-rx15fu?file=pages%2F_app.tsx

Steps to generate the treemaps:

  1. Download the stackblitz example
  2. npm i
  3. npm run build && npm run start
  4. Run lighthouse
  5. Open the Treemap viewer

Situation 1: LazyMotion is in the _app.tsx but not using framer-motion

Treemap

  • We see a small pages/_app bundle (not even shown by default)
  • We see a small pages/index bundle (not even shown by default)
  • We see the framer-motion chunk (chunks/22 in this case)

Situation 2: LazyMotion in _app.tsx and m used in index.tsx

Treemap

  • We see a small pages/_app bundle (not even shown by default)
  • We see an increate for pages/index bundle, but expected
  • We see the framer-motion bundle now being split up in two parts. Not sure why this is happening, but that probably is the shared part between the projectin bundle and the m component?

So far so good

Situation 3: LazyMotion in _app.tsx AND m used in _app.tsx

Treemap

  • pages/index is back to it's initial size, which is expected, because those parts are loaded in _app.
  • The pages/_app.tsx bundle has gobbled up everything, now including the common parts but more importantly all the projection stuff...

馃く

Any ideas?

@paales
Copy link
Author

paales commented Jul 7, 2022

@mattgperry this might be interesting vercel/next.js#33394 (comment)

@paales
Copy link
Author

paales commented Jul 11, 2022

This fixed the issue! graphcommerce-org/graphcommerce#1550 (comment)

Page Size old Size new Size diff First load old First load new First load diff
/ 0.9kB 2.6kB +1.7kB鈿狅笍 268kB 258.0kB -10kB馃敟
/_app 195kB 180.0kB -15kB馃敟
/[...url] 5.5kB 3.7kB -1.8kB 285kB 275.0kB -10kB馃敟
/404 4.4kB 6.1kB +1.7kB鈿狅笍 258kB 249.0kB -9kB馃敟
/account 12.8kB 12.8kB 242kB 230.0kB -12kB馃敟
/account/addresses 14.4kB 14.4kB 246kB 236.0kB -10kB馃敟
/account/addresses/add 5.4kB 8.4kB +3.0kB鈿狅笍 261kB 251.0kB -10kB馃敟
/account/addresses/edit 1.0kB 1.0kB 264kB 254.0kB -10kB馃敟
/account/authentication 9.7kB 12.4kB +2.7kB鈿狅笍 259kB 249.0kB -10kB馃敟

@paales paales closed this as completed Jul 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants