-
Notifications
You must be signed in to change notification settings - Fork 29
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
Not able to retrieve single types #14
Comments
I've noticed this too. From what I can see, jekyll-strapi just doesn't support single types. Strapi returns an array of objects for a collection, and just an object for a single type. Since this plugin only supports collections, if you enter the details of a single type in your config, then the plugin won't be able to understand the response since it's of an unexpected type. As a workaround, I've added some middleware to my Strapi installation to wrap the single type response up in an array and return that. I can then access the single in my Strapi themes with This is the middleware code I'm using: const singleName = "";
module.exports = strapi => {
return {
initialize() {
strapi.app.use(async(ctx, next) => {
await next();
if (ctx.url.startsWith("/" + singleName)) {
ctx.response.body = [ ctx.response.body ]
}
});
},
};
}; |
Hi @wlabarron, i'm quite new to Strapi and Jekyll and looking to implement something similar to be able to query single types. Are you able to provide additional context of how you have implemented your middleware in your project? |
Sure! The middleware basically cheats by returning queries for the single type like it's actually a collection containing only one item. The middleware checks the path of each request to Strapi. If the path matches the one we specify in the So in my Strapi project, I've got a file at Finally, the middleware is enabled in module.exports = {
settings: {
single_to_array: {
enabled: true
}
}
}; I'd like to PR this plugin with a more native solution but I've not had time. |
Hi Andrew,
Thanks so much for running me through this. Much appreciated.
Hope you have a great weekend.
Cheers,
Rory
… On 6 Aug 2021, at 6:25 pm, Andrew Barron ***@***.***> wrote:
Sure! The middleware basically cheats by returning queries for the single type like it's actually a collection containing only one item.
The middleware checks the path of each request to Strapi. If the path matches the one we specify in the singleType variable, it puts the response into an array and continues. Otherwise, it just continues.
So in my Strapi project, I've got a file at middlewares/single_to_array/index.js containing the above code snippet. Set the singleName variable to the name of your single type. If you've got more than one single type you need to query, you'll need to modify the if statement directly.
Finally, the middleware is enabled in config/middleware.js like this:
module.exports = {
settings: {
single_to_array: {
enabled: true
}
}
};
I'd like to PR this plugin with a more native solution but I've not had time.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub <#14 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/ABP35WWMKIXG2DUJZK3IV5TT3OMALANCNFSM4X5ZWCNQ>.
Triage notifications on the go with GitHub Mobile for iOS <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android <https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email>.
|
@wlabarron Thanks for the great workaround |
Looks for me like a feature request which should not be difficult to deliver. We need first tackle the permalink issue ;( |
I've set the config file with the single type collection like this:
Unfortunatelly, if I do
{{ strapi.collections.aboutme | inspect }}
in the template, the result that I get is an empty array even though I can see in the terminal that the request was made:Jekyll Strapi: Fetching entries from http://localhost:1337/about-me?_limit=10000
I would expect for this to be working too and retrieve all the fields/values on the single types also. Is this possible ?
The text was updated successfully, but these errors were encountered: