-
-
Notifications
You must be signed in to change notification settings - Fork 177
[V5] Preview-URL for headless setup #7240
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
Comments
@lukasbestle I think you should have a closer look at this. |
Good point. We should definitely find a better solution. I need your help, so I'll explain where this came from and why it is needed: This change was introduced in #6836 because with the new Changes feature, it is suddenly possible to preview changed content of already published pages (not just of drafts as in v4). This means that supporting previews of custom preview targets (controlled by the For this to work with the preview tokens, we could no longer generate the tokens based on model data of the current page, but need to use data of the target URL. We came to the conclusion that the most robust way is to just use the target path. This is what the As the TL;DR: IMO we first need to think about that assumption on external servers again. In your (Georg's) use case, you control both the headless backend and frontend, so both the Panel server and the server handling the preview. So you could easily set a shared custom An issue is that in this case the What we could probably do is to just always use the full URL and not the path. This could slightly reduce reliability if the hostname is detected differently on the preview request than when generating the preview URL. But in most setups it should work fine. |
Not sure if I fully understand the problem, but this is how I do it or did handle previews in my headless Kirby setup: I use Kirby’s regular templates to return JSON from them. I've added a Then I have a Since my The frontend is called with the preview token, which it then adds to its fetch request. Just like with regular templates, the preview data instead of the regular data was returned. With version 5, no token is added to my preview URL and therefore previews stopped working. Let me know if I can help somehow … in the meantime I try to understand the problem better. 😀 |
Thank you all for the feedback! @lukasbestle , I think the solution you propose would be fine from my end. I can easily add a I'm not sure if I need to involve the frontend here tough. Maybe I'm missing something. As Kirby is creating the preview URL, e.g. Query::$entries['preview'] = function (string $pageUid, string $token, string $version): Page|null {
if (!$token || !$version) {
throw new Exception('Not all required parameters are set');
}
$page = page($pageUid);
if (!$page) {
throw new Exception('Page not found');
}
$version = $page->version($version);
if (!$version->exists()) {
$version = $page->version('latest');
}
$expectedToken = $version->previewToken();
if (hash_equals($expectedToken, $token) !== true) {
throw new Exception('Invalid token');
}
// I'm not sure if this is the most elegant way to do that. I couldn't find a method that returns a page object from the version.
$versionPage = Page::factory([
'slug' => $page->slug(),
'content' => $version->content()->toArray(),
'template' => $page->intendedTemplate()->name(),
'model' => $page->intendedTemplate()->name(),
]);
return $versionPage;
}; The frontend is doing a KQL query like But as I said, maybe I'm missing something here. And for sure: If needed I can set a @silllli : The missing preview token was the reason for me to start a refactoring. Overwriting the Just as a side note: I really like what you guys did with versions and changes! Thie UI is very nice and the architecture and APIs seem to be very good to work with. I was able to remove a lot of custom code I needed to have for headless preview and our live-preview storybuilder. Kudos to that! |
@georgobermayrsaf Yep, that’s also the part that confuses me. Until now the backend got its authentication in form of the token and responded with the otherwise inaccessible content (HTML, or in my case JSON), although the token now would have to include the desired |
Thank you both for your input. I think we'll try the solution I drafted in my last post and see if that again has any disadvantages in the real world. I think it should be pretty robust and is probably the only way to support external preview URLs without complex logic. A few off-topic replies and ideas for your setups: @silllli Since the new token implementation hooks into
|
Description
I try to rewire the new preview and changes views for a headless setup.
For that my idea was to overwrite the URL function in the page model:
With that I get an
Cannot produce local preview token for model
error in the Panel. This comes from thepreviewTokenFromUrl
function inKirby\Content\Version
.The issue seems to be this comparison in the function:
Obviously the base URL from Kirby is no longer equal with the base URL from the model. So this fails and throws the exception. If I remove the if-clause, I can see the preview in the Panel derived from my separate frontend. The preview Url als has the token attached. With that I would continue to refactor the frontend to get the Preview version from a Kirby API.
Can this comparison be removed? Or is there another/better way to create a different preview URL for headless setups?
Your setup
Kirby Version
Kirby 5 RC-1
The text was updated successfully, but these errors were encountered: