Giter Club home page Giter Club logo

elm-hash-routing's Introduction

elm-hash-routing

A small wrapper around Browser.application that enables easy hash routing!

The Basics

Before using this package, make sure you have a good idea of how applications are structured in Elm. Here are some good resources:

If you are already up to speed on these, then skip to the next section!

Why Hash Routing?

In most cases, you should prefer not to use this package or use hash routing. It's sort of a hack that allows us to let the client manage routing of the application, since servers traditionally ignore the hash (or "fragment") in a Url.

However, sometimes you may not have control of the server or it's routing logic. A common example is a single page application on a static hosting service, like Github pages. In cases like this, you can use Browser.Hash.application to keep your Url parsing the same.

Usage

First, install the package:

elm install mthadley/elm-hash-routing

You should write your URL parsing logic as if your application was not using hash routing. Then, all you need to do is swap out your call to Browser.application with Browser.Hash.Application:

module Main exposing (main)

import App exposing (Model, Msg, init, subscriptions, update, view)
import Browser.Hash as Hash
import Router


main : Program () Model Msg
main =
    Hash.application
        { init = init
        , view = view
        , update = update
        , subscriptions = subscriptions
        , onUrlChange = App.RouteChange << Router.parse
        , onUrlRequest = App.OnUrlRequest
        }

Now the Url that your onUrlChange messages recieve will appear as if you are using server routing! The fragment is essentially moved to the path of the Url, with some extra processing to make sure everything is consistent.

You'll still need to be mindful of any internal links you create in your application. Make sure to prefix them with a #.

If you decide to migrate away from hash routing in the future, you just need to switch back to Browser.application, and your url parsing can remain the same.

Coming from Elm 0.18

This can be used to replace the behavior provided by parseHash in the evancz/url-parser package. It was removed in Elm 0.19, however we can easily replicate hash routing in this package with the tools provided in elm/browser and elm/url!

elm-hash-routing's People

Contributors

dependabot-preview[bot] avatar dependabot-support avatar dependabot[bot] avatar mthadley avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

nduatik

elm-hash-routing's Issues

Switch hash/path routing from Program Flag

It's great that this package is basically a drop-in.

But it could be used to dynamically switch routing between using hash or path with a little more integration effort.

I think the hard part is that Browser.application does not have access to flags at onUrlChange = config.onUrlChange << updateUrl.

If updateUrl was exposed, then it would be possible to use optionally depending on flag in init and the onUrlChange msg in update.

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.