question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

optimizeCss incorrect behavior in Next.js 12 (Performance decrease)

See original GitHub issue

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System: Platform: darwin Arch: x64 Version: Darwin Kernel Version 21.2.0: Sun Nov 28 20:28:54 PST 2021; root:xnu-8019.61.5~1/RELEASE_X86_64 Binaries: Node: 16.10.0 npm: 7.24.0 Yarn: N/A pnpm: N/A Relevant packages: next: 12.1.6 react: 17.0.2 react-dom: 17.0.2 critters: 0.0.7

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

Here is the official documentation about how to use a CDN with Asset Prefix:

https://nextjs.org/docs/api-reference/next.config.js/cdn-support-with-asset-prefix

However, in Next.js 12 the publicPath of critters changed to use assetPrefix:

v10.2.3 vs v12.2.2

const Critters = require('critters')
const cssOptimizer = new Critters({
  ssrMode: true,
  reduceInlineStyles: false,
 path: renderOpts.distDir,
- publicPath: '/_next/',
+ publicPath: `${renderOpts.assetPrefix}/_next/`,
  preload: 'media',
  fonts: false,
  ...renderOpts.optimizeCss,
})

Now every page request is downloading the CSS from the CDN before calculate the critical CSS to inline, and this has a significant impact on page latency:

[32mInlined 5.76 kB (38% of original 15 kB) of https://cdn.vinissimus.com/front/_next/static/css/d9c4e8c06d2c316e.css.
Time 465.162456

image

Expected Behavior

Even if I use a CDN in my project, to access the CSS you would have to access the /_next directly as before, it doesn’t make sense to fetch it from a CDN.

Link to reproduction

https://stackblitz.com/edit/vercel-next-js-qqua8l?file=next.config.js

To Reproduce

In the link to the reproduction you need to change assetPrefix inside next.config.js to a correct CDN (hard to reproduce it without it). Then yarn build && yarn start and check the logs and performance.

Without the link to reproduction (all the process):

  1. Activate optimizeCss on next.config.js and add a CDN inside assetPrefix:
{
  assetPrefix: cdnUrl,
  experimental: { optimizeCss: true }
}
  1. yarn add critters@0.0.7
  2. Add some CSS in some page
  3. yarn build && yarn start
  4. Check the logs and performance.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
fraitagcommented, Oct 11, 2022

Are you shure? I check code of next 12.3.1 and critters and i found place when assetPrefix is replaced. https://github.com/GoogleChromeLabs/critters/blob/main/packages/critters/src/index.js#L242

    if (normalizedPath.indexOf(pathPrefix) === 0) {
      normalizedPath = normalizedPath
        .substring(pathPrefix.length)
        .replace(/^\//, '');
    }

on normalizedPath i have https://cos/_next/static/css/5749cf13b9f9d25a.css on pathPrefix i have https://cos/_next/

on result in normalizedPath i have static/css/5749cf13b9f9d25a.css

I think i your case is not problem with using files from cdn becouse is getted from disk.

0reactions
baraeb92commented, Nov 12, 2022

We have experienced this as well.

There is a spike in the CPU usage when using optimizeCSS. As it keep on requesting the CSS file on every load.

screen_shot_2022-10-04_at_11.54.09.pngscreen_shot_2022-10-04_at_13.10.35.pngscreen_shot_2022-10-05_at_13.53.11-1.png

@SukkaW @janis

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Features: Next.js Compiler
The Next.js Compiler, written in Rust using SWC, allows Next.js to transform and minify your JavaScript code for production. This replaces Babel for...
Read more >
Basic Features: Image Optimization
Some of the optimizations built into the Image component include: Improved Performance: Always serve correctly sized image for each device, using modern image ......
Read more >
Going to Production
Before taking your Next.js application to production, here are some recommendations to ensure the best user experience.
Read more >
Blog - Next.js 12.1
Faster Minification with SWC (RC): 7x faster minification than Terser. Self-Hosting Improvements: ~80% smaller Docker images. React 18 & Server ...
Read more >
Upgrade Guide
Upgrading from 12 to 13. To update to Next.js version 13, run the following command using your preferred package manager ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found