ContentfulBundle icon indicating copy to clipboard operation
ContentfulBundle copied to clipboard

Strategy for preview implementation

Open ghisleouf opened this issue 2 years ago • 0 comments

Hello everybody,

I'm back with another question which is much more about getting your advices and best practices for preview setup.

Let'say that I setup a website called www.mysite.com with Contentful and I want to add the preview feature.

What I have in mind is that I want to setup a simple preview passing a query string parameter like ?preview=1 to switch to preview mode. For example, on the homepage https://www.mysite.com/ will switch to preview mode when I add https://www.mysite.com/?preview=1

First question, is it a good idea ?

Now, going deeper in it, I created a service called "Contentful" where I inject the client thanks to dependency injection:

<?php

namespace App\Service;

use Contentful\Delivery\Client\ClientInterface;
use Contentful\Delivery\Query;
use Contentful\Delivery\Resource\Entry;

class Contentful
{


    private ClientInterface $client;

    public function __construct(ClientInterface $client)
    {
        $this->client = $client;
    }
...

Now, I'm a little bit stuck about the way to switch to preview API with my client injected like that. What I have in mind is :

  • Intercept all the request to my website
  • Check if the query string preview=1 exists
  • Create a factory to inject the client saying to use preview API instead of main API when the query string is there

Is it a good strategy or am I overcomplicating things ? What is yours ? ( using a subdomain ? )

Thanks for your feedback.

Best.

ghisleouf avatar May 05 '22 12:05 ghisleouf