-
Notifications
You must be signed in to change notification settings - Fork 27.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cleaner server-side dependency injection into getInitialProps
?
#1117
Comments
If I understood right, sounds like a similar need to #978 |
My issue is more about getting server-specific inputs into |
@finneganh It's easy to add an interface with HOC to handle that. The problem is with |
Using a HOC to pull things off of the |
@reel |
I want to give a +1 for this. Currently, I'm setting up the GraphQL server in the same express instance with next.js . (I know it's bad practice but for a side project it's more than enough). Since Heroku disable local network, I had to perform some weird trick to proxy the request back to my server when doing server side rendering. Apollo Server document stated that I should use https://github.com/af/apollo-local-query to avoid going through the HTTP stack. But doing that mean I need to bundle postgres, my schema and everything in the client bundle. |
I found a solution for this... If you're using apollo or any other query that can be done server-side and client side, export your data fetching function from your React component file. Then, once you hit that route on the server, require the dist file and try to execute that function. Then, inject the result in Reference: https://github.com/simplehub/henri/#withdata I know it's not bulletproof but it works! |
I know this thread is old but it seems like one way to do this is to allow passing custom data in the That call seems to be done here: https://github.com/zeit/next.js/blob/v3-beta/server/render.js#L63-L64 Would be a relatively simple fix if there's interest in a PR for this. |
@unregistered You can use eval
Webpack can't resolve eval so it won't end up in client bundle |
@thangngoc89 The idea is that data should be fetched server-side and passed though the component on render for a truly isomorphic SSR. The current method seems to be passing data through the @unregistered is suggesting that a simple code change could make this more explicit by adding a |
@thangngoc89 For those who already have a bunch of server-side data fetching being done in routes in their express apps, being able to pass the data through the Also surely recommending consumers of your library to use eval to hack around webpack is a bit inelegant? |
@unregistered it's just my workaround for my usecase (see above). It works great for me. But you can do it another way if you like. IMHO, going through the HTTP stack is the easiest way of doing this. Same logic for client and server |
@unregistered I'm not highly opposed to your idea of adding another parameter. @arunoda what do you think? 😄 |
@unregistered @thangngoc89 I'm totally opposed of adding a server only stuff to Next.js. That's not the goal of Next.js. If you want you can still fetch data and pass into your app via queryParams in the server side. Use that option. No need to add a new one. The whole reason we are doing this to discourage you to mixing data loading stuff in the Next.js app. We want to make it minimal as possible. If not, feel free to use custom server API and pass data via query string. |
@arunoda that's fair, I wasn't aware of that queryParams could be used this way. Perhaps this could be a candidate for an example project in Regarding moving to a separate server, we have pages where data is cached in memory so we can render quickly (particularly important for an e-commerce site with a lot of traffic), and we'd rather not hop into the network stack to call localhost for the initial page render. |
I think there's no reason not to do that on Next.js itself. Even you can do it inside the Having a example for this case would be something great. I'd love to have it. |
For custom server routes, I register a route that renders the component while injecting data in queryParams and I also register a /_data/(same route) twin route. Both routes go to the same controller but the latter, in the render function, sees the /_data/ section of url and simply res.json instead of rendering a component |
@arunoda I did try that, but I had difficulty doing this in @reel Thanks for the suggestion. I ended up kind of doing that, but opted to go with express's content negotiation feature:
The PR with the example: #2594 Feedback appreciated 😀 |
@unregistered even with v3, you need to use eval. See this post. Yep. Doing that in express looks nice and clean. |
@unregistered great solution. will definitely steal it :) |
* Add example on how to pass data through js api during SSR Requested in #1117 * Use content negotiation instead of a separate route * Codereview feedback * Move security related test cases into a its own file. * Removes the unused renderScript function * Add a nerv example. (#3573) * Add a nerv example. * Fix for indentation/style * Fix for name
Just wanted to raise something that I’ve noticed using the beta.
During server-side rendering (using the custom routing and
renderToHTML
), there are some server-side dependencies I want to make available (for example, a means of injecting requests directly into my Hapi app so that the server-side data fetching doesn’t go through a network interface). In some cases I also have data to seed an initial Redux store based on the request.Currently I’m getting this data in by attaching custom properties to the Node
Request
object that’s passed in to the context.This is fine, though a little awkward. I was raising this up in case you want to make it easier to add server-specific properties to the
getInitialProps
context.The text was updated successfully, but these errors were encountered: