Automatic Versioning & “Cache Busting”

Implemented a CDN/caching layer but haven’t had time to get the versioning of assets worked out properly? Try some basic “cache-busting” using a querystring parameter which gets updated each time you build:

Update AssemblyInfo.cs to end with an asterisk (“*”):
[csharp]
[assembly: AssemblyVersion("1.0.0.*")]
[/csharp]

Create a little helper method:
[csharp]
public static class AppHelper
{
private static string _version;
public static string SiteVersion()
{
return _version ?? (_version =
Assembly.GetAssembly(typeof (HomeController))
.GetName().Version.ToString());
}
}
[/csharp]

And use this value in static file references:

[html]
<img src="/img/[email protected]()" alt="Logo" />
[/html]

Which will render something like:

[html]
<img src="/img/logo.png?v=1.0.0.20123" alt="Logo" />
[/html]

This number will change with every build, so should force retrieval of updated static – cached – content.

Upload to Azure Blob Storage using Powershell

I needed to automate the process of uploading images to Azure blob storage recently, and found that using something like the excellent Azure Storage Explorer would not set the Content Type correctly (defaulting to “application/octetstream”). As such, here’s a little script to loop through a directory and do a basic check on extensions to set the content type for PNG or JPEG:

The magic is in Set-AzureStorageBlobContent.

Don’t forget to do the usual dance of calling the following!

These select your publish settings file, and set which subscription is the currently active one:

  • Import-AzurePublishSettingsFile
  • Set-AzureSubscription
  • Select-AzureSubscription

Update

Actually, the Aug 2014 version of Azure Storage Explorer already sets the content type correctly upon upload. Oh well. Still a handy automation script though!

Getting past Powershell & SQL’s “Incorrect syntax near ‘GO’ ” message

Many a night have I bashed my head on the keyboard when seeing “Incorrect syntax near ‘GO'” come back from a powershell script trying to execute a batch SQL script.

After learning that “GO” is not actually SQL, and more of a SQL Server Management Studio “batch helper”, I quickly knocked together this powershell script to execute SQL batch scripts remotely. Fits nicely into an environment creation pipeline, so it does.

Param(
    [string]$Server,
    [string]$DB,
    [string]$user,
    [string]$Pwd,
    [string]$Script
)

$batches = $Script -split "GO\r\n"

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=$Server;Database=$DB;User ID=$user;Password=$Pwd;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"
$SqlConnection.Open()

foreach($batch in $batches)
{
    if ($batch.Trim() -ne ""){

        $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
        $SqlCmd.CommandText = $batch
        $SqlCmd.Connection = $SqlConnection
        $SqlCmd.ExecuteNonQuery()
    }
}
$SqlConnection.Close()

London Web Performance Group meetup

London Web Performance Group Meetup

I’ll be speaking with my cohort, Dean Hume, at the next London Web Performance meetup on Oct 14th at the Financial Times offices!

We’re presenting an extended (director’s cut?) version of our Velocity NY 2014 session, The Good, the Bad, and the Ugly of the HTTP Archive where we investigate a selection of websites exposed by the HTTP Archive as well as talk about how to use Google’s Big Query and dig into some awesome example queries to explore what’s happening in the interwebs.

The LWPG is where you can “meet with other web site system administrators, developers, designers and business people who’re interested in making their sites work fast to get better user experience, lower abandonment rates and make more money.

If you’ve read Steve Souder’s books and you use ySlow & PageSpeed and you want learn more or share your knowledge please please sign up!”

Go on – you know you want to!

XML RPC Vulnerability

Looks like this poor little blog was victim of a recent spate of WordPress hack attacks, using the xmlrpc.php file (used for implementing access to the wordpress functionality from remote clients). My Apache logs are freakin stuffed with tens of thousands of POSTs to this url, which apparently can cause the server to bork.

.htaccess updated to block it, plus the feature is now disabled. And WordPress updated. Let’s see how we go now..