mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-10-01 23:58:36 -05:00
Move app to apps/web-react in pnpm workspace layout
This commit is contained in:
26
apps/web-react/app/utils/request-context.server.ts
Normal file
26
apps/web-react/app/utils/request-context.server.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { AsyncLocalStorage } from "node:async_hooks";
|
||||
|
||||
// TODO: this is only needed for our current hacky toast setup, once a proper one in place this middleware can be deleted
|
||||
|
||||
interface RequestContext {
|
||||
/** Normalized request URL, as provided to middleware in framework mode
|
||||
* (single-fetch `.data` suffix and internal search params removed). */
|
||||
url: URL;
|
||||
}
|
||||
|
||||
const requestContextAsyncLocalStorage = new AsyncLocalStorage<RequestContext>();
|
||||
|
||||
/** Runs `fn` with the given request context available to server-side helpers
|
||||
* (e.g. toast redirects) that don't otherwise receive the request. */
|
||||
export function runWithRequestContext<T>(
|
||||
context: RequestContext,
|
||||
fn: () => T,
|
||||
): T {
|
||||
return requestContextAsyncLocalStorage.run(context, fn);
|
||||
}
|
||||
|
||||
/** Normalized pathname of the current request, or `undefined` outside a request
|
||||
* context. Used to build absolute redirects from helpers lacking the request. */
|
||||
export function currentRequestPathname(): string | undefined {
|
||||
return requestContextAsyncLocalStorage.getStore()?.url.pathname;
|
||||
}
|
||||
Reference in New Issue
Block a user