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

Make sure hash variable could be changed #85

Merged
merged 4 commits into from
Mar 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ Via npm with `npm install navigo` or drop `lib/navigo.min.js` into your page.

```js
var root = null;
var useHash = false;
var router = new Navigo(root, useHash);
var useHash = true; // Defaults to: false
var hash = '#!'; // Defaults to: '#'
var router = new Navigo(root, useHash, hash);
```

The constructor of the library accepts two argument - `root` and `useHash`. The first one is the main URL of your application. If you call the constructor without parameters then Navigo figures out the root URL based on your routes.
The constructor of the library accepts three argument - `root`, `useHash` and `hash`. The first one is the main URL of
your application. If you call the constructor without parameters then Navigo figures out the root URL based on your routes.

If `useHash` set to `true` then the router uses an old routing approach with hash in the URL. Navigo anyways falls back to this mode if there is no History API supported.
If `useHash` set to `true` then the router uses an old routing approach with hash in the URL. Navigo anyways falls back
to this mode if there is no History API supported. The `hash` parameter allows you to configure the hash character. To
make your URLs crawlable by Google you should use use '#!'. Read more at [developers.google.com](https://developers.google.com/webmasters/ajax-crawling/docs/learn-more).

### Adding a route

Expand Down
26 changes: 16 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,16 @@ function isHashChangeAPIAvailable() {
);
}

function extractGETParameters(url, useHash) {
function extractGETParameters(url, useHash, hash) {
var [ onlyURL, ...query ] = url.split(/\?(.*)?$/);

if (typeof hash === 'undefined') {
// To preserve BC
hash = '#';
}

if (!useHash) {
onlyURL = onlyURL.split('#')[0];
onlyURL = onlyURL.split(hash)[0];
}
return { onlyURL, GETParameters: query.join('') };
}
Expand All @@ -125,10 +130,11 @@ function manageHooks(handler, route) {
handler();
};

function Navigo(r, useHash) {
function Navigo(r, useHash, hash) {
this.root = null;
this._routes = [];
this._useHash = useHash;
this._hash = typeof hash === 'undefined' ? '#' : hash;
this._paused = false;
this._destroyed = false;
this._lastRouteResolved = null;
Expand All @@ -137,9 +143,9 @@ function Navigo(r, useHash) {
this._usePushState = !useHash && isPushStateAvailable();

if (r) {
this.root = r.replace(/\/$/, '/#');
this.root = r.replace(/\/$/, '/' + this._hash);
} else if (useHash) {
this.root = this._cLoc().split('#')[0].replace(/\/$/, '/#');
this.root = this._cLoc().split(this._hash)[0].replace(/\/$/, '/' + this._hash);
}

this._listen();
Expand All @@ -162,7 +168,7 @@ Navigo.prototype = {
history[this._paused ? 'replaceState' : 'pushState']({}, '', to);
this.resolve();
} else if (typeof window !== 'undefined') {
window.location.href = window.location.href.replace(/#(.*)$/, '') + '#' + path;
window.location.href = window.location.href.replace(new RegExp('(?:' + this._hash + ')+(.*)$'), '') + path;
}
return this;
},
Expand Down Expand Up @@ -193,10 +199,10 @@ Navigo.prototype = {
var url = (current || this._cLoc()).replace(this._getRoot(), '');

if (this._useHash) {
url = url.replace(/^\/#/, '/');
url = url.replace(new RegExp('/^\/' + this._hash + '/'), '/');
}

let { onlyURL, GETParameters } = extractGETParameters(url, this._useHash);
let { onlyURL, GETParameters } = extractGETParameters(url, this._useHash, this._hash);

if (
this._paused ||
Expand All @@ -218,7 +224,7 @@ Navigo.prototype = {
handler(m.params, GETParameters);
}, m.route);
return m;
} else if (this._defaultHandler && (onlyURL === '' || onlyURL === '/' || onlyURL === '#')) {
} else if (this._defaultHandler && (onlyURL === '' || onlyURL === '/' || onlyURL === this._hash)) {
manageHooks(() => {
this._lastRouteResolved = { url: onlyURL, query: GETParameters };
this._defaultHandler.handler(GETParameters);
Expand Down Expand Up @@ -270,7 +276,7 @@ Navigo.prototype = {
return result;
}, '');

return this._useHash ? '#' + result : result;
return this._useHash ? this._hash + result : result;
},
link: function (path) {
return this._getRoot() + path;
Expand Down