From c183f6b55334a14ac6d963df7c66dcf6fd62a552 Mon Sep 17 00:00:00 2001 From: jervtub Date: Fri, 15 May 2020 12:43:44 +0200 Subject: [PATCH] Pull request to resolve issue #1442 https://github.com/sveltejs/sapper/issues/1142 Route with address "/client/" is not working. The first request to, for instance "http://localhost/client/new" would pass the filter function, as [`req.start.startsWith(prefix)`](https://github.com/sveltejs/sapper/blob/facbd96e0bd2539acf32c5ac14cd9478c7c635e5/runtime/src/server/middleware/index.ts#L104) would pass. I assume all the content stored under the manifest has an extension, such as ".js" and ".svelte". Therefore, adding a new filter to have an extension should be sufficient to resolve this issue. --- runtime/src/server/middleware/index.ts | 7 ++++++- test/apps/basics/src/routes/apple/new.svelte | 1 + test/apps/basics/src/routes/client/client/new.svelte | 1 + test/apps/basics/src/routes/client/index.svelte | 1 + test/apps/basics/src/routes/client/new.svelte | 1 + test/apps/basics/test.ts | 9 +++++++++ 6 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 test/apps/basics/src/routes/apple/new.svelte create mode 100644 test/apps/basics/src/routes/client/client/new.svelte create mode 100644 test/apps/basics/src/routes/client/index.svelte create mode 100644 test/apps/basics/src/routes/client/new.svelte diff --git a/runtime/src/server/middleware/index.ts b/runtime/src/server/middleware/index.ts index 96fd9ea05..eeb774f5b 100644 --- a/runtime/src/server/middleware/index.ts +++ b/runtime/src/server/middleware/index.ts @@ -99,9 +99,14 @@ export function serve({ prefix, pathname, cache_control }: { pathname?: string, cache_control: string }) { + // Filter requests based on req.path. const filter = pathname ? (req: Req) => req.path === pathname - : (req: Req) => req.path.startsWith(prefix); + /* + * [#1442](https://github.com/sveltejs/sapper/issues/1142) + * Exception: Ensure an extension exists in the pathname, to filter out the first request to routes under "/client/". + */ + : (req: Req) => req.path.startsWith(prefix) && /\..*$/.test(req.path); const cache: Map = new Map(); diff --git a/test/apps/basics/src/routes/apple/new.svelte b/test/apps/basics/src/routes/apple/new.svelte new file mode 100644 index 000000000..a9d787cc5 --- /dev/null +++ b/test/apps/basics/src/routes/apple/new.svelte @@ -0,0 +1 @@ +Success. diff --git a/test/apps/basics/src/routes/client/client/new.svelte b/test/apps/basics/src/routes/client/client/new.svelte new file mode 100644 index 000000000..ef8e05020 --- /dev/null +++ b/test/apps/basics/src/routes/client/client/new.svelte @@ -0,0 +1 @@ +

Success.

diff --git a/test/apps/basics/src/routes/client/index.svelte b/test/apps/basics/src/routes/client/index.svelte new file mode 100644 index 000000000..ef8e05020 --- /dev/null +++ b/test/apps/basics/src/routes/client/index.svelte @@ -0,0 +1 @@ +

Success.

diff --git a/test/apps/basics/src/routes/client/new.svelte b/test/apps/basics/src/routes/client/new.svelte new file mode 100644 index 000000000..ef8e05020 --- /dev/null +++ b/test/apps/basics/src/routes/client/new.svelte @@ -0,0 +1 @@ +

Success.

diff --git a/test/apps/basics/test.ts b/test/apps/basics/test.ts index f1313b81a..5ce42f887 100644 --- a/test/apps/basics/test.ts +++ b/test/apps/basics/test.ts @@ -386,4 +386,13 @@ describe('basics', function() { it('survives the tests with no server errors', () => { assert.deepEqual(r.errors, []); }); + + it('finds routes under /client/', async () => { + await r.load('/client'); + assert.equal(await r.text('h1'), 'Success.'); + await r.load('/client/new'); + assert.equal(await r.text('h1'), 'Success.'); + await r.load('/client/client/new'); + assert.equal(await r.text('h1'), 'Success.'); + }) });