From 5783e9bb778983e1d8bac90a3e4f57bdf2a5c64d Mon Sep 17 00:00:00 2001 From: Chew Tee Ming Date: Fri, 24 Jan 2025 13:22:07 +0800 Subject: [PATCH] restore original path --- packages/kit/src/exports/adapter/index.js | 3 ++- packages/kit/src/runtime/server/respond.js | 11 +++++++++-- packages/kit/src/runtime/shared.js | 2 ++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/kit/src/exports/adapter/index.js b/packages/kit/src/exports/adapter/index.js index 6cfc9d36a49b..cf1a91b12699 100644 --- a/packages/kit/src/exports/adapter/index.js +++ b/packages/kit/src/exports/adapter/index.js @@ -1,4 +1,4 @@ -import { INVALIDATED_PARAM, TRAILING_SLASH_PARAM } from '../../runtime/shared.js'; +import { INVALIDATED_PARAM, ORIGINAL_PATH_PARAM, TRAILING_SLASH_PARAM } from '../../runtime/shared.js'; import { add_data_suffix, has_data_suffix, strip_data_suffix } from '../../utils/url.js'; /** @@ -28,6 +28,7 @@ export function applyReroute(url, reroute) { if (reroute_path) { const new_url = new URL(url); + new_url.searchParams.set(ORIGINAL_PATH_PARAM, url.pathname); new_url.pathname = is_data_request ? add_data_suffix(reroute_path) : reroute_path; return new_url; } diff --git a/packages/kit/src/runtime/server/respond.js b/packages/kit/src/runtime/server/respond.js index c1fb1fd63d0a..a1bddc1b7bc8 100644 --- a/packages/kit/src/runtime/server/respond.js +++ b/packages/kit/src/runtime/server/respond.js @@ -29,7 +29,7 @@ import { import { get_option } from '../../utils/options.js'; import { json, text } from '../../exports/index.js'; import { action_json_redirect, is_action_json_request } from './page/actions.js'; -import { INVALIDATED_PARAM, TRAILING_SLASH_PARAM } from '../shared.js'; +import { INVALIDATED_PARAM, ORIGINAL_PATH_PARAM, TRAILING_SLASH_PARAM } from '../shared.js'; import { get_public_env } from './env_module.js'; import { load_page_nodes } from './page/load_page_nodes.js'; import { get_page_config } from '../../utils/route_config.js'; @@ -57,9 +57,16 @@ const allowed_page_methods = new Set(['GET', 'HEAD', 'OPTIONS']); * @returns {Promise} */ export async function respond(request, options, manifest, state) { - /** URL but stripped from the potential `/__data.json` suffix and its search param */ + // URL but stripped from the potential `/__data.json` suffix and its search param const url = new URL(request.url); + // if the url has been rewritten by a middleware, we need to restore the original path + const original_path = url.searchParams.get(ORIGINAL_PATH_PARAM); + if (original_path) { + url.pathname = original_path; + url.searchParams.delete(ORIGINAL_PATH_PARAM); + } + if (options.csrf_check_origin) { const forbidden = is_form_content_type(request) && diff --git a/packages/kit/src/runtime/shared.js b/packages/kit/src/runtime/shared.js index b5c559b4292c..6570156e4a1b 100644 --- a/packages/kit/src/runtime/shared.js +++ b/packages/kit/src/runtime/shared.js @@ -14,3 +14,5 @@ export function validate_depends(route_id, dep) { export const INVALIDATED_PARAM = 'x-sveltekit-invalidated'; export const TRAILING_SLASH_PARAM = 'x-sveltekit-trailing-slash'; + +export const ORIGINAL_PATH_PARAM = 'x-sveltekit-original-path';