(accidentally deleted the message originally sharing this… apparently there’s no “undo delete” feature in Discord.)
Alright, this isn’t a solution to the problem discussed so far, but it’s another approach that’s working well enough for now (and uses simpler code).
I learned that through the page
methods created the same JSON output as the published version, which of course makes sense since this is what Kirby uses internally. Here’s the solution I’m using for now:
// Creates a preview version of the file with the in-memory data (i.e. the data that's yet to be saved) for use
// in our Lambda function that builds a Netlify preview.
// Create the name, using our naming structure of `internal-preview-[slug]`
$tempName = 'internal-preview-' . $this->requestBody('slug');
// Duplicate the published file (this creates a Kirby draft by default)
$tempCopy = $kirby->page($tempName) ? $this->page($tempName) : $this->page($this->requestBody('slug'))->duplicate($tempName);
// Update the duplicated file with our in-memory data
$tempCopy->update($this->requestBody('data'), $this->language(), false); return new Response(null, null, 204);
The one downside/note about this approach is that I couldn’t come up with a workable way to name the file the same as the published version. After thinking this through, however, I realized that it actually seems desirable to use a different name anyways (to explicitly indicate that it’s for preview purposes, rather than an actual draft file that would be indistinguishable from Kirby’s draft files).
However, since settling on this approach this code is now returning a 403 (permission denied) error. No idea what, if anything, changed.
Update: Figured out the reason for the permission issue (Permission problems trying to duplicate a page via custom API endpoint - #4 by isaac)