diff --git a/src/Content/Version.php b/src/Content/Version.php index 6e6e32878d..221d426704 100644 --- a/src/Content/Version.php +++ b/src/Content/Version.php @@ -427,9 +427,13 @@ protected function previewTokenFromUrl(string $url): string|null { $localPrefix = $this->model->kirby()->url('base') . '/'; - // Todo: this is only a quick fix to get home page previews working again, - // we need to double-check if this is still correct - if (Str::startsWith($url, $localPrefix) === false && $url . '/' !== $localPrefix) { + // normalize homepage URLs to have a trailing slash + // to make the following logic work with those as well + if ($url . '/' === $localPrefix) { + $url .= '/'; + } + + if (Str::startsWith($url, $localPrefix) === false) { return null; } diff --git a/tests/Content/VersionTest.php b/tests/Content/VersionTest.php index bb64cf2eae..a15ffa6fe3 100644 --- a/tests/Content/VersionTest.php +++ b/tests/Content/VersionTest.php @@ -774,13 +774,33 @@ public function testMoveToVersion(): void $this->assertSame($content, Data::read($fileENLatest)); } + public static function previewTokenIndexUrlProvider() + { + return [ + ['/'], + ['/subfolder'], + ['/subfolder/'], + ['https://example.com'], + ['https://example.com/'], + ['https://example.com/subfolder'], + ['https://example.com/subfolder/'], + ]; + } + /** * @covers ::previewToken + * @dataProvider previewTokenIndexUrlProvider */ - public function testPreviewToken() + public function testPreviewToken(string $indexUrl) { $this->setUpSingleLanguage(); + $this->app = $this->app->clone([ + 'urls' => [ + 'index' => $indexUrl + ] + ]); + // site $version = new Version( model: $this->app->site(), @@ -789,7 +809,15 @@ public function testPreviewToken() $expected = substr(hash_hmac('sha1', '{"uri":"","versionId":"latest"}', static::TMP . '/content'), 0, 10); $this->assertSame($expected, $version->previewToken()); - // page + // homepage + $version = new Version( + model: $this->app->site()->page('home'), + id: VersionId::latest() + ); + $expected = substr(hash_hmac('sha1', '{"uri":"","versionId":"latest"}', static::TMP . '/content'), 0, 10); + $this->assertSame($expected, $version->previewToken()); + + // another page $version = new Version( model: $this->model, id: VersionId::latest()