Skip to content

Commit

Permalink
restore original path
Browse files Browse the repository at this point in the history
  • Loading branch information
eltigerchino committed Jan 24, 2025
1 parent e38a660 commit 5783e9b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/kit/src/exports/adapter/index.js
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down Expand Up @@ -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;
}
Expand Down
11 changes: 9 additions & 2 deletions packages/kit/src/runtime/server/respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -57,9 +57,16 @@ const allowed_page_methods = new Set(['GET', 'HEAD', 'OPTIONS']);
* @returns {Promise<Response>}
*/
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) &&
Expand Down
2 changes: 2 additions & 0 deletions packages/kit/src/runtime/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit 5783e9b

Please sign in to comment.