Fork me on GitHub
#clojurescript
<
2024-05-07
>
DrLjótsson12:05:09

I'm using goog.history.Html5History and am confused by the method names .getToken, .setToken, and .replaceToken. Why "token"? The methods seem to return and set the current href but why are they not then called something with href or URL or similar?

p-himik12:05:08

It might be something other than the URL since there's a way to convert between tokens and URLs which you can adjust by using setPathPrefix or specifying a transformer. Note also that the query isn't a part of the token as well. All follows from the implementation of getUrl_:

goog.history.Html5History.prototype.getUrl_ = function(token) {
  'use strict';
  if (this.useFragment_) {
    return '#' + token;
  } else {
    return this.transformer_ ?
        this.transformer_.createUrl(
            token, this.pathPrefix_, this.window_.location) :
        this.pathPrefix_ + token + this.window_.location.search;
  }
}

🙏 1
DrLjótsson13:05:20

Ok. So the token may be some internal representation of a browsing target?

Kelvin14:05:15

So I’ve been looking into the pros and cons of different routing libraries, reading articles like this one: https://ericnormand.me/mini-guide/clojure-routers

Kelvin14:05:37

And this sentence stood out to me: > If you’re doing both frontend and backend, you want to be able to share the routes between them. Having the same code work on the server and the client might be useful. Why would a web app want to share routes between BE and FE if everything’s a SPA, or at least doesn’t share a 1-on-1 correspondence between FE browser URLs and BE REST endpoints, these days (and haven’t for over a quarter century)?

valerauko14:05:44

So that you don't have to define your API endpoints both sides. Define it one place, implement it on the server-side, use the routes for (API call) URL generation on the frontend

valerauko14:05:16

Also works if the server for example sends emails that should contain the user-facing UI URLs

Kelvin14:05:15

I suppose that would be useful for some contexts, just not every single context (e.g. if your app uses GraphQL and is served using a single endpoint for the BE)

DrLjótsson18:05:38

Has worked great for me. Reitit API routes are in a .cljc file and the front end does requests to route names instead of URL strings.

1