squidfunk / mkdocs-material

Documentation that simply works
https://squidfunk.github.io/mkdocs-material/
MIT License
20.3k stars 3.48k forks source link

Problem loading assets in mkdocs deployed on github pages #4678

Closed danimrprofe closed 1 year ago

danimrprofe commented 1 year ago

Contribution guidelines

I've found a bug and checked that ...

Description

Hello, don't know if this is the right place to ask for help, but i'm having a little problem and can't find the solution to it. By the way, love mkdocs and the material theme and I would appreciate if you could help me.

Mkdocs works perfect when served locally (localhost), but displays without styles or images when deployed to github.

Thank you in advance, if more information is needed please let me now.

Expected behaviour

I installed mkdocs, initiated a new project in a repo and configurated the yaml as the guide said. mkdocs serves works perfect as in local server everything displays as expected. Then I deploy with the command with the option gh-deploy on mkdocs but on github doesn't load assets images or styles, only the HTML.

Actual behaviour

This is where the build shoud deploy the website. I did it from command line via mkdocs command, and it pushed to the new branch of the repository (gh-deploy)

https://danimrprofe.github.io/apuntes/

As you can see no styles ar shown:

image

This is how the website is displayed (without styles). Seems to be looking at the media in the root of the URL, where they are not. I mean, the HTML looks for:

https://danimrprofe.github.io/assets/images/favicon.png

Instead of where it really is:

https://danimrprofe.github.io/apuntes/assets/images/favicon.png

My pages options in the repo.

image

Steps to reproduce

  1. Build with mkdocs
  2. Deploy with gh-deploy
  3. Check in the github.io page.

Package versions

Configuration

site_name: My Docs

theme:
  name: material
  language: es
  features:
    - navigation.tabs
    - navigation.tabs.sticky

System information

squidfunk commented 1 year ago

Thanks for reporting. When deploying, you need to specify site_url – it must point to your GitHub URL:

https://github.com/squidfunk/mkdocs-material/blob/80b4a4c8aca25cfec059806a6877658ff60a4154/mkdocs.yml#L23

The reason is that MkDocs will resolve asset URLs relative to the configured site URL. If no site URL is configured, / is assumed, so your project is expected to live at the root of the current URL. Since your documentation is hosted in a subdirectory, this setting is mandatory. I'll add a note to the GitHub Pages deployment guide.

danimrprofe commented 1 year ago

Thank you very much for your suport, it worked perfectly!!!!