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

后端 未结 6 1946
既然无缘
既然无缘 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 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 {
        const initialProps = await Document.getInitialProps(ctx);
        return { ...initialProps };
      }
    
      render(): React.ReactElement {
        return (
          
            
              
              
              
              
              
              
              
            
            
              
    ); } } 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.

提交回复
热议问题