Skip to content

Add basic strapi5 compatibility option #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ A: Every project deserves to have the cute deer as a logo.

* Support for Strapi 4
* Authentication
* (Basic) Strapi5 Compatability
* Permalinks
* Caching and collecting assets from Strapi
* Added UnitTests
Expand All @@ -38,6 +39,8 @@ plugins:
strapi:
# Your API endpoint (optional, default to http://localhost:1337)
endpoint: http://localhost:1337
# Strapi5 compatability (optional, default false)
v5compat: true
# Collections, key is used to access in the strapi.collections
# template variable
collections:
Expand Down Expand Up @@ -78,6 +81,12 @@ To access non Public collections (and by default all Strapi collections are non

It is recommended that you will use new Content API tokens for this task: https://strapi.io/blog/a-beginners-guide-to-authentication-and-authorization-in-strapi

### (Basic) Strapi5 Compatability

Strapi5 introduced breaking changes to the response format as well as the way published documents are accessed. (https://docs.strapi.io/dev-docs/migration/v4-to-v5/breaking-changes/)

In order to use this plugin with strapi5, make sure to set `v5compat: true` in your `_config.yml`.

## Usage

This plugin provides the `strapi` template variable. This template provides access to the collections defined in the configuration.
Expand Down
11 changes: 8 additions & 3 deletions lib/jekyll/strapi4/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ def get_data
# https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest/sort-pagination.html#pagination-by-page
uri = URI("#{@site.endpoint}/api/#{endpoint}#{path_params}")
Jekyll.logger.debug "StrapiCollection get_document:" "#{collection_name} #{uri}"
response = strapi_request(uri)
response = strapi_request(uri, @site.v5compat)
response.data
end

def get_document(did)
uri_document = URI("#{@site.endpoint}/api/#{endpoint}/#{did}?populate=#{populate}")
Jekyll.logger.debug "StrapiCollection iterating uri_document:" "#{uri_document}"
strapi_request(uri_document)
strapi_request(uri_document, @site.v5compat)
# document
end

Expand All @@ -51,7 +51,12 @@ def each
if single_request?
document.strapi_attributes = document.attributes
else
document_response = get_document(document.id)
# use documentId if we are running against strapi5 https://docs.strapi.io/dev-docs/migration/v4-to-v5/breaking-changes/use-document-id
if @site.v5compat
document_response = get_document(document.documentId)
else
document_response = get_document(document.id)
end
# We will keep all the attributes in strapi_attributes
document.strapi_attributes = document_response['data']["attributes"]
end
Expand Down
4 changes: 4 additions & 0 deletions lib/jekyll/strapi4/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def has_strapi_collections?
def endpoint
has_strapi? and @config['strapi']['endpoint'] or "http://localhost:1337"
end

def v5compat
has_strapi? and @config['strapi']['v5compat'] or false
end

def strapi_link_resolver(collection = nil, document = nil)
return "/" unless collection != nil and @config['strapi']['collections'][collection]['permalink'] != nil
Expand Down
6 changes: 5 additions & 1 deletion lib/jekyll/strapi4/strapihttp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This is a helper method to authenticate during getting data from Strapi instance.
require "json"

def strapi_request(url)
def strapi_request(url, v5compat = false)
uri = URI(url)
req = Net::HTTP::Get.new(uri)
strapi_token = ENV['STRAPI_TOKEN']
Expand All @@ -15,6 +15,10 @@ def strapi_request(url)
}
req['Authorization'] = "Bearer #{strapi_token}"
end
# Add header to adjust response structure https://docs.strapi.io/dev-docs/migration/v4-to-v5/breaking-changes/new-response-format
if v5compat
headers['Strapi-Response-Format'] = "v4"
end
Jekyll.logger.info "Jekyll StrapiHTTP:", "Fetching entries from #{uri} using headers: #{headers.keys}"
response = Net::HTTP.get_response(uri, headers)

Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll/strapi4/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Jekyll
module Strapi
VERSION = "1.0.12"
VERSION = "1.0.13"
end
end