Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - Force trailing slash #352

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,48 @@ import 'file?name=index.html!../example/index.html';
import 'react-toolbox/lib/commons.scss';
import Root from './root';
import registry from './lib/registry';
import { forceTrailingSlash } from './lib/pathHelper';
import './index.css';

if (process.env.NODE_ENV !== 'production') {
require('./utils.css'); // eslint-disable-line
}

// Create mount element dynamically
const el = document.createElement('div');
el.id = 'root';
document.body.appendChild(el);
const fixUrl = (pathname) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with your mention of placement - it would be good to put this somewhere route specific, and try to involve React Router if possible. I like this approach. We could then put the rewriting functions in a separate file under src/routing. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, will take a look at the react router solution you linked and see if I can do it that way

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gave this a go (see new commit) -- but unfortunately it didn't work - if you have a URL http://localhost/admin, http://localhost/admin/ or http://localhost/admin// the path is / as far React router is concerned.

window.location.href =
`${ window.location.protocol }//${ window.location.host }${ pathname }`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: we can shrink this down by first destructuring window.location.

+ `${ window.location.search }${ window.location.hash }`;
};

render((
<AppContainer>
<Root />
</AppContainer>
), el);
const init = () => {
// Create mount element dynamically
const el = document.createElement('div');
el.id = 'root';
document.body.appendChild(el);

if (process.env.NODE_ENV !== 'production' && module.hot) {
module.hot.accept('./root', () => {
const NextRoot = require('./root').default; // eslint-disable-line
render((
<AppContainer>
<NextRoot />
</AppContainer>
), el);
});
render((
<AppContainer>
<Root />
</AppContainer>
), el);

if (process.env.NODE_ENV !== 'production' && module.hot) {
module.hot.accept('./root', () => {
const NextRoot = require('./root').default; // eslint-disable-line
render((
<AppContainer>
<NextRoot />
</AppContainer>
), el);
});
}
};

const fixedPathname = forceTrailingSlash(window.location.pathname);
if (fixedPathname !== window.location.pathname) {
fixUrl(fixedPathname);
} else {
init();
}

const buildtInPlugins = [{
Expand Down
33 changes: 33 additions & 0 deletions src/lib/__tests__/pathHelper.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { forceTrailingSlash } from '../pathHelper';

describe('forceTrailingSlash', () => {
it('should NOT fix a good path', () => {
expect(
forceTrailingSlash('/admin/')
).toEqual(
'/admin/'
);
});

it('should add a trailing slash to a path without one', () => {
expect(
forceTrailingSlash('/admin')
).toEqual(
'/admin/'
);
});

it('should remove all trailing but one slashes from a path ending in multiple slashes', () => {
expect(
forceTrailingSlash('/admin//')
).toEqual(
'/admin/'
);

expect(
forceTrailingSlash('/admin///')
).toEqual(
'/admin/'
);
});
});
5 changes: 5 additions & 0 deletions src/lib/pathHelper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const absolutePath = new RegExp('^(?:[a-z]+:)?//', 'i');
const normalizePath = path => path.replace(/[\\\/]+/g, '/');

// Fix path to have a single trailing slash
export function forceTrailingSlash(basePath) {
return basePath.replace(/\/*$/, '/');
}

export function resolvePath(path, basePath) { // eslint-disable-line
// No path provided, skip
if (!path) return null;
Expand Down