Skip to content

Instantly share code, notes, and snippets.

@ogzhanolguncu
Created October 9, 2022 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ogzhanolguncu/609237854aff09a327f6e08f4be5dd46 to your computer and use it in GitHub Desktop.
Save ogzhanolguncu/609237854aff09a327f6e08f4be5dd46 to your computer and use it in GitHub Desktop.
Wrapper for NextJS SSR
//Add cache header wrapper
type Options = {
kickUser: boolean;
};
type InjectedProps = {
userLocation: string;
};
type GetServerSidePropsWithInjectedProps = GetServerSidePropsContext &
InjectedProps;
type Callback = (
context: GetServerSidePropsWithInjectedProps
) => Promise<GetServerSidePropsResult<any>>;
export const bounceUsersIfCredentialsHaveNotMet = (
cb: Callback,
options?: Options
) => {
console.log("STEP1");
return async (ctx: GetServerSidePropsWithInjectedProps) => {
//We can kick users,
//Invalidate caches,
//Update headers
if (options?.kickUser) {
return {
notFound: true,
};
}
const injectedContext = {
...ctx,
userLocation: "TR",
};
return cb?.(injectedContext) || {};
};
};
export const logUserInfo = (cb: Callback) => {
return async (ctx: GetServerSidePropsWithInjectedProps) => {
//CALL LOGGING FUNCTIONS
console.log("STEP2");
return cb?.(ctx) || {};
};
};
const getServerSideHomeProps = async (
context: GetServerSidePropsWithInjectedProps
) => {
console.log("STEP3");
return {
props: {
listOfNumbers: [1, 2, 3, 4],
},
};
};
export const getServerSideProps = bounceUsersIfCredentialsHaveNotMet(
logUserInfo(getServerSideHomeProps),
{
kickUser: false,
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment