Skip to content
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

"Ideal" code splitting with Relay #1317

Closed
taion opened this issue Aug 2, 2016 · 10 comments
Closed

"Ideal" code splitting with Relay #1317

taion opened this issue Aug 2, 2016 · 10 comments

Comments

@taion
Copy link
Contributor

taion commented Aug 2, 2016

I don't know if this is the right place for this.

One thing that seems difficult – as in I can't figure out how to do it – right now is doing code splitting in an ideal way with Relay.

Typically code splitting happens along module boundaries. However, ideally it would be good to load data from a remote API server concurrently with loading the JS code for "dumb" components.

However, there isn't really a straightforward way to get this working with e.g. System.import with Relay because the data requirements are colocated.

To be more concrete, I think what I want to do is to not split out the queries and fragments, and instead only split out the actual React components. Doing this would be tricky, though – maybe something to stub out the actual components and only keep the Relay container definitions?

@rterysen-openroad
Copy link

Do you mean the concept similar to a styleguide? Ability to pull in react components to only behave as the view and have no knowlegde of relay or have to care about data outside there props?

@taion
Copy link
Contributor Author

taion commented Aug 2, 2016

The opposite, actually – I think I want to be able to just pull in the code that defines the data dependencies for a component (and its children), while code splitting away the actual rendering code for those components (via e.g. System.import), so I can concurrently load the data and the rendering code.

@josephsavona
Copy link
Contributor

josephsavona commented Aug 3, 2016

If I understand correctly the use case is as follows: given that you're about to transition to some view, download all the data and code necessary to render it.

In current Relay one option to support this is as you described. Download just enough code to construct the query, then fetch the query and the remaining code in parallel. This could work, and it's likely possible to implement this via a custom build script.

Our goal is to support a different and hopefully more efficient alternative: fetching both the data and code in parallel. This requires knowing statically what query a view will send. We're working on an update to Relay core that is able to extract Relay queries at build time. This would allow higher-level tools to associate a specific query with a route, for example, and know what data to fetch without downloading any code. We'll discuss this more over the coming weeks in a blog post and some meetup/conference talks.

@taion
Copy link
Contributor Author

taion commented Aug 3, 2016

Awesome. That's exactly what I'm looking for.

The context here is the React Router exposes a hook (getComponent) to load the component corresponding to a React Router route on-demand. However, this is suboptimal for working with Relay, because I can't make the query until after I've received the code for the split out component, whereas ideally I'd like to do both at the same time.

Is there a meta-issue related to that update? Does it make sense to keep this issue open?

@taion
Copy link
Contributor Author

taion commented Aug 3, 2016

Oops, I misread – I was actually trying to describe something like what you had. Keep the code required to build the query in the "main" bundle, so I can concurrently fetch the data and the code for the split-out bundle.

In other words, two parallel network requests, rather than a single "small" network request followed by 2 larger network requests.

@josephsavona
Copy link
Contributor

@taion see #1369 (Relay 2 meta task) - one of the things that static queries enables is to prefetch the data for a route without having to download and execute the code for that route. Given that the framework will provide core support for prefetching, we'd be happy to support you and the community in integrating prefetching into popular bundling tools.

@taion
Copy link
Contributor Author

taion commented Jun 12, 2017

This just works with Relay Modern now. 👍

@taion taion closed this as completed Jun 12, 2017
@gorfadvijay
Copy link

@taion hey do you have any update how can i do code spiltting in relay and found router

@sibelius
Copy link
Contributor

@taion
Copy link
Contributor Author

taion commented Dec 29, 2019

@taion hey do you have any update how can i do code spiltting in relay and found router

If you're currently using Found with the current non-experimental functionality, all you have to do is replace Component={MyComponent} with something like getComponent={() => import('./MyComponent.js').then(m => m.default)}, and Webpack will deal with the rest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants