Jekyll 和 Strapi:Liquid 异常:请确保 Strapi 服务器正确 运行

Jekyll and Strapi: Liquid Exception: Please make sure Strapi Server is correctly running

我目前正在尝试学习本教程 (https://blog.strapi.io/building-a-static-website-using-jekyll-and-strapi/),在 "Posts List" 步骤之前一切正常。添加 _layouts/home.html 文件并重新启动 jekyll 服务器(bundle exec jekyll serve)后,我得到了一条错误消息:

Liquid Exception: The Strapi server sent a error with the following status: 404. Please make sure it is correctly running. in /_layouts/home.html jekyll 3.8.5 | Error: The Strapi server sent a error with the following status: 404. Please make sure it is correctly running.

事实是,strapi 服务器是 运行...我可以访问 strapi 管理后端,我可以访问和查看帖子 json 对象,方法是:http://localhost:1337/posts.

我实际上不太确定发生了什么以及如何解决这个问题。文档没有帮助,我似乎也无法在 google 或堆栈溢出中找到有关此问题的任何内容。

还有其他人遇到过这个问题吗?

这里是home.html的内容:

---
layout: default
---

<div class="home">
    <h1 class="page-heading">Posts</h1>
    {%- if strapi.collections.posts.size > 0 -%}
    <ul class="post-list">
        {%- for post in strapi.collections.posts -%}
        <li>
            <span class="post-meta">{{ post.createdAt | date_to_string }} by {{ post.author.username }}</span>
            <h3>
                <a class="post-link" href="{{ post.url | relative_url }}">
                    {{ post.title }}
                </a>
            </h3>
            <!-- Display an excerpt of the article -->
            <p>{{ post.content | markdownify | strip_html | truncatewords: 10 }}</p>
        </li>
        {%- endfor -%}
    </ul>
    {%- endif -%}
</div>

编辑:添加端点配置

strapi:  
# Your API endpoint (optional, default to http://localhost:1337)
  endpoint: http://localhost:1337
  # Collections, key is used to access in the strapi.collections
  # template variable
  collections:
    # Example for a "posts" collection
    posts:
    # Collection name (optional). Used to construct the url requested. Example: type `foo` would generate the following url `http://localhost:1337/foo`.
      type: post
      # Permalink used to generate the output files (eg. /posts/:id).
      permalink: /posts/:slug/

终于让它工作了...我最终将 jekyll-strapi 更新到最新版本 0.1.2,并将 jekyll 更新到版本 3.8.5。

我已经安装了 gem jekyll-strapi,所以要更新它,我在终端中做了:

gem install jekyll-strapi

这可确保您拥有修复了错误的最新版本。

接下来,您的 gem 文件应该这样配置:

gem "jekyll", "~> 3.8.5"
[...]
group :jekyll_plugins do
  gem "jekyll-feed", "~> 0.12"
  gem 'jekyll-strapi', github: 'strapi/jekyll-strapi'
end

“github: 'strapi/jekyll-strapi'”参数将确保您拥有臭名昭著的错误的最新修复:“液体异常:无法将整数转换为字符串”

还有一件事,在 blog/_config.yml 中,而不是“键入:post”

strapi:
  collections:
    posts:
      type: post

你需要'post'复数。所以,你最终会得到:

strapi:
  collections:
    posts:
      type: posts

如果你不这样做,你最终会得到这个错误:“液体异常:Strapi 服务器发送了一个错误,状态如下:404。请确保它是正确的 运行。在 /_layouts /home.html"

我希望它能帮助其他正在努力学习本教程的人。随着 gems 和框架的快速移动和更新,一些在线教程变得过时并且与最新版本不同步。