Troubleshooting: Vue 3 Vite encountering 404 error when attempting to load fonts from assets using Font Loading API

Attempting to dynamically load fonts from the assets directory within Vue 3 (Typescript) using Vite has led to a 404 error occurring.

https://i.sstatic.net/H3Ho7.png

const fonts = import.meta.glob('@/assets/fonts/*.otf')

console.log(fonts)

async function loadFonts() {
  for (const path in fonts) {
    const fontName = path.split('/')[3].split('-')[0]

    console.log(fontName)

    const myFont = new FontFace(fontName, `url('${path}')`)
    await myFont.load()
    document.fonts.add(myFont)

    console.log(myFont)
  }
}

loadFonts()

https://i.sstatic.net/pVYgo.png

Answer №1

I have successfully implemented your codes into my setup, and everything is functioning correctly.

It seems like the issue might be related to an incorrect path alias for @ in your vite.config.ts file.

The error message indicates that the source path is

http://localhost:3000/assets/fonts/Acumin-Bold.otf
, while the actual path for the fonts should be src/assets/fonts/Acumin-Bold.otf. It appears that the src part of the path is missing.

Here is a snippet from my vite.config.ts for your reference:

resolve: {
  alias: {
    '@': path.resolve(__dirname, './src')
  }
}

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

Guide to animating several objects simultaneously in THREE.js

I am facing an issue where I am trying to add a group of birds to a scene, but only one of them is playing the animation that I loaded. I have tried using THREE.AnimationObjectGroup as mentioned in the documentation, however, I am struggling to make it w ...

Warning: React has reached the maximum update depth limit

I am currently developing a D3 chart using React. Within my chart, I have integrated a brush component that interacts with React Hooks. Here is the snippet of code I am working with: App.js import React, {useEffect, useState} from 'react'; impo ...

Should JavaScript be referenced at the start or generated dynamically?

As I continue working on my small web application, I've noticed that the amount of Javascript is increasing. I'm curious about the best practice for loading/referencing Javascript - should it all be loaded at once at the beginning or dynamically ...

Interacting with a JavaScript alert by clicking the OK button using C# in a web browser control

In my C# Windows Forms program, I am utilizing a webbrowser control to navigate multiple pages of a website and interact with forms to carry out transactions. I initially tried using httpwebrequest and webclient, but encountered challenges with cookies and ...

Having trouble getting my initial Vue.js code to function properly alongside basic HTML

I am in need of assistance with this code as it does not seem to be working properly. I am using an external js file named app.js for the Vue JS code in the index.html file. Here is the code from index.html: new vue({ el: '#app', data: { ...

Utilizing GUID model binding within ASP.NET MVC

Can someone please assist me in resolving this issue? I've been struggling to find the solution and it seems like there's something obvious that I'm missing... The problem I am facing involves sending a GET request from my JavaScript code, ...

I have to display a pop-up message box after selecting an option from a dropdown menu that fulfills a set of conditions

I am attempting to display a pop-up message when a selection is made on a dropdown menu that meets specific criteria. The dropdown list is generated from a coldfusion output query. I am relatively new to JavaScript, so I may be missing something in my code ...

Functionality of JavaScript limited within a form

Here is some HTML code I am working with: <div class = "login"> <form> <input type="text" id="username" name="username" placeholder="Username" style="text-align:center"> <br> <br> <input type="pa ...

Continuously encountering a Login Required message while attempting to upload a file on Google Drive

I am currently developing a chrome extension that is designed to intercept specific downloads, like .doc and .docx files, and automatically upload them to a designated folder in Google Drive. Below is the manifest for this extension: { // Manifest det ...

Failure to Link Different Files Using HTML Links

Organizing the different pages of my website, I initially stored HTML files in a folder called X, with index.html, main.html, and other pages. To streamline access, I decided to separate the articles into folders A, B, and C within folder X. However, this ...

Retrieve the raw text from the input field instead of the date object

Below is the HTML code for an input field: <div class="input-group input-group-md"> <input id="date" name="date" [readOnly]="disabled" type="text" placeholder="M0/d0/0000 Hh:m0:s0" [placeholder]="pl ...

Hovering over a dropdown in jQuery triggers specific behavior

Struggling with getting this dropdown to function properly. The goal is for the dropdown to stay open when hovering over the element that triggered it. I created a function that checks every half-second if the mouse is still hovering over the element. If i ...

The canvas element automatically removes itself after being resized within an HTML5/JavaScript environment

I created a canvas that is interactive and responsive to screen size. However, I encountered an issue where the canvas clears itself when the browser is resized by just one pixel. Is there a way to prevent this from happening? I have included code in my sc ...

I'm facing a production error with Django and Vue.js where nothing appears after attempting to run npm build and linking static files in Django

I successfully connected Django to Vue using the webpack loader. Everything was working fine before running the command `npm build`. After running the command and connecting the static file, when I tried to check `localhost:8000` (default Django server), I ...

Issue with ngModel being undefined after data has finished loading in Ionic 3

As a newcomer to Angular 4, I've been struggling to find a solution for a seemingly simple issue related to an Ionic app. Whenever a user logs in, the entire user object is saved to localStorage. Despite trying various plugins, I settled on a straight ...

Jerky visuals on the canvas

My canvas animation runs smoothly in Chrome, but it's as choppy as a bad haircut in Firefox. Surprisingly, the code is not even that complex. Is there anything in my code that could be causing this slowdown? Here is the jsfiddle link for reference: h ...

Having trouble pinpointing the element with protractor's binding locator

<div class="apiRequestDisplay ng-scope"> <pre class="ng-binding">GET</pre> <pre class="ng-binding">v1/securityprofiles/{securityProfileID} </pre> </div> I am trying to target the specific text within v1/secur ...

What is the proper way to retrieve items from an Array that have an index that is evenly divisible by

let numbers = [4, 5, 7, 8, 14, 45, 76]; function getEvenElements(arr) { let evenArray = []; for (let i = 0; i < arr.length / 2; i++) { evenArray.push(arr[2 * i]); } return evenArray.filter(num => num !== undefined); } alert(getEvenEle ...

What is the method for deactivating body parser json and urlencoded specifically on certain website links?

let shouldParseRequest = function (req) { let url = req.originalUrl; return (url.startsWith('/api/payments/stripe-webhook') || url.startsWith('/uploadimg')); } let parseJSON = bodyParser.json({ limit: '900kb' }); let u ...

Syncing Google Map marker movement with MySQL database updates

I have implemented a Google map that displays positions with movable markers, with latitude and longitude information coming from a database. Now I need to update the database with new latitude and longitude values whenever a marker is moved. Can this be a ...