Skip to content

Commit

Permalink
Fix React Router history bug where basename is not applied to initial…
Browse files Browse the repository at this point in the history
… server render

In the React Router 5 implementation, when the server history object was created, the prefix was prepended to the current URL: https://github.com/uber/fusionjs/pull/1409/files#diff-b9d2e3755ea5d8daffc083230c68bfcea742135f9fca8a8aa7f54d13467c6017L103

This behavior was incorrectly removed in the RR6 implementation. The history location should always have the basename/prefix as part of the URL since that's what React Router expects.

Also include a small fix to fusion-plugin-apollo where if we are in dev mode, to log out SSR errors the way we do in fusion-react. This is so the errors show up in dev.

Co-authored-by: shYkiSto <siarhei@uber.com>
  • Loading branch information
2 people authored and fusionjs-sync-bot[bot] committed Oct 13, 2022
1 parent 0488d66 commit 482157a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions fusion-plugin-apollo/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export default (
getDataFromTree?: typeof defaultGetDataFromTree
) => {
return (getDataFromTree || defaultGetDataFromTree)(root).catch((e) => {
if (__DEV__) {
console.error('SSR Failed with Error', e);
}
logger && logger.error('SSR Failed with Error', e);
});
};
17 changes: 17 additions & 0 deletions fusion-plugin-react-router/__tests__/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,23 @@ if (__NODE__) {
expect(ctx.response.status).toBe(200);
cleanup();
});

test('handles basename missing from url by prefixing it internally', async () => {
const Hello = () => <div>Hello</div>;
const element = (
<div>
<Routes>
<Route path="/hello" element={<Hello />} />
</Routes>
</div>
);

const app = getPrefixApp(element);
const simulator = setup(app);
const ctx = await simulator.render('/hello');
expect(ctx.rendered.includes('Hello')).toBeTruthy();
cleanup();
});
}

test('events with trackingId', async () => {
Expand Down
2 changes: 1 addition & 1 deletion fusion-plugin-react-router/src/modules/ServerHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function createServerHistory(
}
const history = {
action: 'POP',
location: createLocation(location, basename, false),
location: createLocation(location, basename, true),
go: staticHandler('go'),
push,
replace,
Expand Down

0 comments on commit 482157a

Please sign in to comment.