Next.js Discord

Discord Forum

`next/local/font` not applying when using Dynamic Routing

Unanswered
Great Black-backed Gull posted this in #help-forum
Open in Discord
Great Black-backed GullOP
1. What I'm trying to accomplish
I am using next/local/font using Next 12 and tailwindCSS to apply this font at all pages, even the pages with Dynamic Routes.
My problem is that my local fonts are not applying when I move to a dynamic routing page, sepecifically only on the first visit

2. My code
// pages/_app.tsx
// fonts are in the pages/Fonts folder
const gmarket = localFont({
  src: [
    {
      path: './fonts/GmarketSansLight.woff',
      weight: '400',
    },
    {
      path: './fonts/GmarketSansMedium.woff',
      weight: '600',
    },
    {
      path: './fonts/GmarketSansBold.woff',
      weight: '700',
    },
  ],

  variable: '--gmarket',
});

function MyApp({
  Component,
  pageProps: { session, ...pageProps },
}: AppProps<{ session: Session }>) {
  return (
    <>
      <main className={`${gmarket.variable} font-sans`}>
        <Component {...pageProps} />
      </main>
    </>
  );
}

export default MyApp;


3. logs/errors I am receiving
There's no error in my code, but when I move to a dynamic routing page in my application, my local fonts are not applying. It only happens at the first time when I move to a dynamic routing page. After the first visit, the local fonts are applying right.

4. What I've tried to solve the issue
- I tried to put font-display: 'swap' property, but it's causing FOUT which I don't want this to be happened.
- I tried to put local fonts in the style tag in the _app.tsx, but not woking.

Also, I put my local fonts variable in the tailwind.config.js
theme: {
    extend: {
      fontFamily: {
        sans: ['var(--gmarket)'],
      },
    }
}

but it doesn't work. It works when I put this fontFamily inside of theme not extend

Is there any way to have my local fonts applied in every pages, including pages with the dynamic routes?

12 Replies

hey 👋 first of all thanks for writing the question so well formatted. since you are using the pages folder, I recommend moving the fonts outside this folder, otherwise they could be mistakenly be used to create endpoints in the app (like app.com/fonts/...)
this is probably not the fix to your issue, which I believe would require a reproduction since it kinda looks like a bug with next
@Rafael Almeida this is probably not the fix to your issue, which I believe would require a reproduction since it kinda looks like a bug with next
Great Black-backed GullOP
wow! thanks for your help. 😆
so if it's a bug with next, there is no way that I can fix this issue by myself, right? should I post this issue on the github issue?

I'm new to this situation, so I'm not sure if there's anything that I can try or do...

thanks a lot!
Great Black-backed GullOP
ok, thank you so much!!
Great Black-backed GullOP
hello again, @joulev @Rafael Almeida

I have attempted to update my project's Next.js version to next/canary, but I am still experiencing the FOUT issue. Consequently, I have decided to submit an issue regarding this matter. While attempting to create a minimal reproduction in the reproduction-template (which is located in a different directory from my project), I discovered that it works correctly. The main difference I observed is the folder directory structure. In the reproduction-template, the pages directory is not directly under the src folder, whereas in my project, the pages directory is located within the src folder.

Does this imply that the problem has been resolved? Should I update my project's directory structure to match that of the reproduction template?
Or should I still need to make an issue about it?

Thank you.
Great Black-backed GullOP
so when I submit reproduction code, should I change the directory just like my project? my project is using next version 12 so the directory is not the same as reproduction template.
you just need to make a repo that can reproduce the problem in minimal amount of code
how you structure it is for you to decide
if it is only reproducible with src dir on, then use it
Great Black-backed GullOP
oh ok! thank you so much