Create a location object - particularly useful for server-side rendering
locationDefaults)
:
A location as defined by rackt/history with sane defaults
location
:
a complete location object as defined by rackt/history
ServerHistory
// server-side
import express from 'express'
import {run} from '@cycle/core'
import {
makeHistoryDriver,
createServerHistory,
createLocation
} from '@cycle/history'
import {makeHTMLDriver} from '@cycle/dom'
const app = express()
app.use((res, req) => {
...
const history = createServerHistory()
const {sources} = run(main, {
history: makeHistoryDriver(history),
html: makeHTMLDriver(),
})
history.push(createLocation(req.url))
sources.html.subscribe(html => res.end(html))
...
})
app.listen(3000)
The history driver used by run()
The output returned from your main()
function. This can be a URL string, any valid Location object, or an object
with a type key to execute a particular history function like goBack(),
goForward(). When using type: 'go' a value key is expected to have an
integer with how many locations to go back or forward.
Observable
.<
location
>
:
An Observable containing the current location you have navigated to.
return { history: Observable.just({type: 'go', value: -2}) }
return { history: Observable.just('/some/path')} }
return { history: Observable.just({pathname: '/some/path', state: {some: 'state'}}) }
Default parameters for createLocation; Same structure used by rackt/history
Instantiates an new history driver function using a valid history object.
a valid history instance as defined by
ractk/history. Should have createLocation(), createHref(), listen(),
and push() methods.
options object - currently accepts a boolean for
the parameter capture. capture will automatically capture link clicks.
historyDriver
:
The history driver function
import {run} from '@cycle/core'
import {makeHistoryDriver} from '@cycle/history'
import {useQueries, createHashHistory} form 'history'
function main(sources) {...}
const history = createHashHistory()
run(main, {
history: makeHistoryDriver(history),
})
a way to listen for location changes (used internally)
a way to push new locations to listeners. Can be used to push url changes on the server-side.
a way to replace the current location - effectively the same as push() in this instance. (used internally)
creates an HREF (used internally)
Function that returns whether or not the current environment supports the HistoryAPI
Boolean
:
Returns true if the current environment supports the History API; false if it does not.
import {run} from '@cycle/core'
import {makeHistoryDriver, supportsHistory} from '@cycle/history'
import {createHashHistory, createHistory} from 'history'
function main(sources) {...}
const history = supportsHistory() ?
createHistory() : createHashHistory()
run(main, {
history: makeHistoryDriver(history)
})