Skip to content

Commit d46830c

Browse files
committed
Improved handling of not found pages
1 parent 9d4fd52 commit d46830c

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/hooks.server.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,26 @@ if (env.FRACTAL_SERVER_HOST.endsWith('/')) {
1717
}
1818

1919
export async function handle({ event, resolve }) {
20-
if (event.url.pathname.startsWith('/_app')) {
21-
// The _app folder contains static files that are the result of npm build.
22-
// The hooks handle function is not called when loading valid static files, however we can
23-
// reach this point if a not existing file is requested. That could happen after an update
24-
// if the browser is loading a cached page that references to outdated contents.
25-
// In that case we can just skip the whole function.
26-
return await resolve(event);
27-
}
28-
2920
if (event.url.pathname.startsWith('/api')) {
3021
// API page - AJAX request - handled in proxy'
3122
logger.trace('API endpoint detected, leaving the handling to the proxy');
3223
return await resolve(event);
3324
}
3425

26+
if (event.route.id === null) {
27+
if (event.url.pathname.startsWith('/_app')) {
28+
// The _app folder contains static files that are the result of npm build.
29+
// The hooks handle function is not called when loading valid static files, however we can
30+
// reach this point if a not existing file is requested. That could happen after an update
31+
// if the browser is loading a cached page that references to outdated contents.
32+
// In that case we can usually ignore the logs.
33+
logger.trace('[%s] - %s - [NOT FOUND]', event.request.method, event.url.pathname);
34+
} else {
35+
logger.info('[%s] - %s - [NOT FOUND]', event.request.method, event.url.pathname);
36+
}
37+
throw error(404, 'Route not found');
38+
}
39+
3540
logger.info('[%s] - %s', event.request.method, event.url.pathname);
3641

3742
// Retrieve server info (alive and version)

tests/not_found.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { expect, test } from '@playwright/test';
2+
3+
test('Handles not found pages', async ({ page }) => {
4+
await page.goto('/foo');
5+
await expect(page.getByText('Route not found')).toHaveCount(1);
6+
await page.goto('/_app/foo');
7+
await expect(page.getByText('Route not found')).toHaveCount(1);
8+
});

0 commit comments

Comments
 (0)