npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

next-fbt

v0.5.0

Published

Easily integrate FBT with Next.js apps.

Downloads

7

Readme

next-fbt

Easily integrate FBT with Next.js apps.

Useful links

Setup

  1. Install.

    npm install next-fbt
    npm install -D next-fbt-cli next-fbt-babel
  2. Create next-fbt.config.js.

    /** @type {import('next-fbt').Config} */
    module.exports = {
      // Regular configuration for `i18n` key for Next's config.
      i18n: {
        // These are all the locales you want to support in
        // your application.
        locales: ['en-US', 'pl-PL', 'es-ES'],
        // This is the default locale you want to be used when visiting
        // a non-locale prefixed path e.g. `/hello`.
        defaultLocale: 'en-US',
        // Always use the locale that is specified in url
        // without redirecting to the one detected by the browser.
        localeDetection: false,
      },
    
      // Configuration for 'next-fbt' and 'next-fbt-cli'.
      nextFbt: {
        // The root url your want your translation
        // files to be served from.
        // The pathname of `publicUrl` (here `i18n`) determines
        // the directory name (inside ./public) where files with translations
        // will be put.
        publicUrl: 'http://localhost:3000/i18n',
        // Split translations by file path (relative to the CWD).
        // Must be array of [string, string[]].
        groups: [
          // Example:
          ['components', ['src/components/*']],
          ['home-page', ['pages/index.tsx']],
          ['other-pages', ['pages/*', 'src/pages/*']],
    
          // Above configuration will result in:
          // - all translations under "src/components" directory will land
          //   in 'public/i18n/<locale>/components.json
          // - all translations from "pages/index.tsx" file will land
          //   in 'public/i18n/<locale>/home-page.json
          // - all translations from "pages" directory (but not from "pages/index.tsx" file)
          //   will land in 'public/i18n/<locale>/other-pages.json
          // - translations from files that don't match any of above
          //   patterns will be extracted to 'public/i18n/<locale>/main.json'
        ],
      },
    };
  3. Update Babel config.

module.exports = {
-  presets: ['next/babel'],
+  presets: ['next-fbt-babel/preset'],
+  plugins: ['next-fbt-babel/plugin'],
};
  1. Wrap next config and pass options.

    // next-config.js
    const { withNextFbtConfig } = require('next-fbt/config');
    const nextFbtConfig = require('./next-fbt.config');
    
    module.exports = withNextFbtConfig({
      i18n: nextFbtConfig.i18n,
      nextFbt: nextFbtConfig.nextFbt,
    
      // ^ you can also just `...nextFbtConfig`
    
      /* the rest of the config */
    });

    next-fbt.config still has to be a CommonJS

    // next-config.mjs
    import { withNextFbtConfig } from 'next-fbt/config';
    import nextFbtConfig from './next-fbt.config.js';
    
    export default withNextFbtConfig({
      i18n: nextFbtConfig.i18n,
      nextFbt: nextFbtConfig.nextFbt,
    
      /* the rest of the config */
    });
  2. Wrap app with provider.

    // pages/_app.tsx
    import { appWithNextFbt } from 'next-fbt';
    
    function App({ Component, pageProps }) {
      return <Component {...pageProps} />;
    }
    
    export default appWithNextFbt(App);
    // pages/_app.tsx
    import NextApp from 'next/app';
    
    export default appWithNextFbt(NextApp);
    // pages/_app.tsx
    import { NextFbtProvider } from 'next-fbt';
    
    function App({ Component, pageProps }) {
      // This is basically the same what `appWithNextFbt` does.
      return (
        <NextFbtProvider __NEXT_FBT_PROPS__={pageProps.__NEXT_FBT_PROPS__}>
          <Component {...pageProps} />
        </NextFbtProvider>
      );
    }
    
    export default App;
  3. Fetch translations for your page.

    Notice, that for the translations to be fetched, the file you declare the fetching logic has to match any group from the config file.

    import { getPropsFetcher } from 'next-fbt';
    
    export default function Page() {
      // your regular page component
    }
    
    export const { getServerSideProps } = getPropsFetcher(import.meta.url);
    // same as above
    
    - export const { getServerSideProps } = getPropsFetcher(import.meta.url);
    + export const { getStaticProps } = getPropsFetcher(import.meta.url);
    import { getProps } from 'next-fbt';
    
    export function getServerSideProps(ctx) {
      // your logic for `yourProps`...
    
      const fbtProps = await getProps(ctx, import.meta.url);
    
      return {
        props: {
          ...fbtProps,
          ...yourProps,
        },
      };
    }

Lazy-loading for dynamic components

The library has first-class support for lazy loading of translations for dynamic components (via next/dynamic).

  1. Replace next/dynamic with next-fbt/dynamic.

    This is a little wrapper around next/dynamic that fetches required translations for components that is going to be rendered.

    // Notice that difference in the import source (`next/dynamic` => `next-fbt/dynamic`).
    import dynamic from 'next-fbt/dynamic';
    
    // Limitation: the wrapper can be used only with components that are rendered on the client.
    // If you wish to have SSR-rendered component, use regular `next/dynamic` and fetch translations
    // via `getServerSideProps` / `getStaticProps`.
    const Component = dynamic(() => import('/path/to/component'), { ssr: false });
    
    export function Page() {
      return (
        // The component will be rendered after translations are fetched.
        <Component />
      );
    }
  2. Add a property on a component so next-fbt/dynamic knows what to fetch.

    If you use memo or forwardRef the assignTranslations has to wrap the memoized/ forwarded component, since memo and forwadRef loose non-React properties on a component.

    import { assignTranslations } from 'next-fbt';
    
    export default function DynamicComponent() {
      return (/* some jsx */)
    }
    
    assignTranslations(DynamicComponent, import.meta.url);

Collecting FBT's for translations

npx next-fbt-collect

This will output .cache/next-fbt/source-strings.json file which you can upload to translations service that supports translating FBT's (like Crowdin).

Creating translation files

After you translate the source string to other languages, download the translations to /path/to/downloaded and run

npx next-fbt-translate /path/to/downloaded

This will create public/i18n directory with translation files.