Issue with route and draft page preview

Hello,

in order to do some filtering I use a custom route in my config file as suggested in this cookbook example.

'routes' => [
	[
		'pattern' => 'work/(:any)',
		'action' => function ($filter) {
			if ($page = page('work/' . $filter)) {
				return $page;
			} else {
				return page('work')->render([
					'filter' => $filter
				]);
			}
		}
	]
],

Although this works as intended it causes problems with preview of draft pages. I found that this has already be documented here.

I tried to implement the solution described in my code but didn’t have any success.

Any thoughts? Thanks in advance.

'routes' => [
    [
        'pattern' => 'work/(:any)',
        'action' => function ($filter) {
            if (page('work')->childrenAndDrafts()->has('work/' . $filter)) {
                if ($page = page('work/' . $filter)) {
                    return $page;
                } else {
                    $this->next();
                }
            } else {
                return page('work')->render([
                    'filter' => $filter
                ]);
            }
        }
    ]
]