Why is gh-pages not recognizing the project directory while looking for resources?

I've encountered an issue with my website that functions perfectly fine locally but when deployed on gh-pages, it results in several 404 errors while attempting to retrieve resources. One such example is a line of CSS for setting the background:

background: #fff url(/assets/images/bg.jpg) repeat top left;

The specific 404 error message I receive is this:

github.io/assets/images/bg.jpg

Instead, the correct path should be:

github.io/myProject/assets/images/bg.jpg

Any suggestions on how to resolve this issue and make it work correctly?

Answer №1

It seems that the issue lies in your local setup where the assets folder is located in the same directory as the file.

For example, if your local path appears like this: file:///index.html and your assets folder is a sibling to index.html, it works because the / leads to the root of the directory, which is file:///, allowing it to find the assets folder locally.

However, on GitHub, your repository might be named my-project. Within my-project, you have an index.html file and an assets folder. When you use /assets/, the beginning directory is now github.io, causing it to look for the assets folder there, which does not exist.

Solution: Avoid adding / before assets. As long as the assets are in the same directory as the code, it should work correctly.

Additionally, consider reviewing relative paths to gain a better understanding:

Answer №2

Although gh-pages are typically created by Jekyll, you can harness its power to simplify the development process.

To do this, simply create a _config.yml file at the root with the following content:

baseurl: myPortfolio/

Now, whenever you need to reference a resource, just use

{{site.baseurl}}assets/images/bg.jpg
, which will automatically convert to myPortfolio/assets/images/bg.jpg.

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

Build a brand new root component in Vue JS

I am facing a challenge with destroying and re-creating the root application component. Below is my template structure: <div id="app"> {{ num }} </div> Here is the code I have implemented: if (app) { app.$destroy(); } else { ...

Best practices for referencing attributes created with the data method in a Vue component

In my application, there is a section that displays a list of users along with a search box to filter names. The user information is fetched from a database and stored in app.users. I have created a component named 'user' to show the details of e ...

What is the CSS/HTML code to position text at the bottom of an image overlay in a card design?

I've been attempting to modify the justify-content in the CSS class to "flex-end" and "end," however the text is not relocating to the bottom. My suggestion is that we concentrate on the img-overlay class, as that's where we adjust the position ...

What is the proper way to integrate three.js (a third-party library) into the view controller of an SAPUI5 application

Seeking a Solution Is there a way to integrate the three.js library into SAPUI5 in order to access it using THREE as the root variable in my main view controller? I attempted to create a directory named libs within my project folder and include it in the ...

Cannot load JavaScript in Ajax tabs

I have implemented this ajax tabs on my website. However, the JavaScript code inside the content.htm file seems to be malfunctioning. Below is the code snippet I am using: Javascript $(document).ready(function(){ $("button").click(function(){ ...

What is the best approach to execute the jquery script exclusively on mobile devices?

I want to modify this code so that it only functions on mobile devices and is disabled on computers. How can I achieve this? <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body> <ul id="pri ...

When viewing Google maps in full screen mode, the image suddenly vanishes

I have a map with a logo positioned in the lower right corner near the controls. However, when I switch to full screen mode, the logo disappears. I can see the div (aggdata-info) in Firebug, but the image/logo is not visible. Any suggestions on how to fix ...

PHP query will execute even in the absence of clicking the button

I'm encountering an unusual issue. I've defined a query to insert two names into the database, and I've used Javascript(Jquery) to ensure it only runs when the create button is clicked. However, the script seems to be executing every time I ...

Update the default arrow icon in the expand/collapse navigation bar

I need help changing the downward arrow in the code below to a fontawesome icon. I'm unsure about how to: 1. Remove the default arrow on the right of the parent node. 2. Use "+/-" symbols to represent the collapse state. It seems that the onclick c ...

JavaScript Error: Unable to execute getJsonData due to function not found

I am encountering an issue with a function that retrieves JSON data from a URL Here is the code snippet: var retrieveJsonData = function(uri,callback){ $.ajax({ type: "GET", dataType: "jsonp", url: uri, jsonpCallback: 'r ...

Converting a string to an array in React when updating the state

Currently, I am working on a React CRUD app that allows me to manage dishes. I can add new dishes, display a list of existing ones, update the details of a dish, or delete a dish. My main challenge at the moment is updating the ingredients of a dish. Whil ...

Generating nested arrays using Vue.js

Calculating the total costs of products selected by a user for each company is my current task. Below is the code snippet I am using: let companiesProductPrices = []; let prices = []; this.selectedCompaniesDetails.forEach ...

The jQuery animation concludes before its anticipated completion

I'm currently facing a small issue with a jQuery animation. The HTML code I have is as follows: <div id="menu"> <a id="menu-about" href="/">About...</a><br /> <a id="menu-ask" href="/">Ask me a question</a> ...

Designing a sleek border for form input fields

Seeking assistance in finding a solution as my extensive online search has been unsuccessful. Currently, I have this code to style my form labels: label { display:inline-block; height: 15px; width: 350px; float: left; padding: 5px; ...

Jest unit tests are failing due to an error stating that VUE_APP_CONFIG is not defined

export interface VueAppSettings { BASE_URL: string; BASE_URL_V2: string; } declare const VUE_APP_SETTINGS: VueAppSettings; export const APP_SETTINGS = { ...VUE_APP_SETTINGS } as const; I am encountering a reference error in the code snippet abov ...

I'm curious, in which environment does SvelteKit, Next.js, and Nuxt.js code execute? Also, is it possible to create HTTP request handlers within

My experience with using SvelteKit in my personal projects has been quite enjoyable. However, coming from a background of working with frameworks like Next.js and Nuxt.js, I often find myself confused about where the code I write actually runs. In my day ...

Transforming instance of a Class into Json format for WebService

My goal is to retrieve an object of the Product class as a Json for a webservice. Despite successfully loading all the values into the object, I encounter an error when trying to return it as Json. Below is my method: <AllowAnonymous> <HttpGet&g ...

What is the art of spinning a div?

Looking to add an interactive circular div on my website that can be spun like a wheel of fortune using the mouse. I tried using jQuery drag without success in rotating the div. Is there a method to rotate div elements with the mouse? It would also be awe ...

Tips for utilizing background-position and background-size in CSS while also positioning an image at a specific location

My current layout is achieved using background-position: center and background-size: cover. Take a look at the image here: https://i.sstatic.net/552RA.png, originally sourced from this link. I am aiming to modify the appearance of the layout to resemble t ...

Apply the jQuery method to select elements with index 3, 4, 5, 6, or 7, then add

My current Wordpress blog displays 20 posts. I am looking to apply a specific class to posts numbered 2 through 10, and then again from 12 to 20. I do not want the first post or post number 11 to have this class added. I attempted using CSS selectors lik ...