In a Next.js app, the code is divided into different bundles to enhance performance. These bundles are loaded as JavaScript files when you view the page source of the app.
To break down the code and make it more understandable, let’s use an HTML formatter:
There are four JavaScript files declared in the head section using rel="preload" and as="script":
/_next/static/development/pages/index.js - 96 lines of code.
/_next/static/development/pages/_app.js - 5900 lines of code.
/_next/static/runtime/webpack.js - 939 lines of code.
/_next/static/runtime/main.js - 12k lines of code.
By preloading these files, the browser starts loading them as soon as possible, improving page loading performance. Without this approach, scripts would have an additional delay.
At the end of the body section, these four bundle files are loaded along with /_next/static/development/dll/dll_01ec57fc9b90d43b98a8.js (31k LOC), and a JSON snippet that sets defaults for the page data:
The loaded bundle files already implement a feature called code splitting. For example, the index.js file contains code specific to the index component, which serves the / route. If there were more pages, each page would have its own bundle file that would only be loaded when needed, thus improving the page’s load time.