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

后端 未结 6 1856
既然无缘
既然无缘 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 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 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) => (
      
    // Other layout/components
    ); 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) => (
      
    // Other layout/components
    );

提交回复
热议问题