Automating DNS Change Propagation Check
When you change hosting providers or have other reason to change your domain's DNS, you'll want to test the website as soon as possible after the DNS has changed.
Does everything work? Perhaps not every page needs to be tested. But certainly every type of functionality — forms, links, database reading and writing.
The DNS can change nearly immediately or take up to 48 hours. The change has to propagate to every ISP in the world before everybody in the world can find the website at its new location.
How soon you can access the website's new location depends on your ISP. Many ISPs update their routers every hour or every several hours. Others might do it once a day.
Sitting around waiting for the DNS to propagate isn't the most efficient use of time. Nor is it efficient to intermittently interrupt other projects to do a manual check.
Here's one way to automate it:
-
Make a web page to be used only for this automation. Let it have "Hello!" as it's content. It also has a JavaScript-generated meta tag to reload the page every 5 minutes.
-
Make a copy of that page and upload it to the server at the previous DNS. Note its URL.
-
To the original copy of the web page, add a bit of JavaScript to spawn a "DNS Changed!" alert box. Upload it to the server at the new DNS so it has the very same URL as the copy residing at the previous DNS.
-
Load the web page URL into your browser. Let it run in a tab by itself while you use other tabs for other projects.
When the DNS has changed, the tab will spawn the alert box.
It works because the first time the page reloads after your ISP has updated the DNS for the web site, it will get the page from the new location. And that new-location page has the JavaScript with the alert box.
Here is the source code for both versions of the web page.
The Web Page to Automate DNS Propagation Check
Here's the web page to upload to the server with the previous DNS.
<!DOCTYPE html> <html> <head> <script> document.write('<meta http-equiv="refresh" content="300; url=http://example.com/test.html?" + (new Date().getTime()) + ">'); </script> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Automated DNS Check</title> </head> <body> <div style="text-align:center; font-size:1in;"> Hello! </div> </body> </html>
On the fifth line of the above source code are two places to customize. One place is colored red and the other is colored blue.
-
If desired, change 300 to the number of seconds to pause before the next page reload. The number 300 represents 300 seconds, which is 5 minutes. The 300 can be changed to any other number.
-
Change the http://example.com/test.html URL to the URL of the web page when it's uploaded to the server with the previous DNS.
The reason the meta-refresh tag is generated with JavaScript is to append a different number to the URL every time the page is reloaded. It makes the browser recognize it as a different URL. The JavaScript timestamp is used for this.
When the browser sees the different URL (with the current timestamp appended after the "?" character), it won't find the URL in its cache. Therefore, it must load the page from scratch. This virtually guarantees the new DNS will be used the first time it's available on a page reload.
Paste this line of JavaScript into the web page for uploading to the new DNS server:
<script>alert("DNS Changed!")</script>
Paste the line of JavaScript anywhere into the web page. The example code below assumes a location immediately above the cancel </body>
tag.
Here's the complete web page source code with the line of JavaScript inserted.
<!DOCTYPE html> <html> <head> <script> document.write('<meta http-equiv="refresh" content="300; url=http://example.com/test.html?" + (new Date().getTime()) + ">'); </script> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Automated DNS Check</title> </head> <body> <div style="text-align:center; font-size:1in;"> Hello! </div> <script>alert("DNS Changed!")</script> </body> </html>
The line of JavaScript inserted into the above code is colored green.
Setting It Up
Upload the two web pages to their respective servers. Ensure they both have the same URL when the DNS is pointing to the server they're on.
Load the URL into your browser.
When you see the "DNS Changed!" alert message, the DNS has changed at your ISP.
(This article first appeared in Possibilities ezine.)
Will Bontrager