createLocation([location])

src/util.js

Create a location object - particularly useful for server-side rendering

Parameters

  • location = location (default locationDefaults) :

    A location as defined by rackt/history with sane defaults

Returns

location :

a complete location object as defined by rackt/history

createServerHistory

src/serverHistory.js

Returns

ServerHistory

Examples

// 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)

historyDriver(sink$)

src/makeHistoryDriver.js

The history driver used by run()

Parameters

  • Observable .< location > sink$ :

    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.

Returns

Observable .< location > :

An Observable containing the current location you have navigated to.

Examples

return { history: Observable.just({type: 'go', value: -2}) }
return { history: Observable.just('/some/path')} }
return { history: Observable.just({pathname: '/some/path', state: {some: 'state'}}) }

location

src/util.js

Default parameters for createLocation; Same structure used by rackt/history

Properties

makeHistoryDriver(history, options)

src/makeHistoryDriver.js

Instantiates an new history driver function using a valid history object.

Parameters

  • object history :

    a valid history instance as defined by ractk/history. Should have createLocation(), createHref(), listen(), and push() methods.

  • Object options :

    options object - currently accepts a boolean for the parameter capture. capture will automatically capture link clicks.

Returns

historyDriver :

The history driver function

Examples

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),
})

ServerHistory

src/serverHistory.js

Properties

  • function listen :

    a way to listen for location changes (used internally)

  • function push :

    a way to push new locations to listeners. Can be used to push url changes on the server-side.

  • function replace :

    a way to replace the current location - effectively the same as push() in this instance. (used internally)

  • function createHref :

    creates an HREF (used internally)

  • createLocation createLocation

supportsHistory

src/util.js

Function that returns whether or not the current environment supports the HistoryAPI

Returns

Boolean :

Returns true if the current environment supports the History API; false if it does not.

Examples

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)
})