36

I have created a website and am trying to host it on git hub pages. My site is available at -

http://<username>.github.io/<project name>/

But the static files for my site are available at the following path -

http://<username>.github.io/css/site.css
http://<username>.github.io/script/main.js

The above path omits the <project name>

So whenever I hit the url my static files are not loaded.

Is there a way to make it work with the github url?

Note: When I use a custom domain everything works fine because the relative paths are fine in that case.

Temporary Solution I have created a User page instead of a Project page to overcome this issue.

mokagio
  • 16,391
  • 3
  • 51
  • 58
tusharmath
  • 10,622
  • 12
  • 56
  • 83
  • does a website exist at `username.github.io` too? what happens when you go to `username.github.io/css/site.css` in the browser? you might have to try `raw.username.github.io/css/site.css` –  Aug 05 '13 at 07:50
  • No there is no website at username.github.io, css file ate not available at that location. What is 'raw'? – tusharmath Aug 05 '13 at 09:49
  • you need to change the static urls for your css and scripts to point to the right location then. just try `/css/site.css` –  Aug 05 '13 at 12:20
  • Yes but then it would not work with actual domain. – tusharmath Aug 05 '13 at 14:07
  • you cannot have it both ways, either create the username.github.io site and put the style there or put it in the project so style would be at username.github.io/projectname/css/site.css –  Aug 06 '13 at 12:11
  • `raw.github.com` is the subdomain used by GitHub to show you the "raw" version of your files in the repo, meaning just the pure file, outside of the website structure. Open a file in your repo and click "Raw" in the toolbar on the top right corner to check it out. – mokagio Aug 28 '13 at 08:07

3 Answers3

36

The other option is to include the file .nojekyll in your top-level directory which tells github to just serve your pages with out trying to break them (this is useful if you are trying to post the oputput from sphinx which does not need any more parsing/munging/rewriting).

relevant link

tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • 1
    I was tearing my hair out over this! A simple 2 file site (`index.html` and `_bundle.js`) needed the `.nojekyll` file to work. – xavdid Feb 27 '17 at 10:28
15

From the documentation to setup a custom domain:

Warning: Project pages subpaths like http://username.github.io/projectname will not be redirected to a project's custom domain.

This means that due to the relative paths you can either have the assets on your username.github.io/project-name or on your custom domain.

If you want them in the github one check what the documentation says about the baseurl configuration, it's at the bottom of the page, in the "Project Url Structure". It's simple you just need to add a

baseurl: /project-name

row to your _config.yml and use the {{ baseurl }} tag in your permalinks, jekyll will do the substitutions. And to test it locally just run the server with this option

jekyll serve --baseurl ''

Hope it helps :) Happy coding!

mokagio
  • 16,391
  • 3
  • 51
  • 58
  • 1
    I think the permalink tag should be `{{ site.baseurl }}` instead of `{{ baseurl }}` – mikeybeck Apr 21 '15 at 03:34
  • FWIW, I had the same problem while using webpack to build my site. In `webpack.config.js` set [`output.publicPath`](https://github.com/webpack/docs/wiki/configuration#outputpublicpath)`: /project-name`. Even so, the basic principal of setting the published url path in the build config is the correct solution. – gfullam May 05 '16 at 18:46
1

If your project is a React app created with Create React App, set the homepage in your package.json, for example:

"homepage": "/<project name>",

If you're not using client-side routing, use this:

"homepage": ".",

See https://create-react-app.dev/docs/deployment/#building-for-relative-paths.

Ron Inbar
  • 2,044
  • 1
  • 16
  • 26