Repository navigation
Replies: 2 comments 2 replies
2 replies
|
I found a hacky solution that monkey-patches This is better than the solution described in #32021 (comment) because it avoids the |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I've tried asking this question in other places ( stackoverflow, reddit ), but unfortunately the answers so far still feel kind of hacky, and still require using
getInitialPropsin _app.js.So hopefully someone has a better idea here. Using NextJS, how can one run code that:
a) only runs serverside and not client-side
b) only runs on initial page load, and not on page transitions
c) does not require using
getInitialPropsin _app.jsHere are some strategies that don't work:
getServerSideProps: This code will run only on serverside, but it is also invoked on page transitions as part of an api call that returns jsonin App component: Assuming your parent-level App component only renders once, you can write code that won't run across page transitions since App is already mounted, however, it runs both server-side and client-side.(Correction: Even if App renders only once on a page load, it will still render on page transitions.)getInitialPropsin _app.js. Unfortunately,getInitialPropswill run on page transitions if transitioning to a page withgetServerSideProps. It will also run client-side if transitioning to a page withoutgetServerSideProps. This can be avoided by examiningrequest.urlfor the prefix/_next/data, but this feels hacky and unsupported. Besides, even after that it can be called in error scenarios. Finally (and biggest), I want to avoidgetInitialPropsentirely since it is not recommended, and since it turns off automatic static optimizations for other pages.Options I haven't looked into yet:
Anything else that is an option? Ideally something that is truly "the NextJS way"... it seems running "serverside app initialization" would be a common concern, at least it has come up a lot for us.
All reactions