Giter Site home page Giter Site logo

Comments (13)

developit avatar developit commented on May 13, 2024 4

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>

from preact-router.

developit avatar developit commented on May 13, 2024 4

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);
});

from preact-router.

developit avatar developit commented on May 13, 2024 2

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.

from preact-router.

developit avatar developit commented on May 13, 2024 1

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.

from preact-router.

Vani-gurnani avatar Vani-gurnani commented on May 13, 2024

from preact-router.

Vani-gurnani avatar Vani-gurnani commented on May 13, 2024

from preact-router.

Vani-gurnani avatar Vani-gurnani commented on May 13, 2024

from preact-router.

MatteoWebDeveloper avatar MatteoWebDeveloper commented on May 13, 2024

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

from preact-router.

developit avatar developit commented on May 13, 2024

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.

from preact-router.

MatteoWebDeveloper avatar MatteoWebDeveloper commented on May 13, 2024

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

from preact-router.

prateekbh avatar prateekbh commented on May 13, 2024

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

from preact-router.

prateekbh avatar prateekbh commented on May 13, 2024

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

can we close the issue @developit

from preact-router.

developit avatar developit commented on May 13, 2024

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

from preact-router.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.