How to use the universal-router.resolve 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 DefinitelyTyped / DefinitelyTyped / types / universal-router / universal-router-tests.ts View on Github external
action: (context: ActionContext) => `Welcome, ${context.params["username"]}!`
  }
];

resolve(routes2, { path: "/hello/john" })
  .then(result => console.log(result));

// Test 3
const routes3 = [
  {
    path: "/hello/:username",
    action: (ctx: ActionContext, { username }: Params) => `Welcome, ${username}!`
  }
];

resolve(routes3, { path: "/hello/john" })
  .then(result => console.log(result));

// Test 4
const routes4 = [
  {
    path: "/hello",
    action: () => new Promise(resolve => {
      setTimeout(() => resolve("Welcome!"), 1000);
    })
  }
];

resolve(routes4, { path: "/hello" })
  .then(result => console.log(result));

// Test 5
github openforis / collect / collect-web / collect-webapp / frontend / server.js View on Github external
app.get('*', async (req, res, next) => {
  try {
    let css = new Set();
    let statusCode = 200;
    const data = { title: '', description: '', style: '', script: assets.main.js, children: '' };

    await UniversalRouter.resolve(routes, {
      path: req.path,
      query: req.query,
      context: {
        insertCss: (...styles) => {
          styles.forEach(style => css.add(style._getCss())); // eslint-disable-line no-underscore-dangle, max-len
        },
        setTitle: value => (data.title = value),
        setMeta: (key, value) => (data[key] = value),
      },
      render(component, status = 200) {
        // console.log('inside render of UniversalRouter', component);
        css = new Set();
        statusCode = status;
        data.children = ReactDOM.renderToString(component);
        data.style = [...css].join('');
        return true;
github phylogeny-explorer / explorer / client / src / client.js View on Github external
function onLocationChange(location) {
    // Save the page scroll position into the current location's state
    if (currentLocation.key) {
      saveState(currentLocation.key, {
        ...readState(currentLocation.key),
        scrollX: windowScrollX(),
        scrollY: windowScrollY(),
      });
    }
    currentLocation = location;
    UniversalRouter.resolve(routes, {
      path: location.pathname,
      query: location.query,
      state: location.state,
      context,
      render: render.bind(undefined, container, location), // eslint-disable-line react/jsx-no-bind, max-len
    }).catch(err => console.error(err)); // eslint-disable-line no-console
  }
github start-react / sb-admin-seed-react / src / server.js View on Github external
app.get('*', async (req, res, next) => {
  try {
    let css = new Set();
    let statusCode = 200;
    const data = { title: '', description: '', style: '', script: assets.main.js, children: '' };

    await UniversalRouter.resolve(routes, {
      path: req.path,
      query: req.query,
      context: {
        insertCss: (...styles) => {
          styles.forEach(style => css.add(style._getCss())); // eslint-disable-line no-underscore-dangle, max-len
        },
        setTitle: value => (data.title = value),
        setMeta: (key, value) => (data[key] = value),
      },
      render(component, status = 200) {
        // console.log('inside render of UniversalRouter', component);
        css = new Set();
        statusCode = status;
        data.children = ReactDOM.renderToString(component);
        data.style = [...css].join('');
        return true;
github jsmentor / react-workshops / apps / user-management-app / src / client.js View on Github external
// Remember the latest scroll position for the previous location
  scrollPositionsHistory[currentLocation.key] = {
    scrollX: window.pageXOffset,
    scrollY: window.pageYOffset,
  };
  // Delete stored scroll position for next page if any
  if (action === 'PUSH') {
    delete scrollPositionsHistory[location.key];
  }
  currentLocation = location;

  try {
    // Traverses the list of routes in the order they are defined until
    // it finds the first route that matches provided URL path string
    // and whose action method returns anything other than `undefined`.
    const route = await UniversalRouter.resolve(routes, {
      ...context,
      path: location.pathname,
      query: queryString.parse(location.search),
    });

    // Prevent multiple page renders during the routing process
    if (currentLocation.key !== location.key) {
      return;
    }

    if (route.redirect) {
      history.replace(route.redirect);
      return;
    }

    appInstance = ReactDOM.render(
github malbernaz / preact-isomorphic-example / src / client.js View on Github external
}

    if (!self.fetch) {
      await require.ensure([], require =>
        require('isomorphic-fetch'), 'fetch-polyfill'
      )
    }
  }

  CURRENT_LOCATION = location

  const router = require('./routes').default // eslint-disable-line global-require

  const { pathname } = location

  const route = await match(router, { path: pathname, currentRoute: pathname, ...routerMiddleware })

  const component = (
    
      { route.component }
    
  )

  render(component, mnt, mnt.lastElementChild)

  if (FIRST_RENDER) {
    const node = document.getElementById('css')

    if (node) node.parentNode.removeChild(node)

    FIRST_RENDER = false
  }
github phodal / mole / src / server.js View on Github external
app.get('*', async(req, res, next) => {
  try {
    let css = new Set();
    let statusCode = 200;
    const data = { title: '', description: '', style: '', script: assets.main.js, children: '' };

    await UniversalRouter.resolve(routes, {
      path: req.path,
      query: req.query,
      context: {
        insertCss: (...styles) => {
          styles.forEach(style => css.add(style._getCss())); // eslint-disable-line no-underscore-dangle, max-len
        },
        setTitle: value => (data.title = value),
        setMeta: (key, value) => (data[key] = value),
      },
      render(component, status = 200) {
        css = new Set();
        statusCode = status;
        data.children = ReactDOM.renderToString(component);
        data.style = [...css].join('');
        return true;
      },
github fechy / retropie-web-gui / src / client.js View on Github external
// Remember the latest scroll position for the previous location
  scrollPositionsHistory[currentLocation.key] = {
    scrollX: window.pageXOffset,
    scrollY: window.pageYOffset,
  };
  // Delete stored scroll position for next page if any
  if (history.action === 'PUSH') {
    delete scrollPositionsHistory[location.key];
  }
  currentLocation = location;

  try {
    // Traverses the list of routes in the order they are defined until
    // it finds the first route that matches provided URL path string
    // and whose action method returns anything other than `undefined`.
    const route = await UniversalRouter.resolve(routes, {
      ...context,
      path: location.pathname,
      query: queryString.parse(location.search),
    });

    // Prevent multiple page renders during the routing process
    if (currentLocation.key !== location.key) {
      return;
    }

    if (route.redirect) {
      history.replace(route.redirect);
      return;
    }

    ReactDOM.render(
github DefinitelyTyped / DefinitelyTyped / types / universal-router / universal-router-tests.ts View on Github external
interface Render {
  render: typeof render;
}

const routes7 = [
  { path: "/one", action: ({ render: func }: ActionContext &amp; Render) =&gt; func("<h1>Page One</h1>") },
  { path: "/two", action: ({ render: func }: ActionContext &amp; Render) =&gt; func("<h1>Page Two</h1>") }
];

function render(component: string) {
  return new Promise(resolve =&gt; {
    console.log(`Rendering... ${component}`);
  });
}

resolve&gt;(routes7, { path: "/one", render });
github lskjs / lskjs / src / Uapp / _ / ReactApp.common.js View on Github external
async getHtmlProps(req) {
    const reqProps = await this.getReqProps(req);
    const route = await UniversalRouter.resolve(this.getUniversalRoutes(), reqProps);
    return {
      ...reqProps,
      ...route,
      route,
      children: route.component,
    };
  }
}

universal-router

Isomorphic router for JavaScript web applications

MIT
Latest version published 1 year ago

Package Health Score

63 / 100
Full package analysis