vercel / next.js

The React Framework
https://nextjs.org
MIT License
121.28k stars 25.95k forks source link

Only add optimize packages to transpile packages during production build #65245

Closed aovens-quantifi closed 2 weeks ago

aovens-quantifi commented 2 weeks ago

What?

63537 introduced an issue where compilation of a new route (like an api route) on the pages router during local webpack dev caused the hash of _document.js to change, forcing active hmr pages to do a full page refresh.

This PR simply forces the above-mentioned PR to only apply during production builds and not during dev (likely the time you care about optimizing imports anyway)

Why?

The above mentioned bug was causing issues for our local dev as the page would refresh as soon as we hit an api route

aovens-quantifi commented 2 weeks ago

@ijjk I don't know how to make the CI run

aovens-quantifi commented 2 weeks ago

I could add a test case for this code but I'm not sure I could add one for the issue I was having. I entered the issue in discord but not on GitHub. Adding here for posterity:

My next app (v14.2.3, pages) is performing a page refresh on the first instance of a fetch to an api route. This started happening after upgrading from 14.0.4 to 14.2.2 (more specifically from 14.1.0 to 14.2.0). I have isolated the server code to this line: https://github.com/vercel/next.js/blob/a0df9860c4f94508f60514d71f4513922ffd025d/packages/next/src/server/dev/hot-reloader-webpack.ts#L1319

Based on my reading, it is determining the _document.js content is changing, however I have made no changes to the code when this happens

aovens-quantifi commented 2 weeks ago

Here is the difference between the _document.js chunks before and after the api call:

image