Skip to content

Commit 378b2d9

Browse files
authored
Merge pull request #1852 from hydephp/improve-realtime-compiler-routing-for-nested-index-pages
Improve realtime compiler routing for nested index pages
2 parents 46b6ee0 + d1b9ec5 commit 378b2d9

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ This serves two purposes:
2929
- Added missing collection key types in Hyde facade method annotations in https://github.com/hydephp/develop/pull/1784
3030
- Fixed heading permalinks button text showing in Google Search previews https://github.com/hydephp/develop/issues/1801 in https://github.com/hydephp/develop/pull/1803
3131
- Realtime Compiler: Updated the exception handler to match HTTP exception codes when sending error responses in https://github.com/hydephp/develop/pull/1853
32+
- Realtime Compiler: Improved routing for nested index pages in https://github.com/hydephp/develop/pull/1852
3233

3334
### Security
3435
- in case of vulnerabilities.

packages/realtime-compiler/src/Http/Middleware/PathNormalizerMiddleware.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
class PathNormalizerMiddleware
1010
{
1111
protected array $pathRewrites = [
12-
'/docs' => '/docs/index',
1312
'/docs/search.html' => '/docs/search',
1413
];
1514

packages/realtime-compiler/src/Routing/PageRouter.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Hyde\Pages\Concerns\BaseMarkdownPage;
1111
use Hyde\Framework\Actions\StaticPageBuilder;
1212
use Hyde\RealtimeCompiler\Http\LiveEditController;
13+
use Hyde\Framework\Exceptions\RouteNotFoundException;
1314
use Hyde\Framework\Features\Documentation\DocumentationSearchPage;
1415
use Hyde\Pages\Concerns\HydePage;
1516
use Hyde\RealtimeCompiler\Concerns\InteractsWithLaravel;
@@ -98,6 +99,16 @@ protected function getPageFromRoute(): HydePage
9899
return new DocumentationSearchPage();
99100
}
100101

101-
return Routes::getOrFail($this->normalizePath($this->request->path))->getPage();
102+
try {
103+
return Routes::getOrFail($this->normalizePath($this->request->path))->getPage();
104+
} catch (RouteNotFoundException $exception) {
105+
$index = Routes::get($this->normalizePath($this->request->path).'/index');
106+
107+
if ($index) {
108+
return $index->getPage();
109+
}
110+
111+
throw $exception;
112+
}
102113
}
103114
}

packages/realtime-compiler/tests/Integration/IntegrationTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,39 @@ public function test404()
1919
->assertSeeText('Route [non-existent-page] not found.');
2020
}
2121

22+
public function testNestedIndexPageRouting()
23+
{
24+
if (! is_dir($this->projectPath('_pages/about'))) {
25+
mkdir($this->projectPath('_pages/about'), 0755, true);
26+
}
27+
28+
file_put_contents($this->projectPath('_docs/index.md'), '# Documentation');
29+
file_put_contents($this->projectPath('_pages/about/index.md'), '# About');
30+
file_put_contents($this->projectPath('_pages/about/contact.md'), '# Contact');
31+
32+
$this->get('/docs/index.html')->assertStatus(200)->assertSeeText('Documentation');
33+
$this->get('/about/index.html')->assertStatus(200)->assertSeeText('About');
34+
$this->get('/about/contact.html')->assertStatus(200)->assertSeeText('Contact');
35+
36+
$this->get('/docs/index')->assertStatus(200)->assertSeeText('Documentation');
37+
$this->get('/about/index')->assertStatus(200)->assertSeeText('About');
38+
$this->get('/about/contact')->assertStatus(200)->assertSeeText('Contact');
39+
40+
$this->get('/docs/')->assertStatus(200)->assertSeeText('Documentation');
41+
$this->get('/about/')->assertStatus(200)->assertSeeText('About');
42+
$this->get('/about/contact/')->assertStatus(200)->assertSeeText('Contact');
43+
44+
$this->get('/docs')->assertStatus(200)->assertSeeText('Documentation');
45+
$this->get('/about')->assertStatus(200)->assertSeeText('About');
46+
$this->get('/about/contact')->assertStatus(200)->assertSeeText('Contact');
47+
48+
$this->get('/about/contact/index')->assertStatus(404);
49+
50+
unlink($this->projectPath('_docs/index.md'));
51+
unlink($this->projectPath('_pages/about/index.md'));
52+
unlink($this->projectPath('_pages/about/contact.md'));
53+
}
54+
2255
public function testDynamicDocumentationSearchPages()
2356
{
2457
file_put_contents($this->projectPath('_docs/index.md'), '# Documentation');
@@ -28,6 +61,10 @@ public function testDynamicDocumentationSearchPages()
2861
->assertStatus(200)
2962
->assertSeeText('Search the documentation site');
3063

64+
$this->get('/docs/search.html')
65+
->assertStatus(200)
66+
->assertSeeText('Search the documentation site');
67+
3168
$this->get('/docs/search.json')
3269
->assertStatus(200)
3370
->assertHeader('Content-Type', 'application/json')

packages/realtime-compiler/tests/Integration/TestResponse.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class TestResponse
1010
protected ResponseInterface $response;
1111
protected IntegrationTestCase $test;
1212

13+
protected string $uri;
1314
protected string $html;
1415
protected string $text;
1516

@@ -19,13 +20,14 @@ public static function get(IntegrationTestCase $test, string $uri): static
1920

2021
$response = $guzzle->get('http://localhost:8080'.$uri, ['http_errors' => false]);
2122

22-
return new static($test, $response);
23+
return new static($test, $response, $uri);
2324
}
2425

25-
public function __construct(IntegrationTestCase $test, ResponseInterface $response)
26+
public function __construct(IntegrationTestCase $test, ResponseInterface $response, string $uri)
2627
{
2728
$this->test = $test;
2829
$this->response = $response;
30+
$this->uri = $uri;
2931

3032
$this->parsePageData();
3133
}
@@ -70,7 +72,7 @@ public function ddHeaders(): void
7072

7173
public function assertStatus(int $code): static
7274
{
73-
$this->test->assertSame($code, $this->response->getStatusCode());
75+
$this->test->assertSame($code, $this->response->getStatusCode(), sprintf('Did not receive expected code %s when accessing %s', $code, $this->uri));
7476

7577
return $this;
7678
}

0 commit comments

Comments
 (0)