Wrapper for NextJS SSR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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