Skip to content

Commit

Permalink
Fix: Encode sublinks in URLs to fix broken routes (#1389)
Browse files Browse the repository at this point in the history
Follow up of #1332. This appears to have fixed the top level section link, but any sub-links within that section are still unencoded and result in "Page not found".
  • Loading branch information
mendrew authored and sapegin committed Jun 24, 2019
1 parent 5b9b517 commit 44b640b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/client/utils/__tests__/getUrl.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@ describe('getUrl', () => {
expect(result).toBe('/styleguide/#/Documentation/%40foo%2Fcomponents');
});

it('should return a route path with encoded name if sections (hashPath) has inappropriate symbols', () => {
expect(
getUrl({ name: '@foo/components', slug, hashPath: ['@foo/bar-documentation'] }, loc)
).toBe('/styleguide/#/%40foo%2Fbar-documentation/%40foo%2Fcomponents');

expect(
getUrl(
{
name: '@foo/components',
slug,
hashPath: ['@foo/bar-documentation', '@foo/bar-activations-section'],
},
loc
)
).toBe(
'/styleguide/#/%40foo%2Fbar-documentation/%40foo%2Fbar-activations-section/%40foo%2Fcomponents'
);
});

it('should return a route path with a param id=foobar', () => {
const result = getUrl({ name, slug, hashPath: ['Documentation'], id: true }, loc);
expect(result).toBe('/styleguide/#/Documentation?id=foobar');
Expand Down
5 changes: 3 additions & 2 deletions src/client/utils/getUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ export default function getUrl(
}

if (hashPath) {
let encodedHashPath = hashPath.map(encodeURIComponent);
if (!id) {
hashPath = [...hashPath, encodedName];
encodedHashPath = [...encodedHashPath, encodedName];
}
url += `#/${hashPath.join('/')}`;
url += `#/${encodedHashPath.join('/')}`;
}

if (id) {
Expand Down

0 comments on commit 44b640b

Please sign in to comment.