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.

All language files are loaded on first request when using SWC

See original GitHub issue

Upgrading 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: Babel

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

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:closed
  • Created 2 years ago
  • Reactions:17
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
derkoecommented, Jun 3, 2022

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.js

module.exports = {
  locales: ["en", "de-AT", "sk-SK"],
  defaultLocale: "en",
  pages: {
    "*": ["my-bundle"],
  },
  loader: false,
  localeDetection: false,
};

pages/app.tsx

import "../styles/globals.css";
import type { AppProps } from "next/app";
import appWithI18n from "next-translate/appWithI18n";
import i18nConfig from "../i18n";
import merge from 'lodash/merge';

function MyApp({ Component, pageProps }: AppProps) {
  return <Component {...pageProps} />;
}

export default appWithI18n(MyApp as any, {
  ...i18nConfig,
  loadLocaleFrom: (locale, ns) => {
    const englishTexts = import(`../locales/en/${ns}.json`);
    let countrySpecific = Promise.resolve({});
    let languageOnly = Promise.resolve({});
    if (locale) {
      countrySpecific = import(
        `../locales/${locale.replace("-", "_")}/${ns}.json`
      ).catch(() => ({}));
      languageOnly = import(
        `../locales/${locale.substring(0, 2)}/${ns}.json`
      ).catch(() => ({}));
    }
    return Promise.all([englishTexts, countrySpecific, languageOnly]).then(
      ([en, country, language]) => merge({}, en, country, language)
    );
  },
});
3reactions
alisakovacommented, Jan 18, 2022

hi! we have the same issue in our team

Read more comments on GitHub >

github_iconTop 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 >

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