How to use the universal-router function in universal-router

To help you get started, we’ve selected a few universal-router examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github malbernaz / preact-hn / src / client.js View on Github external
function insertCss(...styles) {
  const removeCss = styles.map(x => x._insertCss());

  return () => removeCss.forEach(f => f());
}

function redirect(to) {
  history.replace(to);
}

const context = { insertCss, store };

const mnt = document.querySelector("main");

const router = new Router(routes);

async function bootstrap(location, router) {
  if (FIRST_RENDER) {
    if (!_DEV_) await registerServiceWorker();

    if (!self.fetch) {
      await import("unfetch" /* webpackChunkName: "fetch-polyfill" */);
    }
  }

  CURRENT_LOCATION = location;

  const { pathname: path, search } = location;

  const route = await router.resolve({ path, store, redirect, search, ...routerMiddleware });
github goodmind / treact / packages / treact / src / client.tsx View on Github external
const render = async (location: Location, Routes: typeof routes) => {
  if (routes) currentRoutes = Routes
  const Router = new UniversalRouter(currentRoutes, {
    resolveRoute,
    context: { store },
  })
  const Route = await Router.resolve({
    pathname: location.pathname,
  })
  return renderComponent({Route})
}
github goodmind / treact / src / client.tsx View on Github external
const render = async (location: Location, Routes: typeof routes) => {
  if (routes)
    currentRoutes = Routes
  const Router = new UniversalRouter(currentRoutes, { resolveRoute, context: { store } })
  const Route = await Router.resolve({ pathname: location.pathname })
  return renderComponent({Route})
}
github charlessolar / eShopOnContainersDDD / src / Web / src / app / router.ts View on Github external
debug('isSetup', store.status.isSetup);
      if (!store.status.isSetup && !pathname.startsWith('/seed')) {
        return { redirect: '/seed' };
      }
    },
    children: [
      ...modules.auth.routes,
      ...modules.catalog.routes,
      ...modules.configuration.routes,
      ...modules.administrate.routes,
      ...modules.basket.routes,
      ...modules.orders.routes
    ]
  };

  return new Router(routes, {
    resolveRoute(routerContext: UniversalRouterContext, params: any) {
      const { route } = routerContext;

      if (typeof route.action === 'function') {
        const result = route.action(routerContext, params);
        if (result) {
          return result;
        }
      }

      if (typeof route.component === 'function') {
        return route.component(routerContext);
      }
    }
  });
}
github bykof / cordova-plugin-webserver / src / www / appserver / AppServer.js View on Github external
initRouter() {
    this.router = new UniversalRouter(this.routes);
  }
github antoninadert / proto-starter / client / router.js View on Github external
import UniversalRouter from 'universal-router';
import createHistory from 'history/createBrowserHistory'
import { hydrate } from 'react-dom';
import routes from '../both/routes'


const History = createHistory()
const location = History.location
const router = new UniversalRouter(routes);

function renderLocation(location) {
  return router.resolve({pathname: location.pathname}).then(route  => { 
    //route is what our action() returns for a specific path
    document.title = route.title;
    hydrate(route.component, document.getElementById("app"));
  });
}

//Initialize the first page render with current History
renderLocation(location);
//listen to URL(location) changes and render the new layout based on URL automatically
History.listen((anylocation) => {renderLocation(anylocation)});

export { History };
github lskjs / lskjs / src / Uapp / Uapp.js View on Github external
async run(props = {}) {
    await super.run();
    const context = this.provide();
    this.log.trace('router.context', Object.keys(context))
    this.routes = this.getRoutes();
    this.router = new UniversalRouter(this.routes, {
      context,
    });


    if (__CLIENT__ && !__DEV__) {
      setTimeout(() => this.checkVersion(), 120 * 1000);
    }
  }
github labzero / lunch / src / router.js View on Github external
export default routes => new Router(routes, {
  resolveRoute(context, params) {
    if (typeof context.route.load === 'function') {
      return context.route.load().then(action => action.default(context, params));
    }
    if (typeof context.route.action === 'function') {
      return context.route.action(context, params);
    }
    return null;
  },
});

universal-router

Isomorphic router for JavaScript web applications

MIT
Latest version published 1 month ago

Package Health Score

90 / 100
Full package analysis