Skip to content
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

Open
sebastiancretu opened this issue Feb 20, 2021 · 6 comments
Open

Not able to retrieve single types #14

sebastiancretu opened this issue Feb 20, 2021 · 6 comments

Comments

@sebastiancretu
Copy link

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 ?

@wlabarron
Copy link

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

@rjlg
Copy link

rjlg commented Aug 6, 2021

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?

@wlabarron
Copy link

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.

@rjlg
Copy link

rjlg commented Aug 13, 2021 via email

@paulkitt
Copy link

@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?

@bluszcz
Copy link
Member

bluszcz commented Sep 20, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants