Draft Preview giving error page

Hi there Kirby folks,

I’m not 100% sure if this is new behavior in one of the recent updates, but I am currently running Kirby 3.1.

The problem is that all of my draft pages are directing me to the error page. I can see that it is loading the site and subpage correctly and then adding a ?token=string of numbers

I’m pretty sure that draft preview was working in 3.0.0 and I think 3.0.1 but the most recent update prior to 3.1 I think it wasn’t working.

Please let me know what further info I could possibly provide. Thanks!

Is this happening while you are logged in to the Panel? Works fine for me when I click on the Open link of a newly created draft?

Yes, logged into the panel, and I’m getting the same result when I click the open link of a new draft or a draft that existed previously.

I’m using a route to remove some parent folders from the URL. I wonder if that could be interfering? There does seem to be an error page option here that might be?

My current routes in my config are:


'routes' => [
		[
			'pattern' => '(:any)',
			'action'  => function($uid) {

				$page = page($uid);

				if(!$page) $page = page('about/' . $uid);
				if(!$page) $page = page('releases/' . $uid);
				if(!$page) $page = site()->errorPage();

				return site()->visit($page);

			}
		],
		[
			'pattern' => ['about/(:any)', 'releases/(:any)'],
			'action'  => function($uid) {
				go($uid);
			}
		],
		[
			'pattern' => '(:any)/(:any)',
			'action'  => function($parent, $uid) {

				$page = page($parent.'/'.$uid);

                if(!$page) $page = page('releases/' .$parent .'/'. $uid);
				if(!$page) $page = site()->errorPage();

				return $page;

			}
		],
		[
			'pattern' => 'releases/(:all)',
			'action'  => function($uid) {
				go($uid);
			}
		]

Does it work if you remove your routes?

Aha. It does!

Trying removing just those error lines now. I’m wondering if those may just be unnecessary anyway?

So unfortunately just removing the error lines from these routes doesn’t fix it, but removing the routes completely does.

Hm, I haven’t tested this yet, but maybe if you set the preview option in your blueprints according to your routing scheme, it might work: https://getkirby.com/docs/reference/panel/blueprints/page#preview

The drafts are not specifically related to the pages altered by the routes, but I’ll take a look at that and see if I can get it going.

Thanks!

Ok, I applied your routes in my test starter kit and the same happens. I’ll try to narrow it down.

Edit: Hm, I currently have no idea how to solve this or if it is a bug.

Thanks Sonja,

It’s a real mystery. I’m 90% sure it was working in 3.0 and, I think 3.0.1. I was working on launching my site with those and I think I would have noticed if drafts weren’t working, although the routes came near the end of my recoding process. Maybe something changed in how previews were being handled?

I’ll try a few things on my end this morning and see if I can discover anything.

One thing you can definitely do is use the new $this->next() method to ignore pages that shouldn’t be routed. That will at least make draft links for these pages work, not however the remaining routed pages’ drafts.

    'routes' => [
		[
			'pattern' => '(:any)',
			'action'  => function($uid) {
    
				if ($page = page('about/' . $uid) || $page =  page('releases/' . $uid)) {
                    return site()->visit($page);
                }
                $this->next();		
            } 
        
		],
		[
			'pattern' => ['about/(:any)', 'releases/(:any)'],
			'action'  => function($uid) {
				return go($uid);
			}
        ],
        
		[
			'pattern' => '(:any)/(:any)',
			'action'  => function($parent, $uid) {	

                if( $page = page('releases/' .$parent .'/'. $uid)) {
                    return $page;
                }
				$this->next();
			}
		],
		[
			'pattern' => 'releases/(:all)',
			'action'  => function($uid) {
				go($uid);
			}
        ]
    ]

That’s a huge help, thank you! I rarely make drafts in the /releases/ section and never in the /about/ so that should clear things up for the bulk of my functionality at least.

Thank you!

I’ll nevertheless create an issue to have the devs look into whether this is expected behavior or how to prevent the issue.

Thank you as always for your tremendous effort Sonja!

1 Like

When I replace my current routes with this, I am now getting an invalid page object error in the Kirby Site.php

 // handle invalid pages
        if (is_a($page, 'Kirby\Cms\Page') === false) {
            throw new InvalidArgumentException('Invalid page object');
        }

Where exactly is that error thrown and when calling which page? Could you provide type of error, line that is causing it or a screenshot of the Whoops page.I didn’t test those routes properly, because I didn’t have an corresponding page structure, so there might be something wrong.

Will do. I had to run out but will take a screenshot this afternoon. Thanks again!

Okay.

The error is happening whenever I visit any of the first level pages under /about or /releases however, I can navigate directly to the second level pages under /releases referenced in the $parent of the third pattern here. Other pages are unaffected.

Here is a screenshot of the error message:

I can confirm that previews are working on the other sections.

We need parenthesis around the conditions:

if (($page = page('about/' . $uid)) || ($page =  page('releases/' . $uid))) {