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

Code Splitting Example #96

Closed
Vani-gurnani opened this issue Nov 30, 2016 · 13 comments
Closed

Code Splitting Example #96

Vani-gurnani opened this issue Nov 30, 2016 · 13 comments

Comments

@Vani-gurnani
Copy link

Since preact is all about smaller Java Script it would be great if you can provide functionality to support Code Split for preact-router

@developit
Copy link
Member

It's unlikely to be a feature of preact-router itself, but I'd be happy to provide examples and recommended ways to do this. The simplest way is to use a compositional component like <SplitPoint /> as a child of the router:

<Router>
  <SplitPoint path="/" load={require('bundle?lazy!./home')} />
  <SplitPoint path="/user" load={require('bundle?lazy!./users')} />
  <SplitPoint path="/users/:user" load={require('bundle?lazy!./users/user')} />
  <SplitPoint default load={require('bundle?lazy!./not-found')} />
</Router>

@Vani-gurnani
Copy link
Author

Vani-gurnani commented Nov 30, 2016 via email

@developit
Copy link
Member

developit commented Nov 30, 2016

yup! you can pass any URL to the router and it will render the correct children accordingly:

import { h, Component } from 'preact';
import renderToString from 'preact-render-to-string';

// example app (it just needs to accept a url prop):
class MyApp extends Component {
  render({ url }) {
    return (
      <Router url={url}>
        <Home path="/" />
      </Router>
    );
  }
}

// example: express
app.get( (req, res) => {
  let html = renderToString(<MyApp url={req.url} />);
  res.send(html);
});

@Vani-gurnani
Copy link
Author

Vani-gurnani commented Nov 30, 2016 via email

@developit
Copy link
Member

There isn't one yet, but that sounds like a good idea! I don't do much server rendering since I like to deploy all my stuff to a CDN, but there are lots of people who want it. Perhaps a clone of preact-boilerplate that does SSR and shows how Code Splitting works would be ideal. The code splitting could be in both, really.

@Vani-gurnani
Copy link
Author

Vani-gurnani commented Nov 30, 2016 via email

@MatteoWebDeveloper
Copy link

@developit I was trying to add code splitting too for js and css too.

@developit
Copy link
Member

Code splitting for CSS is super tricky. I'm not aware of any great ways of doing that with Webpack currently. Usually you have to decide between loading all your CSS at once, or loading initial CSS but pushing less-used CSS into your lazy-loaded JavaScript modules.

@MatteoWebDeveloper
Copy link

MatteoWebDeveloper commented Jan 5, 2017

is it for the css order issue? I was about to try the second option.

@developit
Copy link
Member

No, I just don't know of an existing way of doing it cleanly. I guess the underlying reason would be that typically external stylesheets are created via ExtractTextPlugin in Webpack, and that plugin only supports extracting to a single external stylesheet.

@prateekbh
Copy link
Member

Code splitting can be easily done once #135 is resolved.

@prateekbh
Copy link
Member

prateekbh commented Feb 16, 2017

for code splitting and lazyloading components please visit
https://github.com/prateekbh/preact-async-route

can we close the issue @developit

@developit
Copy link
Member

Yup yup! Happy to close now that we have a great lib for this :)

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

No branches or pull requests

4 participants