Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ async fn main() {
let api_routes = Router::new().nest("/v3", v3_routes);

let index = format!("{}{}", config::FRONTEND_PATH.to_string(), "/index.html");
// SPA fallback: serve `index.html` for client side routes.
// `fallback` instead of `not_found_service`, as the latter forces a `404` status code,
// while the document itself is served successfully. A missing note is signalled by the API.
let serve_dir =
ServeDir::new(config::FRONTEND_PATH.to_string()).not_found_service(ServeFile::new(index));
ServeDir::new(config::FRONTEND_PATH.to_string()).fallback(ServeFile::new(index));
let app = Router::new()
.nest("/api", api_routes)
.merge(health_routes)
Expand Down
19 changes: 19 additions & 0 deletions test/web/spa-fallback.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expect, test } from '@playwright/test'

// The SPA fallback serves index.html for client side routes. The document is
// served successfully, so it must not be reported as 404.
// A missing note is signalled by the API on /api/notes/<id> instead.
// https://github.com/cupcakearmy/cryptgeon/issues/217
test.describe('@web', () => {
for (const path of ['/', '/about', '/note/does-not-exist']) {
test(`serves ${path} with status 200`, async ({ request }) => {
const response = await request.get(path)
expect(response.status()).toBe(200)
})
}

test('api still reports a missing note as 404', async ({ request }) => {
const response = await request.get('/api/notes/does-not-exist')
expect(response.status()).toBe(404)
})
})