jekyll-strapi icon indicating copy to clipboard operation
jekyll-strapi copied to clipboard

Not able to retrieve single types

Open sebastiancretu opened this issue 4 years ago • 6 comments

I've set the config file with the single type collection like this:

strapi:
    # Your API endpoint (optional, default to http://localhost:1337)
    endpoint: http://localhost:1337
    collections:
         aboutme:
                type: about-me

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 ?

sebastiancretu avatar Feb 20 '21 11:02 sebastiancretu

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 strapi.collections.mySingle[0].

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 ]
		}
            });
        },
    };
};

wlabarron avatar Apr 11 '21 10:04 wlabarron

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 strapi.collections.mySingle[0].

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?

rjlg avatar Aug 06 '21 08:08 rjlg

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.

wlabarron avatar Aug 06 '21 08:08 wlabarron

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 https://github.com/strapi/jekyll-strapi/issues/14#issuecomment-894094778, 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.

rjlg avatar Aug 13 '21 22:08 rjlg

@wlabarron Thanks for the great workaround It seems with strapi4 the config syntax has changed. Anybody a idea how to use this workaround with strapi4?

paulkitt avatar Sep 20 '22 10:09 paulkitt

Looks for me like a feature request which should not be difficult to deliver. We need first tackle the permalink issue ;(

bluszcz avatar Sep 20 '22 15:09 bluszcz