Pre-loading custom fonts in Next.js for a smoother user experience

I am currently utilizing next.js.

My objective is to ensure that the fonts are loaded before any content is displayed on the screen.
I attempted to achieve this by including them in the Head component within the _.document file using the rel="preload" attribute.

<link
        rel="preload"
        href="/public/fonts/Mulish-ExtraBold.ttf"
        as="font"
        type="font/ttf"
        crossOrigin="anonymous"
      ></link>

To do this, I created a public directory at the root level with a font sub-directory inside it. Then, in the link tag, I specified the href attribute like shown above:

href="/public/fonts/Mulish-ExtraBold.ttf"
.

href="/public/fonts/Mulish-ExtraBold.ttf"

However, it appears that this setup is not functioning as anticipated.

The code for my _.document.tsx file is as follows:

import Document, { Head, Html, Main, NextScript } from "next/document";
import { ServerStyleSheet } from "styled-components";

export default class MyDocument extends Document {
  render() {
    return (
      <Html>
        <Head>
          <link
            rel="preload"
            href="/public/fonts/Mulish-ExtraBold.ttf"
            as="font"
            type="font/ttf"
            crossOrigin="anonymous"
          ></link>
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }

What could be the issue here?

Answer №1

To resolve the problem, simply get rid of the /public part

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

ReactJS does not recognize the library mentioned

click here to access the pondjs folder inside node-modulesAfter installing pondjs(https://www.npmjs.com/package/pondjs) using npm install pondjs --save in my react application and confirming its presence in package.json, I encountered an issue in App.js. W ...

Adjusting various angular-cli configuration files or providing input variables

My application caters to different customers, requiring personalized configurations based on their needs. I am looking for a way to customize the settings in the angular-cli.json file each time I run ng build. Is there a method to: 1) Dynamically cha ...

Ajax transmits an array of data to the server

My table contains dynamic data with input fields for each item. I need to send the input field values along with the item's ID to the backend. Encountered Issue When making an AJAX request, the data sent as an array is not coming out as expected. Cod ...

Resolving issues with JavaScript onchange functionality

When I try to use both of these scripts (inline and external) together, only the second one seems to be working while the first one does not. The first script (inline) is responsible for submitting the form and loading the list: <select id="my-sel ...

What is the method for obtaining a Token in next-auth?

I am facing an issue with implementing login logic for my credentials. Despite using the useSession hooks, I am unable to access the token required for communicating with backend REST API endpoints. I have searched extensively, read resources, and even sou ...

Certain cases will see success with iPhone jquery/ajax functionality, while others may encounter

I am facing an issue with multiple pages in my project that utilize AJAX to submit a form to django. While the buttons work perfectly on various platforms and browsers like Chrome, Firefox, and desktop Safari, they seem to malfunction specifically on mobil ...

Struggling to get JQuery timing events to function properly?

Encountering timing issues with the events in my self-made simon memory game. Experimented with setTimeout but struggling to figure out which events to time and the appropriate duration for them to be distinguishable. $('#play').click(function( ...

Resolve feature for UI routes fails to function upon refreshing the page

My app utilizes UI Route for view routing. When accessing /berlinerliste/, a function is triggered to display an array of objects. If one of these objects is clicked, the view changes to /berlinerliste/{id}/ and shows the details of that specific object. ...

How can I make a row div taller?

Looking at the livelink and code provided below, I have successfully created a responsive grid layout. The last adjustment that needs to be made is to change some of the squares from square shape to rectangular. The top two squares in the second column an ...

Redirecting with Django using specific element identifier

We are attempting to utilize JavaScript to redirect to our checkout.html page, which requires an ID in its arguments for access. This script is embedded within our store.html page. {% for item in product %} <div class="col-md-3"> ...

Revolutionizing user experience: New feature seamlessly triggers button actions with the power of "enter"

I need assistance with modifying a form that contains two buttons and several text inputs. Currently, if the enter key is pressed, it triggers a click event on the first button. However, I want to modify this behavior so that if any of the text boxes are f ...

Positioning a div around a multi-layered canvas for a centered effect

I am looking to create a unique HTML5 canvas setup where multiple canvases are stacked on top of each other to allow for rendering with different layers using JavaScript. The challenge is to have these canvases centered both horizontally and vertically on ...

What steps should I take to eliminate the gap between two side panel components?

Let's take a look at the concept I am working on: https://i.sstatic.net/RyQAB.png I'm aiming to extract the statistics element within the red outline. The spacing issue between the stats and balance panels perplexes me. To align these panels to ...

Adjust the colors of two elements by clicking a button's onclick event

As stated in the title, I have a button in the books component. When this button is clicked, the color of a div within the books component and another div in the navbar component should change. - Below is the code for the books component : export class Bo ...

How to override an event in JavaScript when a specific value is entered

I am looking to implement a system with complex conditions. The goal is to have an alert appear when a value is inputted and the button is clicked. First, a confirmation message Have you input ? should be displayed, followed by clicked with input. If no va ...

The new Model.save() method in Mongoose v6.2.7 is experiencing issues and none of the attempted solutions such as promises, callbacks, or async await in try

At first, the project was established to support promises, and all queries utilized promise-like methods such as method.then().catch(). However, some were later switched to try-catch with async await. Everything functioned properly until a few weeks ago wh ...

Tips for Ensuring Element Width is Maintained Despite Border

My webpage layout is using the fantastic Twitter Bootstrap framework and I am trying to add a stylish CSS3 Box Shadow effect to a column. However, I have encountered an issue where the box shadow's 1px border is causing my second column to wrap onto ...

How can I place text on top of an image with a filter using Bootstrap?

I am facing a challenge of overlaying text on an image with a tinted background color and opacity. While working with Bootstrap 4, I suspect that some classes might be conflicting with each other. This is my current setup: Index.cshtml: <div class=" ...

What is the best way to incorporate vertical scrolling into a React material table?

I'm having trouble getting vertical scroll to work with my material table in React Typescript. Horizontal scroll is functioning properly for large data, but I'm stuck on implementing the vertical scroll. Here's my code: {isLoading ? ...

Identify and automatically switch to the mobile site following a selection

Seeking a way to incorporate a redirect on my Joomla site that leads users to a mobile-friendly HTML5 app. After much thought, I've put together the following script: <script type="text/javascript> <!-- if (screen.width <= 800) { ...