0

i have a mobile website that is based on jQuery mobile and MVC. unfortunately there are some problems that the javascript and CSS files are cached on the mobile phones and not always reloaded when i take some updates online.

now i am searching a powershell script for my deployment process that adds with the help of patterns to all javascript and css links the string "?v=randomnumber", so that the javascript and css files will be newly loaded on every update. example:

<script type="text/javascript" src="http://localhost/scripts/myscript.js?v=21876">

as i use MVC this replacement should work for all files placed in "views" folder and all its subfolders.

i am not searching a other solution for the caching problem.

so the first step was to loop all files in the "views" folder. i have done this like this:

Get-ChildItem -Path "C:\inetpub\wwwroot\Inet\MyApp\Views" | ForEach-Object {

}

thanks for your help!

1 Answer 1

1

To achieve this, you'll need to do some kind of search and replace, the following should help you on your way, this uses a guid for the unique identifier.

$guid    = [guid]::NewGuid()
$Search  = "myscript.js"
$Replace = "myscript.js?v=$guid"

Get-ChildItem -Path "C:\inetpub\wwwroot\Inet\MyApp\Views" | ForEach-Object {
    get-content $_ | % {$_ -replace $Search,$Replace} | Set-Content $_ -Force
}

One thing to mention though is that MVC 4 can do this automatically - Bundling and Minification

Edit: A more detailed example using a regex

$guid    = [guid]::NewGuid()
$regex  = ".js|.css"
$replace = "?v=$guid"

Get-ChildItem -Path "C:\inetpub\wwwroot\Inet\MyApp\Views" | ForEach-Object {

    # store the filename for later and create a temporary file
    $fileName = $_
    $tempFileName = "$_.tmp" 
    new-item $tempFileName -type file -force | out-null

    get-content $_ | % {

        # try and find a match for the regex
        if ($_ -match $regex)
        {
            # if a match has been found append the guid to the matched search criteria
            $_ -replace $regex, "$($matches[0])$replace" | Add-Content $tempFileName 
        }
        else
        {
            # no match so just add the text to the temporary file
            $_ | Add-Content $tempFileName 
        }
    } 

    # copy the temporary file to the original file (force to overwrite)
    copy-item $tempFileName $fileName -force

    # remove the temp file
    remove-item $tempFileName 
}
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your answer ;) how can i achieve it, to set the guid on EVERY javascript and css file?!
Change your search criteria to match your requirements, if you need to search for ".js" rather than "myscript.js" then put that in the search variable. If you need multiple then consider using a regular expression - regular-expressions.info/powershell.html
yes, i will need regex for sure, but i am wondering how to do that exactly. i am new to regex and can't find one that makes a find and replace for something like that: <link ... href="mycss.css">
I have added an example using a regular expression, maybe that will help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.