How to add a favicon to a Next.js static site?

后端 未结 6 1854
既然无缘
既然无缘 2021-02-03 20:52

I\'m trying to add a favicon to a Next.js static site without much luck.

I\'ve tried customising the document with components from \'next/document\' https:/

相关标签:
6条回答
  • 2021-02-03 20:55

    Only adding .ico file is not enough.

    Add link tags to <Head> section in _document.jsx or _document.tsx The question is only about .ico file, but I would recommend to add different dimensions and formats for better support.

    import React from 'react';
    import Document, { Html, Head, Main, NextScript, DocumentContext, DocumentInitialProps } from 'next/document';
    
    class MyDocument extends Document {
      static async getInitialProps(ctx: DocumentContext): Promise<DocumentInitialProps> {
        const initialProps = await Document.getInitialProps(ctx);
        return { ...initialProps };
      }
    
      render(): React.ReactElement {
        return (
          <Html>
            <Head>
              <link rel="apple-touch-icon" sizes="180x180" href="/favicons/apple-touch-icon.png" />
              <link rel="icon" type="image/png" sizes="32x32" href="/favicons/favicon-32x32.png" />
              <link rel="icon" type="image/png" sizes="16x16" href="/favicons/favicon-16x16.png" />
              <link rel="manifest" href="/favicons/site.webmanifest" />
              <link rel="mask-icon" href="/favicons/safari-pinned-tab.svg" color="#5bbad5" />
              <meta name="msapplication-TileColor" content="#ffc40d" />
              <meta name="theme-color" content="#ffffff" />
            </Head>
            <body>
              <Main />
              <NextScript />
            </body>
          </Html>
        );
      }
    }
    
    export default MyDocument;
    

    You can generate different icons using RealFaviconGenerator and upload results to /public/favicons/ folder. This folder can be referenced by /favicons/ due to nature of public directory.

    0 讨论(0)
  • 2021-02-03 21:06

    In my case it DID NOT WORK WITH OUT THE IMPORT:

    file: _app.tsx

    
        import { AppContext, AppProps } from "next/app";
        import "../styles/common.scss";
        import Head from 'next/head';
        //For me it didn't work without the following import...
        import favico from "../static/favicon.ico";
        
        
        function MyApp({ Component, pageProps }: AppProps) {
          const csrfToken = pageProps["anti-csrftoken-a2z"];
          return (
            <div>
              <Head>
                <link rel="shortcut icon" href={favico} type="image/x-icon" />
              </Head>
              <Component {...pageProps} />
            </div>
          );
        }
        
        MyApp.getInitialProps = async ({ Component, ctx }: AppContext) => {
          let pageProps = {};
          if (Component.getInitialProps) {
            pageProps = await Component.getInitialProps(ctx);
          }
          return { pageProps };
        };
        
        export default MyApp;
    
    
    0 讨论(0)
  • 2021-02-03 21:07

    Simply added your favicon.ico in public folder, Next js will automatically take that favicon for all pages.

    No need to add any favicon link to any pages or in tag no need to add link for favicon.

    https://github.com/zeit/next.js/blob/master/errors/static-dir-deprecated.md

    0 讨论(0)
  • 2021-02-03 21:14

    As of June 2020, you don't have to add/edit the document.js or _head.js files. All you need do to is place the favicon.ico file inside the public directory and that's it.

    0 讨论(0)
  • 2021-02-03 21:16

    The accepted answer is nice, but might be worth pointing out that you don't have to modify _document.js for adding a favicon (nor for adding any tags to head).

    I found out for myself that placing favicon in _app.js makes more sense. This file is most likely to exist already for setting up a layout for the pages or something like this. And you can add Head tag literally anywhere (see the docs)

    So I ended up with _app.js

    class MyApp extends App {
      render() {
      const { Component, pageProps } = this.props;
      return (
        <Layout>
          <Head>
            <link rel="shortcut icon" href="/favicon.ico" />
          </Head>
          <Component {...pageProps} />
        </Layout>
      );
    }
    
    0 讨论(0)
  • 2021-02-03 21:17
    1. Create a /static folder in project root. This will be added to the static export folder.
    2. Add favicon file in /static folder.
    3. Add _document.js to /pages/ folder according to documentation (nextjs.org) or documentation (github.com).
    4. Add <link rel="shortcut icon" href="/static/favicon.ico" /> to head.
    5. npm run build && npm run export

    P.S. Thanks to the previous answer that was deleted. It works!


    Edit: Another way is to do this is to import Head into your root layout and add the link there. Anything added to Head gets inserted into the document head tag.

    import Head from 'next/head';
    
    const Page = (props) => (
      <div>
        <Head>
          <link rel="shortcut icon" href="/static/favicon.ico" />
        </Head>
        // Other layout/components
      </div>
    );
    
    export default Page;
    

    Update :

    The static directory has been deprecated in favor of the public directory. Doc

    So, the code would now look like

    import Head from 'next/head';
    
    const Page = (props) => (
      <div>
        <Head>
          <link rel="shortcut icon" href="/favicon.ico" />
        </Head>
        // Other layout/components
      </div>
    );
    
    0 讨论(0)
提交回复
热议问题