Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Commit

Permalink
Use RoutePrefixToken if registered
Browse files Browse the repository at this point in the history
  • Loading branch information
rtsao committed Sep 24, 2018
1 parent bee9c9d commit b2e08ea
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"react-router-dom": "^4.3.1"
},
"peerDependencies": {
"fusion-core": "^1.7.0-alpha.2",
"fusion-core": "^1.7.0-alpha.3",
"react": "14.x - 16.x",
"react-dom": "14.x - 16.x"
},
Expand All @@ -44,7 +44,7 @@
"eslint-plugin-prettier": "2.6.2",
"eslint-plugin-react": "7.11.0",
"flow-bin": "0.78.0",
"fusion-core": "^1.7.0-alpha.2",
"fusion-core": "^1.7.0-alpha.3",
"fusion-plugin-universal-events": "^1.0.5",
"fusion-react": "^1.1.0",
"fusion-test-utils": "^1.2.2",
Expand Down
10 changes: 2 additions & 8 deletions src/__tests__/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import React from 'react';
import {UniversalEventsToken} from 'fusion-plugin-universal-events';
import {createPlugin} from 'fusion-core';
import {createPlugin, RoutePrefixToken} from 'fusion-core';
import App from 'fusion-react';
import {getSimulator} from 'fusion-test-utils';
import {withRouter, Link} from 'react-router-dom';
Expand All @@ -17,12 +17,6 @@ import {Route} from '../modules/Route';
import {Redirect} from '../modules/Redirect.js';
import RouterPlugin, {RouterProviderToken, RouterToken} from '../plugin';

const addRoutePrefix = (ctx, next) => {
// hack until we have better route prefix support in fusion-test-utils
ctx.prefix = '/test';
return next();
};

function getApp(el) {
const app = new App(el);
app.register(RouterToken, RouterPlugin);
Expand All @@ -31,7 +25,7 @@ function getApp(el) {

function getPrefixApp(el) {
const app = new App(el);
app.middleware(addRoutePrefix);
app.register(RoutePrefixToken, '/test');
app.register(RouterPlugin);
return app;
}
Expand Down
25 changes: 20 additions & 5 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@

import * as React from 'react';
import {UniversalEventsToken} from 'fusion-plugin-universal-events';
import {createPlugin, createToken, html, unescape, memoize} from 'fusion-core';
import {
createPlugin,
createToken,
html,
unescape,
memoize,
RoutePrefixToken,
} from 'fusion-core';
import {Router as ServerRouter} from './server';
import {Router as BrowserRouter} from './browser';
import {Router as DefaultProvider} from 'react-router-dom';
Expand Down Expand Up @@ -41,10 +48,18 @@ export default createPlugin({
deps: {
emitter: UniversalEventsToken.optional,
Provider: RouterProviderToken.optional,
prefix: RoutePrefixToken.optional,
},
middleware: ({emitter, Provider = DefaultProvider}, self) => {
middleware: ({emitter, Provider = DefaultProvider, prefix}, self) => {
return async (ctx, next) => {
const prefix = ctx.prefix || '';
if (!prefix) {
if (ctx.prefix) {
prefix = ctx.prefix;
} else {
prefix = '';
}
}

if (!ctx.element) {
return next();
}
Expand Down Expand Up @@ -125,13 +140,13 @@ export default createPlugin({
return payload;
});
// Expose the history object
const history = createBrowserHistory({basename: ctx.prefix});
const history = createBrowserHistory({basename: prefix});
myAPI.history = history;
ctx.element = (
<Router
history={history}
Provider={Provider}
basename={ctx.prefix}
basename={prefix}
onRoute={payload => {
pageData = payload;
emitter && emitter.emit('pageview:browser', payload);
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2557,9 +2557,9 @@ functional-red-black-tree@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327"

fusion-core@^1.7.0-alpha.2:
version "1.7.0-alpha.2"
resolved "https://registry.yarnpkg.com/fusion-core/-/fusion-core-1.7.0-alpha.2.tgz#f2279a57b93d4e19b468d106402accd1bafdef9f"
fusion-core@^1.7.0-alpha.3:
version "1.7.0-alpha.3"
resolved "https://registry.yarnpkg.com/fusion-core/-/fusion-core-1.7.0-alpha.3.tgz#dfcb57dc2c3e4daa06d4714d729a6d2fbd725945"
dependencies:
koa "^2.4.1"
koa-compose "^4.0.0"
Expand Down

0 comments on commit b2e08ea

Please sign in to comment.