Revert "feat: add ctx.redirect() helper (#2358)" (#2372)

Mostly so that it's easier to cut the next patch release for Fresh.

This reverts commit e3508c3054.
This commit is contained in:
Marvin Hagemeister
2024-03-21 10:27:53 +01:00
committed by GitHub
parent c797716a56
commit 240252fdb4
8 changed files with 20 additions and 82 deletions
+6 -27
View File
@@ -114,31 +114,6 @@ export async function getServerContext(state: InternalFreshState) {
);
}
function redirectTo(pathOrUrl: string = "/", status = 302): Response {
let location = pathOrUrl;
// Disallow protocol relative URLs
if (pathOrUrl !== "/" && pathOrUrl.startsWith("/")) {
let idx = pathOrUrl.indexOf("?");
if (idx === -1) {
idx = pathOrUrl.indexOf("#");
}
const pathname = idx > -1 ? pathOrUrl.slice(0, idx) : pathOrUrl;
const search = idx > -1 ? pathOrUrl.slice(idx) : "";
// Remove double slashes to prevent open redirect vulnerability.
location = `${pathname.replaceAll(/\/+/g, "/")}${search}`;
}
return new Response(null, {
status,
headers: {
location,
},
});
}
export class ServerContext {
#renderFn: RenderFunction;
#plugins: Plugin[];
@@ -308,7 +283,6 @@ export class ServerContext {
ctx.data = data;
return await renderNotFound(req, ctx);
},
redirect: redirectTo,
route: "",
get pattern() {
return ctx.route;
@@ -635,7 +609,12 @@ export class ServerContext {
if (key !== null && BUILD_ID !== key) {
url.searchParams.delete(ASSET_CACHE_BUST_KEY);
const location = url.pathname + url.search;
return redirectTo(location, 307);
return new Response(null, {
status: 307,
headers: {
location,
},
});
}
const headers = new Headers({
"content-type": contentType,
+1 -2
View File
@@ -157,7 +157,7 @@ export type PageProps<T = any, S = Record<string, unknown>> = Omit<
S,
T
>,
"render" | "next" | "renderNotFound" | "redirect"
"render" | "next" | "renderNotFound"
>;
export interface StaticFile {
@@ -206,7 +206,6 @@ export interface FreshContext<
) => Response | Promise<Response>;
Component: ComponentType<unknown>;
next: () => Promise<Response>;
redirect: (path: string, statusCode?: number) => Response;
}
/**
* Context passed to async route components.