Skip to content

Commit

Permalink
add basename support for SSR Links (#885)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bangsheng authored and jchip committed Jul 30, 2018
1 parent 839c838 commit 295b11d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class ReduxRouterEngine {

this.options.withIds = !!options.withIds;

this.options.basename = options.basename || "";

if (!options.stringifyPreloadedState) {
this.options.stringifyPreloadedState = state =>
`window.__PRELOADED_STATE__ = ${escapeBadChars(JSON.stringify(state))};`;
Expand Down Expand Up @@ -198,7 +200,7 @@ class ReduxRouterEngine {
{ store },
React.createElement(
StaticRouter,
{ location, context: routeContext },
{ location, context: routeContext, basename: this.options.basename },
this._routesComponent
)
)
Expand Down
14 changes: 13 additions & 1 deletion packages/electrode-redux-router-engine/test/routes.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import { connect } from "react-redux";
import { withRouter, Redirect } from "react-router-dom";
import { withRouter, Redirect, Link } from "react-router-dom";
import { renderRoutes } from "react-router-config";
import { renderFlatRoutes } from "./render-flat-routes";

Expand Down Expand Up @@ -31,6 +31,12 @@ class Test extends React.Component {
}
}

class TestBasename extends React.Component {
render() {
return <Link to="/to-target">Test</Link>;
}
}

class TestRedirect extends React.Component {
render() {
return (
Expand Down Expand Up @@ -132,6 +138,12 @@ const routes = [
exact: true,
component: Home
},
{
// Default, equivalent of RR3's IndexRoute
path: "/test/basename",
exact: true,
component: TestBasename
},
{
path: "/test/init",
exact: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,26 @@ describe("redux-router-engine", function() {
});
});

it("should render Link with no basename", () => {
const engine = new ReduxRouterEngine({ routes });
testReq.url = Url.parse("/test/basename");

return engine.render(testReq).then(result => {
expect(result.status).to.equal(200);
expect(result.html).to.equal("<div>Page<a href=\"/to-target\">Test</a></div>");
});
});

it("should render Link with basename", () => {
const engine = new ReduxRouterEngine({ routes, basename: "/my-base" });
testReq.url = Url.parse("/test/basename");

return engine.render(testReq).then(result => {
expect(result.status).to.equal(200);
expect(result.html).to.equal("<div>Page<a href=\"/my-base/to-target\">Test</a></div>");
});
});

it("should not populate react-id by default", () => {
const engine = new ReduxRouterEngine({ routes });
testReq.url = Url.parse("/test");
Expand Down

0 comments on commit 295b11d

Please sign in to comment.