All language files are loaded on first request when using SWC
See original GitHub issueUpgrading a medium-sized project to Nextjs 12, we noticed that switching to SWC from Babel increases our bundle size considerably. One culprit seems to be next-translate.
Before the upgrade, language files have their own chunks and only a small part of the “pages/_app” bundle is used by the library:

With SWC (deleted .babelrc), we see all language files included in the app:

Is this a known issue or are we doing something wrong?
We do have a custom fallback in our i18n.js:
module.exports = {
locales: ["en", "de"],
defaultLocale: "en",
loadLocaleFrom: async (lang, ns) => {
const locales = await import(`./locales/${lang}/${ns}.json`).then(
(m) => m.default
);
const defaultLocales =
lang != "en"
? await import(`./locales/en/${ns}.json`).then((m) => m.default)
: {};
return { ...defaultLocales, ...locales };
},
pages: { ... }
};
Issue Analytics
- State:
- Created 2 years ago
- Reactions:17
- Comments:11 (2 by maintainers)
Top Results From Across the Web
All language files are loaded on first request when using SWC ...
Upgrading a medium-sized project to Nextjs 12, we noticed that switching to SWC from Babel increases our bundle size considerably.
Read more >Migrating from Babel to SWC with React - Stack Overflow
After some experimentation I found out that it works if you import React from 'react'; in every file (both the component as well...
Read more >Advanced Features: Next.js Compiler
Automatically sets up transform using SWC; Loading .env (and all variants) into process.env; Ignores node_modules from test resolving and transforms; Ignoring ...
Read more >Migrating to SWC: A brief overview - LogRocket Blog
Let's say you have a simple web application with an HTML file and some JavaScript files. These files are bundled with webpack and...
Read more >Switching a Jest Project from Babel to SWC - Goldblog
Tools such as Speedy Web Compiler (SWC) use lower-level languages such as ... want to see code, check the final pull request's file...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I have found a solution for that. The code splitting works correctly when the imports are directly in “pages/_app”.
Here is an example with complex language fallback logic and “lang-country” locales:
i18n.jspages/app.tsxhi! we have the same issue in our team