Skip to content

Commit

Permalink
Does not erase locale from the URL
Browse files Browse the repository at this point in the history
  • Loading branch information
favorit committed Mar 16, 2018
1 parent 859eaec commit 42b07a3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
5 changes: 4 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ var Routes = function () {
parsedUrl = _match2.parsedUrl;

if (route) {
req.locale = route.locale;
req.nextRoute = route.nextRoute;

if (customHandler) {
customHandler({ req: req, res: res, route: route, query: query });
} else {
Expand Down Expand Up @@ -213,7 +216,7 @@ var Routes = function () {
return _react2.default.createElement(Link, (0, _extends3.default)({}, newProps, {
__source: {
fileName: _jsxFileName,
lineNumber: 124
lineNumber: 127
}
}));
};
Expand Down
7 changes: 0 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,6 @@ class Route {
}

match (path) {
if (path.substring(1, this.locale.length + 1) === this.locale) {
path = path.substring(this.locale.length + 1)

if (!path) {
return {}
}
}
const values = this.regex.exec(path)
if (values) {
return this.valuesToParams(values.slice(1))
Expand Down
14 changes: 7 additions & 7 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ describe('Routes', () => {

test('match and merge params into query', () => {
const routes = nextRoutes().add('a', 'en').add('b', 'en', '/b/:b').add('c', 'en')
expect(routes.match('/en/b/b?b=x&c=c').query).toMatchObject({b: 'b', c: 'c'})
expect(routes.match('/b/b?b=x&c=c').query).toMatchObject({b: 'b', c: 'c'})
})

test('match homepage route', () => {
const {routes, route} = setupRoute('homepage', 'en')
expect(routes.match('/en').route).toMatchObject(route)
expect(routes.match('/').route).toMatchObject(route)
})

test('generate urls from params', () => {
Expand Down Expand Up @@ -101,8 +101,8 @@ describe('Request handler', () => {
}

test('find route and call render', () => {
const {routes, app, req, res} = setup('/en/a')
const {route, query} = routes.add('a', 'en').match('/en/a')
const {routes, app, req, res} = setup('/en-gb/a')
const {route, query} = routes.add('test', 'en-gb', '/en-gb/a').match('/en-gb/a')
routes.getRequestHandler(app)(req, res)
expect(app.render).toBeCalledWith(req, res, route.page, query)
})
Expand All @@ -112,14 +112,14 @@ describe('Request handler', () => {
const app = {getRequestHandler: jest.fn(), render: jest.fn()}
const req = {url: '/cs/test'}

routes.add('test', 'cs', '/test')
routes.add('test', 'cs', '/cs/test')
routes.getRequestHandler(app)(req, {})
expect(req.locale).toEqual('cs')
})

test('find route and call custom handler', () => {
const {routes, app, req, res} = setup('/en/a')
const {route, query} = routes.add('a', 'en').match('/en/a')
const {routes, app, req, res} = setup('/en-us/a')
const {route, query} = routes.add('a', 'en-us', '/en-us/a').match('/en-us/a')
const customHandler = jest.fn()
const expected = expect.objectContaining({req, res, route, query})
routes.getRequestHandler(app, customHandler)(req, res)
Expand Down

0 comments on commit 42b07a3

Please sign in to comment.