Be Aware: This Could Affect Your Page Speed In Laravel

Infinitypaul
3 min readJan 22, 2025

--

Photo by Stephen Phillips - Hostreviews.co.uk on Unsplash

So, I was working on a feature the other day, and one of the things this page had to do was validate an email address and just like we all do, right? In Laravel, it’s super simple: you just add 'email' to your form request validation rules. Done. Laravel handles everything behind the scenes to ensure the email looks valid.

But then I thought, “Why not go the extra mile?” I added 'email:dns' to the validation rules. If you’ve used this or seen it before, you know it checks to make sure the email’s domain has a valid MX record, basically verifying that the email can actually receive messages. This little feature comes from the egulias/email-validator package.

Here’s where it gets interesting.

Can that one small addition have such a big impact on your page response time? Yes, It can. And that’s why I’m writing this to give you a heads-up so you can avoid getting blindsided by this.

With 'email:dns' added, every time someone submits the form, Laravel takes the email, looks at the domain (the part after the "@" symbol), and then reaches out to DNS servers to confirm that the domain exists and can handle emails. Sounds fine, right?

The problem is that this process depends on a third party, the DNS server. You have no control over how fast or slow it responds. Sometimes, it works fine and the page loads quickly. But other times, it gets stuck waiting, and your users are left staring at a loading spinner. Worst case? It times out or fails, causing an error.

And here’s the kicker: because this happens during form validation, you can’t offload it to a queue or delay it. It’s happening right there, in real-time, while your user waits.

With email:dns
Without email:dns

Be Aware

  1. Small Changes Can Have Big Consequences
    Adding 'email:dns' felt like a tiny improvement and one of those "nice-to-have" features. But it can a big impact on the user experience, all because of something you can’t control.
  2. Third-Party Dependencies Are Unpredictable
    DNS lookups rely on external servers. If they’re slow, your page is slow. If they fail, your validation fails. Simple as that.

What Can You Do?

If you’re considering using 'email:dns' in your validation rules, here’s my advice:

  • Think Twice: Do you really need to validate the email’s domain at this stage? For most use cases, just checking the format with 'email' is good enough.
  • Use It Sparingly: If you do need 'email:dns', maybe limit it to specific forms or scenarios where it’s absolutely necessary.
  • Prepare for the Worst: Be ready for situations where DNS lookups are slow or fail altogether. Consider adding a fallback or warning users when validation takes longer than expected.

At the end of the day, every second counts when it comes to page speed and sometimes, less is more.

--

--

Infinitypaul
Infinitypaul

Written by Infinitypaul

Software Developer — I admire breaking up complex problems into understandable and sizeable bits, solving daily challenges with technology

Responses (1)