Coder Social home page Coder Social logo

Comments (4)

mjackson avatar mjackson commented on April 20, 2024

Something like this could work:

function useBasename(createHistory) {
  return function (options={}) {
    let { basename, ...historyOptions } = options
    let history = createHistory(historyOptions)

    function listen(listener) {
      return history.listen(function (location) {
        // When new locations are emitted, remove the basename
        // from the beginning of the pathname.
        if (location.pathname.indexOf(basename) === 0)
          location.pathname = location.pathname.replace(basename, '')

        listener(location)
      })
    }

    // Override all navigation functions with basename-aware versions.
    function pushState(state, path) {
      history.pushState(state, createPath(path))
    }

    function replaceState(state, path) {
      history.replaceState(state, createPath(path))
    }

    function createPath(path) {
      return basename + path
    }

    function createHref(path) {
      return history.createHref(createPath(path))
    }

    return {
      ...history,
      listen,
      pushState,
      replaceState,
      createPath,
      createHref
    }
  }
}

let history = useBasename(createHistory)({
  basename: '/myapp/section'
})

We should probably write some tests for it though :P

from history.

jamischarles avatar jamischarles commented on April 20, 2024

Awesome. Thx @mjackson. I'll take a stab at this on monday.

from history.

rtelicak avatar rtelicak commented on April 20, 2024

Hi,

I tried that snippet, but didn't work. I think you forgot to return listen function as well, like

...

return {
  ...history,
  pushState,
  replaceState,
  createPath,
  createHref,
  listen
}

Thank you.

from history.

mjackson avatar mjackson commented on April 20, 2024

Ah, yes @rtelicak. I forgot to add listen. I'll update my comment.

from history.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.