loading...

Blank, and similarly named titles on DEV

calin92540842 profile image PDS OWNER CALIN (Calin Baenen) ・1 min read

Ok, so I just let out 8 new posts, all with these names and content:


[
    {
        "name": "zero width non joiner",
        "content": "zero width non joiner"
    },
    {
        "name": "zero width non joiner",
        "content": ""
    },
    {
        "name": "zero width joiner",
        "content": "zero width joiner"
    },
    {
        "name": "zero width joiner",
        "content": ""
    },
    {
        "name": "z",
        "content": "1"
    },
    {
        "name": "z",
        "content": "2"
    },
    {
        "name": "Some long ass title just to do a little bit of testing, don't worry about it.",
        "content": ""
    },
    {
        "name": "Some long ass title for testing and to see if content changes the url.",
        "content": "CHARA"
    }
]

And all of them have different URLs, as of now, I can not access the posts, as the text is literally impossible to click on. But my conclusion for the last 4 is it seems to be some kind of random identifier that's generated.

Now, while this could be a truly random identifier, I feel like it's linked to the post in some way.

Why else would it it be there, when the post is already uniquely identified with your dev handle (e.g. https://dev.to/calin92540842/is-there-a-synonym-singular-word-for-removing-emptiness-21em).

Of course, I could be overthinking it, and it has nothing to do with anything at all, though, there's a lot that we don't know happens bts, and honestly, we don't tent to care til it's brought up, which is why I'm bringing it up here.

Thanks for reading.
Cheers.

Posted on by:

calin92540842 profile

PDS OWNER CALIN (Calin Baenen)

@calin92540842

I am a 13 (as of Oct 30 of 2019) yr/o developer (I have been developing mini-projects for 4, years now, since I was 9), who makes projects in languages like: Java, HTML, Python 3, JS, CSS, and C#.

Discussion

markdown guide
 

You can check out the code for DEV and dig around if you want.

The code you're interested in is in app/models/article.rb, around line 606 at time of writing:

def title_to_slug
  title.to_s.downcase.parameterize.tr("_", "") + "-" + rand(100_000).to_s(26)
end

What does that do? We can use irb to find out (that's the oft-bundled Ruby REPL)

$ irb
irb(main):001:0> puts rand(100_000)
66075
irb(main):002:0> puts rand(100_000).to_s(26)
14lh
irb(main):003:0> puts rand(100_000).to_s(26)
2j7l

So you're right, it adds a random slug to the end of every URL.

Why else would it it be there, when the post is already uniquely identified with your dev handle (e.g. dev.to/calin92540842/is-there-a-sy...).

A post isn't uniquely identified by your handle and its title. Consider the scenario where you make several test posts from the API with the same name, or want to make a post called "Happy Friday" a year after you made a post called "Happy Friday". These would collide and cause some kind of rupture in the space-time continuum. Adding four random characters staves that off a bit.

It's not perfect, but it's going to keep you 99% safe if you make several posts called "test" as the same user, for example.

Another approach would have been for DEV to have taken the decision to prevent duplicate user/title combinations altogether. That's legit, too, and would result in slightly nicer-looking URLs.
Since it's random, there's always the possibility of collision - it's not very likely though!

 

Didn't DEV implement a way of making sure collision doesn't happen, like storing the IDs and making a new "slug" if the one made is already taken, similar to YouTube videos?

 

Not that I can see given a quick browse. You could do it other ways like adding an integer delta to duplicate posts so you get "test-0", "test-1" when you save duplicate posts called "test". That's how some CMSs work. Or you could make the slug entirely cosmetic and have something like posts/{post-id}/pretty-name-of-post.

It doesn't particularly matter one way or another, I guess. Nobody's typing these URLs in; they're informative in that you can see where they go given a link and that's all that counts.

Well, thanks for your responses to my inquiries. And you have a wonderful.
Just me, or does the fact that there isn't something to stop collision seems like an oversight?