Repository navigation
Is there a method that runs only once server-side on first page request? #18210
|
I'm new to next.js and I'm looking for a method (similar to The best solution I could find is to use But I think it is not ideal, I may want to make some API calls that don't want the code to be exposed to the client. |
Replies: 8 comments 22 replies
|
There is no mechanism within Next to do this, other than using If you do this then your page will not have the data it needs when you navigate to it via
There is no difference between this and rendering on the server - the data is still available to the client. There are no security issues because the client only receives the data you send it. If you don't want the client to receive some data then exclude it from being returned. If you really want to do this and use |
|
Hey @asajadian - what did you end doing? I'm struggling with the exactly same problem. |
|
Is there a way? I can provide some examples, where such thing would be nice to have:
|
|
So guys, I've implemented something like this: function MyApp({ Component, pageProps }: AppProps) {
// Save these guys in refs so they will stay the same no matter what.
const ab = useRef(pageProps.ab)
const isLaptop = useRef(pageProps.isLaptop)
return (
<ABProvider ab={ab.current}>
<IsLaptopProvider isLaptop={isLaptop.current}>
<Component {...pageProps} />
</IsLaptopProvider>
</ABProvider>
)
}
MyApp.getInitialProps = async (appContext: AppContext) => {
const appProps = await App.getInitialProps(appContext)
// Webpack will not include this in client bundle
if (typeof window === 'undefined') {
// Hacky check from mister @denu5
// Ensures that the expression under this condition will be executed
// only once on first page render.
// If the url starts with /_next/data, that means that we have
// a client-side navigation situation requiring some additional data
// from getServerSideProps or getInitialProps (not sure about this one).
if (!appContext.ctx.req?.url?.startsWith('/_next/data')) {
// Calculate something on server side
appProps.pageProps.isLaptop = isLaptop(appContext.ctx.req)
appProps.pageProps.ab = abMiddleware(appContext.ctx)
}
}
return { ...appProps }
}
export default MyApp |
|
Hey guys, I am looking for something similar, but within next-redux-wrapper. |
|
My solution for export const getStaticProps: GetStaticProps = async (
context
) => {
if (getApps().length === 0) initializeApp(ServerConfig); // <-- Key is here!
getDatabase()
.ref("/destination")
.on(
"value",
(snapshot) => {
console.log(snapshot.val());
},
(errorObject) => {
console.log("The read failed: " + errorObject.name);
}
);
return {
props: {
},
},
};
};Not sure if multiple reinitialize cause any damage? Furthermore, you can refactoring like this export const DB = () => {
if (getApps().length === 0) initializeApp(ServerConfig);
return getDatabase();
}; |
|
If we will use |
|
Like Vercel team doesn't provide any support yet, we should use separate service for everythink we need to link on the server startup. So that we can lunch theme just after having launched our next app. I did it for a cron job and everything is working good now. Happy debugging |
There is no mechanism within Next to do this, other than using
getInitialPropsas you mentioned.If you do this then your page will not have the data it needs when you navigate to it via
next/link. For example if you are on/page1and then click anext/linkto/page2,page2will not know what data to have and will behave differently ifgetServerSidePropslogic is not called. That is whygetServerSidePropsruns on the server and then makes afetchcall to get the data on the client.There is no difference between this and rendering on the server - the data is still available to the client. There are noβ¦