kriasoft/universal-router

Make this work in Meteor server

antoninadert opened this issue · 3 comments

I tried to use it with meteor (on Windows 7 for now) but npm complained if I try to import the package from the server part of Meteor (server/main.js):

`W20170825-14:33:36.645(2)? (STDERR) packages\modules.js:2608\

W20170825-14:33:36.714(2)? (STDERR) constructor(routes, options = {}) {

W20170825-14:33:36.715(2)? (STDERR) -------------------^

W20170825-14:33:36.719(2)? (STDERR) SyntaxError: Unexpected token =

W20170825-14:33:36.721(2)? (STDERR) at Object.exports.runInThisContext (vm.js:53:16)

W20170825-14:33:36.723(2)? (STDERR) at
D:\MyApp.meteor\local\build\programs\server\boot.js:331:30
W20170825-14:33:36.727(2)? (STDERR) at Array.forEach (native)

W20170825-14:33:36.729(2)? (STDERR) at Function..each..forEach
(C:\Users\myUserName\AppData\Local.meteor\packages\meteor-tool\1.5.1\mt-os.windows.x86_32\dev_bundle\server-lib\node_modules\underscore\underscore.js:79:11)

W20170825-14:33:36.735(2)? (STDERR) at
D:\MyApp.meteor\local\build\programs\server\boot.js:158:5
W20170825-14:33:36.737(2)? (STDERR) at
D:\MyApp.meteor\local\build\programs\server\boot.js:387:5
W20170825-14:33:36.740(2)? (STDERR) at Function.run
(D:\MyApp.meteor\local\build\programs\server\profile.js:510:12)

W20170825-14:33:36.744(2)? (STDERR) at
D:\MyApp.meteor\local\build\programs\server\boot.js:386:11`

On Meteor > 1.5, we have a new package server-render for server side rendering. I was hopping it would work out of the box with universal router but it seems there is something to do to make them work together

If you're using the router with Node v5 and below, import it as follows:

import UniversalRouter from 'universal-router/legacy';

That actually works, many thanks :)
I now have new problem because the server-render package from meteor does not run correctly if I call the method in universal router's promise , but this is another story.

function renderLocation(thisSink) { router.resolve({ path: thisSink.request.url.path }).then(route => { thisSink.renderIntoElementById("app", renderToString( <Big /> )); }); is not rendering the Big component while it works outside of the promise.. I guess nothing to do with universal router at this point. You can close

Just to say that I made it all work by using the async from server-render's package sink object:

import { Meteor } from 'meteor/meteor';
import { renderToString } from "react-dom/server";
import { onPageLoad } from "meteor/server-render";
import UniversalRouter from 'universal-router/legacy';

import routes from '../both/routes'

const router = new UniversalRouter(routes);

function renderLocation(thisSink) {
  router.resolve({path: thisSink.request.url.path}).then(route  => {
    console.log(route.title);
    thisSink.renderIntoElementById("app", renderToString(
      route.component
    ));
  });
}

onPageLoad(async sink => {
  //generate new static HTML on every page request
  await renderLocation(sink);
});