Check out a free preview of the full Advanced Elm course

The "SPAs Solution" Lesson is part of the full, Advanced Elm course featured in this preview video. Here's what you'd learn in this lesson:

Richard live-codes the solution to the SPAs exercise.

Preview
Close

Transcript from the "SPAs Solution" Lesson

[00:00:00]
>> Richard Feldman: So we have a couple of different URLs here. We're going to map them to various different routes. First one is /settings. Now, this is going to be one of those hard-coded URLs, so not much to it. It's just gonna be s, which is how we begin all of these.

[00:00:14]
It's gonna be settings, and then we're going to map that to Settings, the route. Same thing with register, same basic pattern here. Register and register the string. Article is going to be a little bit more complicated because it's got the slug associated with it. So that one's going to be map to Article, and then we have article followed by the slug and instead of username.URL parser, we're gonna have slug.url.parser.

[00:00:44]
That's essentially article.slug, and that follows sort of the same pattern of exposing only the decoder and a URL parser, and not the direct way to instantiate this type so that it stays opaque. So that's the slug for the article. Then we have the editor. I'm gonna try to remember is it, right, so there's new article and edit article is the name of those.

[00:01:09]
And I guess it's actually yeah, okay. So for the one where we have no argument here, we're gonna call that new article.
>> Richard Feldman: And so that's gonna be slash editor with no further arguments. And finally, we're gonna have,
>> Richard Feldman: EditArticle, which is actually going to have editor followed by a slug.

[00:01:41]
>> Richard Feldman: Okay, let's see how that looks. Great! Fantastic! One more thing to note as with all parsers, if you ever have a parser that covers the one below it. Bad news, it's never gonna hit the one below it. So you have to be careful with things like this, where if you have two of these in a row, it's a little bit of danger here with editor and editor/slug.

[00:02:06]
That if we had these in the wrong order, then maybe the top one might say I always match first if it's got a slash followed by nothing and then I could potentially mess this up. Now in this particular case, we're good. But it's worth noting that is something that can't happen whenever you're using oneOf.

[00:02:22]
The order definitely matters. It's going to try them in this order. Also that's an opportunity for an extreme micro-optimization that's almost certainly never going to make a difference in practice and performance. But theoretically, you might wanna put the most common ones at the top just so it has to traverse through less of the list.

[00:02:38]
But again, really, really unlikely to make any kind of performance difference in practice. Not something I would necessarily worry about unless you really want to and you're sure it's gonna solve a real problem.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now