0

jQuery モバイルと MVC に基づくモバイル Web サイトがあります。残念ながら、javascript および CSS ファイルが携帯電話にキャッシュされ、オンラインで更新を行ったときに常にリロードされるとは限らないという問題があります。

今、すべてのjavascriptとcssリンクにパターンの助けを借りて追加する展開プロセス用のpowershellスクリプトを検索してます。例:

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

私はMVCを使用しているので、この置換は「ビュー」フォルダーとそのすべてのサブフォルダーに配置されたすべてのファイルに対して機能するはずです。

私はキャッシュの問題に対する他の解決策を探していません。

したがって、最初のステップは、「ビュー」フォルダー内のすべてのファイルをループすることでした。私はこれを次のようにしました:

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

}

ご協力いただきありがとうございます!

4

1 に答える 1

1

これを実現するには、ある種の検索と置換を行う必要があります。次の方法が役立つはずです。これは、一意の識別子に GUID を使用します。

$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
}

ただし、MVC 4 はこれを自動的に行うことができるということは、言及しておく必要があります -バンドルと縮小

編集:正規表現を使用したより詳細な例

$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 
}
于 2013-04-29T08:38:13.927 に答える