From 7df5b6054a59e8a39945328dbbf5770e708cd6e0 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 2 Aug 2021 13:58:13 -0700 Subject: [PATCH] [fix] correctly do fallthrough in simple case --- .changeset/wet-beds-thank.md | 5 +++ packages/kit/src/runtime/server/endpoint.js | 6 +-- packages/kit/src/runtime/server/index.js | 15 ++++++-- packages/kit/src/runtime/server/page/index.js | 38 +++++++------------ .../apps/basics/src/routes/routing/_tests.js | 22 +++++++---- .../[animal].json.js | 0 .../[animal].svelte | 2 +- .../[mineral].json.js | 0 .../[mineral].svelte | 2 +- .../[vegetable].json.js | 0 .../[vegetable].svelte | 2 +- .../fallthrough-advanced/__layout.svelte | 7 ++++ .../routing/fallthrough-simple/[legal].svelte | 13 +++++++ .../routing/fallthrough-simple/[page].svelte | 9 +++++ .../routing/fallthrough/__layout.svelte | 7 ---- 15 files changed, 81 insertions(+), 47 deletions(-) create mode 100644 .changeset/wet-beds-thank.md rename packages/kit/test/apps/basics/src/routes/routing/{fallthrough => fallthrough-advanced}/[animal].json.js (100%) rename packages/kit/test/apps/basics/src/routes/routing/{fallthrough => fallthrough-advanced}/[animal].svelte (81%) rename packages/kit/test/apps/basics/src/routes/routing/{fallthrough => fallthrough-advanced}/[mineral].json.js (100%) rename packages/kit/test/apps/basics/src/routes/routing/{fallthrough => fallthrough-advanced}/[mineral].svelte (81%) rename packages/kit/test/apps/basics/src/routes/routing/{fallthrough => fallthrough-advanced}/[vegetable].json.js (100%) rename packages/kit/test/apps/basics/src/routes/routing/{fallthrough => fallthrough-advanced}/[vegetable].svelte (81%) create mode 100644 packages/kit/test/apps/basics/src/routes/routing/fallthrough-advanced/__layout.svelte create mode 100644 packages/kit/test/apps/basics/src/routes/routing/fallthrough-simple/[legal].svelte create mode 100644 packages/kit/test/apps/basics/src/routes/routing/fallthrough-simple/[page].svelte delete mode 100644 packages/kit/test/apps/basics/src/routes/routing/fallthrough/__layout.svelte diff --git a/.changeset/wet-beds-thank.md b/.changeset/wet-beds-thank.md new file mode 100644 index 000000000000..5544d3169ada --- /dev/null +++ b/.changeset/wet-beds-thank.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +[fix] correctly do fallthrough in simple case diff --git a/packages/kit/src/runtime/server/endpoint.js b/packages/kit/src/runtime/server/endpoint.js index c532e2c10b41..7ba8cd46adbf 100644 --- a/packages/kit/src/runtime/server/endpoint.js +++ b/packages/kit/src/runtime/server/endpoint.js @@ -18,16 +18,16 @@ function is_string(s) { /** * @param {import('types/hooks').ServerRequest} request * @param {import('types/internal').SSREndpoint} route - * @returns {Promise} + * @returns {Promise} */ -export default async function render_route(request, route) { +export async function render_endpoint(request, route) { const mod = await route.load(); /** @type {import('types/endpoint').RequestHandler} */ const handler = mod[request.method.toLowerCase().replace('delete', 'del')]; // 'delete' is a reserved word if (!handler) { - return error('no handler'); + return; } const match = route.pattern.exec(request.path); diff --git a/packages/kit/src/runtime/server/index.js b/packages/kit/src/runtime/server/index.js index 24c8cbb47796..03550e2adc83 100644 --- a/packages/kit/src/runtime/server/index.js +++ b/packages/kit/src/runtime/server/index.js @@ -1,6 +1,7 @@ -import render_page from './page/index.js'; +import { render_endpoint } from './endpoint.js'; +import { render_page } from './page/index.js'; import { render_response } from './page/render.js'; -import render_endpoint from './endpoint.js'; +import { respond_with_error } from './page/respond_with_error.js'; import { parse_body } from './parse_body/index.js'; import { coalesce_to_error, lowercase_keys } from './utils.js'; import { hash } from '../hash.js'; @@ -84,7 +85,15 @@ export async function respond(incoming, options, state = {}) { } } - return await render_page(request, null, options, state); + const $session = await options.hooks.getSession(request); + return await respond_with_error({ + request, + options, + state, + $session, + status: 404, + error: new Error(`Not found: ${request.path}`) + }); } }); } catch (/** @type {unknown} */ err) { diff --git a/packages/kit/src/runtime/server/page/index.js b/packages/kit/src/runtime/server/page/index.js index fb0bf6860eff..b5caee60df1e 100644 --- a/packages/kit/src/runtime/server/page/index.js +++ b/packages/kit/src/runtime/server/page/index.js @@ -1,14 +1,13 @@ import { respond } from './respond.js'; -import { respond_with_error } from './respond_with_error.js'; /** * @param {import('types/hooks').ServerRequest} request - * @param {import('types/internal').SSRPage | null} route + * @param {import('types/internal').SSRPage} route * @param {import('types/internal').SSRRenderOptions} options * @param {import('types/internal').SSRRenderState} state - * @returns {Promise} + * @returns {Promise} */ -export default async function render_page(request, route, options, state) { +export async function render_page(request, route, options, state) { if (state.initiator === route) { // infinite request cycle detected return { @@ -20,19 +19,19 @@ export default async function render_page(request, route, options, state) { const $session = await options.hooks.getSession(request); - if (route) { - const response = await respond({ - request, - options, - state, - $session, - route - }); + const response = await respond({ + request, + options, + state, + $session, + route + }); - if (response) { - return response; - } + if (response) { + return response; + } + if (state.fetched) { // we came here because of a bad request in a `load` function. // rather than render the error page — which could lead to an // infinite loop, if the `load` belonged to the root layout, @@ -42,14 +41,5 @@ export default async function render_page(request, route, options, state) { headers: {}, body: `Bad request in load function: failed to fetch ${state.fetched}` }; - } else { - return await respond_with_error({ - request, - options, - state, - $session, - status: 404, - error: new Error(`Not found: ${request.path}`) - }); } } diff --git a/packages/kit/test/apps/basics/src/routes/routing/_tests.js b/packages/kit/test/apps/basics/src/routes/routing/_tests.js index 852706add695..14bb393ad1f7 100644 --- a/packages/kit/test/apps/basics/src/routes/routing/_tests.js +++ b/packages/kit/test/apps/basics/src/routes/routing/_tests.js @@ -202,15 +202,23 @@ export default function (test) { } ); - test('falls through', '/routing/fallthrough/borax', async ({ page, clicknav }) => { - assert.equal(await page.textContent('h1'), 'borax is a mineral'); + test('fallthrough', '/routing/fallthrough-simple/invalid', async ({ page, clicknav }) => { + assert.equal(await page.textContent('h1'), 'Page'); + }); - await clicknav('[href="/routing/fallthrough/camel"]'); - assert.equal(await page.textContent('h1'), 'camel is an animal'); + test( + 'dynamic fallthrough of pages and endpoints', + '/routing/fallthrough-advanced/borax', + async ({ page, clicknav }) => { + assert.equal(await page.textContent('h1'), 'borax is a mineral'); - await clicknav('[href="/routing/fallthrough/potato"]'); - assert.equal(await page.textContent('h1'), '404'); - }); + await clicknav('[href="/routing/fallthrough-advanced/camel"]'); + assert.equal(await page.textContent('h1'), 'camel is an animal'); + + await clicknav('[href="/routing/fallthrough-advanced/potato"]'); + assert.equal(await page.textContent('h1'), '404'); + } + ); test( 'last parameter in a segment wins in cases of ambiguity', diff --git a/packages/kit/test/apps/basics/src/routes/routing/fallthrough/[animal].json.js b/packages/kit/test/apps/basics/src/routes/routing/fallthrough-advanced/[animal].json.js similarity index 100% rename from packages/kit/test/apps/basics/src/routes/routing/fallthrough/[animal].json.js rename to packages/kit/test/apps/basics/src/routes/routing/fallthrough-advanced/[animal].json.js diff --git a/packages/kit/test/apps/basics/src/routes/routing/fallthrough/[animal].svelte b/packages/kit/test/apps/basics/src/routes/routing/fallthrough-advanced/[animal].svelte similarity index 81% rename from packages/kit/test/apps/basics/src/routes/routing/fallthrough/[animal].svelte rename to packages/kit/test/apps/basics/src/routes/routing/fallthrough-advanced/[animal].svelte index 567f828b13b4..7d5a2ddf6b43 100644 --- a/packages/kit/test/apps/basics/src/routes/routing/fallthrough/[animal].svelte +++ b/packages/kit/test/apps/basics/src/routes/routing/fallthrough-advanced/[animal].svelte @@ -1,7 +1,7 @@ + +

Legal

diff --git a/packages/kit/test/apps/basics/src/routes/routing/fallthrough-simple/[page].svelte b/packages/kit/test/apps/basics/src/routes/routing/fallthrough-simple/[page].svelte new file mode 100644 index 000000000000..25f1bfc0ec68 --- /dev/null +++ b/packages/kit/test/apps/basics/src/routes/routing/fallthrough-simple/[page].svelte @@ -0,0 +1,9 @@ + + +

Page

diff --git a/packages/kit/test/apps/basics/src/routes/routing/fallthrough/__layout.svelte b/packages/kit/test/apps/basics/src/routes/routing/fallthrough/__layout.svelte deleted file mode 100644 index d57c9493c4cb..000000000000 --- a/packages/kit/test/apps/basics/src/routes/routing/fallthrough/__layout.svelte +++ /dev/null @@ -1,7 +0,0 @@ - - -aluminium -broccoli -camel - -potato \ No newline at end of file