Skip to main content
 首页 » 编程设计

jekyll之站点根目录 : Github Pages vs. `jekyll --server`

2025年01月19日9飞鱼

当我跑 jekyll --server我的网站位于 http://localhost:4000/ ,但是当我部署到 GitHub 项目页面时,它可以在 http://username.github.com/projectname/ 上找到。 .

这意味着我不能在引用样式表和其他资源时使用绝对 URL。例如,当使用相同的布局时,相对 URL 会中断。 index.html2012/01/01/happy-new-year.html .将样式表和其他资源添加到 GitHub 项目页面存储库的公认/好方法是什么?

交叉发布到 GitHub Issues .

请您参考如下方法:

出现这个问题是因为根目录始终是用户 url (user.github.com),在用户和项目页面中。我找到了解决方案:配置 url _config.yml 中的变量文件到github项目页面:

safe: true 
... 
url: "http://user.github.io/project-name" 

然后,在布局中,使用绝对引用,使用 site.url变量来做到这一点:
<link rel="stylesheet" href="{{ site.url }}/css/styles.css"> 

并使用以下命令运行服务器:
$jekyll --server --url=http://localhost:4000 

命令行选项在本地模式下覆盖配置文件中的设置。通过这些设置,我获得了指向正确位置的所有链接,它们都托管在 github 和本地模式中。

更新: Jekyll 1.0 由于 Jekyll 达到 1.0,上述选项 serverurl及其各自的命令行选项 --server--url已被弃用。新版本使用说明:

_config.yml文件,添加变量 baseurl (不带斜杠):
baseurl: "http://user.github.io/project-name" 

在布局或页面中,使用绝对引用,使用 site.baseurl多变的:
<link rel="stylesheet" href="{{ site.baseurl }}/css/styles.css"> 

然后,运行本地服务器(baseurl 选项故意为空):
$ jekyll serve --watch --baseurl= 

有关 docs 更改的更多详细信息.