Skip to content

Commit

Permalink
Automatically use <base href> in useBasename
Browse files Browse the repository at this point in the history
Fixes #94
  • Loading branch information
mjackson committed Oct 28, 2015
1 parent df65c02 commit a9759df
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
## HEAD

- `useBasename` transparently handles trailing slashes (see [#108])
- `useBasename` automatically uses the value of <base href> when no
`basename` option is provided (see [#94])

[#108]: https://github.com/rackt/history/pull/108
[#94]: https://github.com/rackt/history/issues/94

## [v1.12.6]
> Oct 25, 2015
Expand Down
4 changes: 4 additions & 0 deletions docs/BasenameSupport.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ Basename-enhanced histories also automatically prepend the basename to paths use
history.createPath('/the/path') // /base/the/path
history.pushState(null, '/the/path') // push /base/the/path
```

### Using <base href>

In HTML documents, you can use the [`<base>` element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base)'s `href` attribute to specify a `basename` for the page. This way, you don't have to manually pass the `basename` property to your `createHistory` function.
10 changes: 10 additions & 0 deletions modules/useBasename.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { canUseDOM } from './ExecutionEnvironment'
import runTransitionHook from './runTransitionHook'
import parsePath from './parsePath'

Expand All @@ -6,6 +7,15 @@ function useBasename(createHistory) {
let { basename, ...historyOptions } = options
let history = createHistory(historyOptions)

// Automatically use the value of <base href> in HTML
// documents as basename if it's not explicitly given.
if (basename == null && canUseDOM) {
let base = document.getElementsByTagName('base')[0]

if (base)
basename = base.href
}

function addBasename(location) {
if (basename && location.basename == null) {
if (location.pathname.indexOf(basename) === 0) {
Expand Down

0 comments on commit a9759df

Please sign in to comment.