Skip to content

Instantly share code, notes, and snippets.

@h0od
h0od / prevent-rsc-prefetch.ts
Last active May 15, 2025 06:45
Workaround for duplicate call to route handler
import { RSC_HEADER } from 'next/dist/client/components/app-router-headers';
import { NextRequest } from 'next/server';
// Next.js tries to prefetch react server components stuff automatically.
// It does that by sending requests with the `_rsc` search param flag and
// also setting the `rsc` header.
// For some reason, Next.js also does this for route handlers when you use
// server-side and client-side navigation to these route handlers.
// So basically you will see two request to the same route handler, one
// with _rsc parameter and another without.
@h0od
h0od / typescript-helpers.ts
Created October 21, 2021 11:03
Typescript type helpers
type NullToUndefined<T> = {
[K in keyof T]: NullToUndefined<null extends T[K] ? Exclude<T[K], null> | undefined : T[K]>;
};
type UndefinedToNull<T> = {
[K in keyof T]: UndefinedToNull<undefined extends T[K] ? Exclude<T[K], undefined> | null : T[K]>;
};
@h0od
h0od / leftJoin.ts
Created October 11, 2021 11:38
Left join on TypeScript arrays
function leftJoin<A, B>(collection1: Array<A>, collection2: Array<B>, on: [keyof A, keyof B]): Array<A & Partial<B>> {
return collection1.map<A & Partial<B>>((leftObject) => {
const rightObject = collection2.find((item) => isEquals(leftObject, on[0], item, on[1]));
if (rightObject) {
return { ...leftObject, ...rightObject };
} else {
return { ...leftObject };
}
});
}