Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advantage over directly using it #8

Closed
deadcoder0904 opened this issue Jul 5, 2020 · 3 comments
Closed

Advantage over directly using it #8

deadcoder0904 opened this issue Jul 5, 2020 · 3 comments

Comments

@deadcoder0904
Copy link

I can't understand the advantage of this package over directly using what Next JS offers. I mean I can do it like the following:

_document.tsx

import React from 'react'
import Document, { Html, Head, Main, NextScript } from 'next/document'

class MyDocument extends Document {
  render() {
    return (
      <Html lang="en">
        <Head>
          <link
            rel="preload"
            href="/fonts/Neoneon.otf"
            as="font"
            crossOrigin=""
          />
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}

export default MyDocument

_app.tsx

import React from 'react'
import { MDXProvider } from '@mdx-js/react'
import { mdComponents } from '../components/mdx'
import { AppProps } from 'next/app'

const MyApp = ({ Component, pageProps }: AppProps) => {
  return (
    <MDXProvider components={mdComponents}>
      <Component {...pageProps} />
      <style jsx global>{`
        @font-face {
          font-family: 'Neoneon';
          src: url('/fonts/Neoneon.otf');
          font-weight: bold;
          font-style: normal;
          font-display: swap;
        }
      `}</style>
    </MDXProvider>
  )
}

export default MyApp

index.tsx

import Link from 'next/link'

const IndexPage = () => (
  <>
    <h1>Akshay Kadam</h1>
    <p>
      <Link href="/about">
        <a>About</a>
      </Link>
    </p>
    <style jsx>{`
      h1 {
        font-family: 'Neoneon';
      }
    `}</style>
  </>
)

export default IndexPage

So what does this package offers differently?

@rohanray
Copy link
Owner

Please refer rohanray/next-fonts#34

@deadcoder0904
Copy link
Author

Makes sense :)

@EricVanDerDijs
Copy link

EricVanDerDijs commented Nov 28, 2020

Hi @rohanray , I found out that this package (alongside with next-images) is the best way to go if you intend to use react-native-web on nextjs without adding expo to your project. This is because (at least in my case) a broadly used library for RN (react-native-vector-icons) which is the peer dependency of several UI libraries requires you to import their .ttf files to your next Head component to decalre the font-icon used by the UI components.

Also if you are using Typescript you will need to add type definitions for your font files on your next-env.d.ts file like this:

./path/to/my/types/next-fonts.d.ts

declare module "*.ttf" {
  const value: string;
  export = value;
}

next-env.d.ts (add the following line)

/// <reference types="./path/to/my/types/next-fonts" />

My previous workaround was to copy the file from node_modules to my public folder and add that script to package.json posinstall and before run dev and build, which seemed to me kind of hacky.

If I knew this earlier it would have saved me a lot of time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants