Part 11: Enhancing Your Blog's Appearance with a Custom Favicon

[Pages] Styling

[Pages] Styling

In our journey to create a visually appealing blog website, we've already explored styling. Now, let's focus on another crucial aspect of your site's appearance: the favicon. A favicon is the small icon displayed in the browser tab next to your page title, and it plays a significant role in your site's branding and user experience.

Understanding the Favicon

When you load a webpage, the browser typically requests a favicon.ico file to display as the tab icon. If this file is missing, you'll often see a generic icon instead. To ensure our website has its unique identity, we need to provide a custom favicon.

Choosing and Preparing Your Favicon

For this example, let's use an emoji symbol as our favicon. You can choose any image you like; just make sure it's clear and recognizable at a small size. After downloading your chosen icon, you'll likely receive it in a zip file containing various sizes and formats. For simplicity, we'll use a single .ico file.

Organizing Your Project with the Public Folder

Next.js simplifies serving static files through the public folder. Any asset placed here is directly accessible in your application. Let's see how it works:

  1. Create and Use a robots.txt File: This file instructs search engines on how to crawl your site. Place it in the public folder to make it accessible at /robots.txt.

    // File: public/robots.txt
    User-agent: *
    Allow: /

    You can verify it by navigating to /robots.txt in your browser, where you should see the content you wrote.

  2. Adding the Favicon: Place your favicon.ico file in the public folder. This enables the browser to find it at the root URL, i.e., /favicon.ico.

Explicitly Declaring the Favicon in Your App

To ensure your favicon is consistently used across all pages, declare it explicitly in your App component using the Next.js Head component. This is how you do it:

// File: pages/_app.js
import Head from 'next/head';
import NavBar from '../components/NavBar';
import '../styles/globals.css';

function App({ Component, pageProps }) {
  return (
    <>
      <Head>
        <link rel="icon" href="/icons/favicon.ico" />
      </Head>
      <header>
        <NavBar />
      </header>
      <Component {...pageProps} />
    </>
  );
}

export default App;

This code places a link element with rel="icon" in the document's head, pointing to your favicon file. The href is set to /icons/favicon.ico, assuming the favicon is in a sub-folder named icons within public.

Organizing Your Static Assets

While the default location for a favicon is at the root, you can organize your public folder with sub-folders like icons, images, or assets. This helps keep your project tidy, especially as you add more files.

  • Moving the Favicon: If you move favicon.ico into an icons sub-folder, update the href in your App component to /icons/favicon.ico.

  • Verifying the Change: Reload your browser to ensure the new path works, and you'll notice the favicon displays correctly without a 404 error.

Conclusion

By adding a custom favicon, you've taken another step toward personalizing your blog website. The public folder in Next.js offers a straightforward way to manage static assets, ensuring your site is not only functional but also visually distinctive. Whether it's favicons, images, or other media, this approach helps you maintain an organized and efficient project structure. Happy developing!

Last updated