Home Gitesh Portfolio Blog About Me Gallery Contact

Sitecore: Powershell query to rebuild SXA optimized min files

With this powershell command you will be to rebuild SXA optimized min files.

 

Write-Host "---Regenerate Optimized-min files...---"
Write-Host ""	
Write-Host "Step 1 - Deleting optimized-min items..." -ForegroundColor Yellow
	
	$optimisedMinPathScript = "master:/sitecore/media library/Themes/projectName/Home/projectName Theme/Scripts/optimized-min"
	$optimisedMinPathStyles = "master:/sitecore/media library/Themes/projectName/Home/projectName Theme/styles/optimized-min"
 
    $optimisedWebMinPathScript = "web:/sitecore/media library/Themes/projectName/Home/projectName Theme/Scripts/optimized-min"
	$optimisedWebMinPathStyles = "web:/sitecore/media library/Themes/projectName/Home/projectName Theme/styles/optimized-min"
	
	Remove-Item -Path $optimisedMinPathScript
	Remove-Item -Path $optimisedMinPathStyles
 
    Remove-Item -Path $optimisedWebMinPathScript
	Remove-Item -Path $optimisedWebMinPathStyles
	
Write-Host "       Finished Deleting optimized-min files from MASTER and WEB..." -ForegroundColor Green 
Write-Host "    "

Write-Host "Step 2 - Regenerating optimized-min items by browsing experience editor..." -ForegroundColor Yellow

	$homeItem = Get-Item -Path "master:/sitecore/content/projectName/Home/Home"
	$urlOptions = [Sitecore.Links.LinkManager]::GetDefaultUrlOptions(); #Shell context, will resolve CM servername.
	$urlOptions.AlwaysIncludeServerUrl = $true;
	$urlOptions.ShortenUrls = $true;
	$urlOptions.SiteResolving = $true;
	$homeUrl = [Sitecore.Links.LinkManager]::GetItemUrl($homeItem, $urlOptions);
	$cmHost = ([System.Uri]$homeUrl).Host
	
	#$shortId = $homeItem.ID.ToString().Replace("{","").Replace("}","")
	#$experienceEditorUrl = "https://$cmHost/?sc_mode=edit&sc_itemid=%7b$($shortId)%7d"
	#$experienceEditorUrl = "$($homeUrl)?sc_database=master"
	$experienceEditorUrl = "https://$cmHost$($homeItem.FullPath)"
	
	$responseURL = $null
	$response = $null
	$response = Invoke-WebRequest -Method Get -Uri $experienceEditorUrl -MaximumRedirection 0 -UseBasicParsing -ErrorAction SilentlyContinue
 
	if ( $response -ne $null -and $response.StatusCode -ge 400 )
	{
		Write-Host "       Unable to contact host to regenerate optimized-min files on $cmHost CM server. StatusCode: $($response.StatusCode). Request URL: $experienceEditorUrl" -ForegroundColor Red
		break
	}
	else {
		$optimisedMinScript = Get-Item $optimisedMinPathScript -ErrorAction SilentlyContinue
		$optimisedMinStyles = Get-Item $optimisedMinPathStyles -ErrorAction SilentlyContinue
		
		if ($optimisedMinScript -ne $null -and $optimisedMinStyles -ne $null) {
			Write-Host "       Optimized-min files regenerated on $cmHost CM server. StatusCode: $($response.StatusCode). Request URL: $experienceEditorUrl" -ForegroundColor Green
		}
		else {
			Write-Host "       Unable to regenerate optimized-min files on $cmHost CM server. StatusCode: $($response.StatusCode). Request URL: $experienceEditorUrl" -ForegroundColor Red
			break
		}
	}
 
Write-Host ""	
 
Write-Host "Step 3 - Publishing files" -ForegroundColor Yellow

Write-Host "       Publishing theme's scripts folder..."
	$item = Get-Item -Path "master:/sitecore/media library/Themes/projectName/Home/projectName Theme/Scripts"
	$item | Publish-Item -Recurse -PublishMode Smart -targets "web" #-AsJob
 
Write-Host "       Publishing theme's styles folder..."
	$item = Get-Item -Path "master:/sitecore/media library/Themes/projectName/Home/projectName Theme/styles"
	$item | Publish-Item -Recurse -PublishMode Smart -targets "web" #-AsJob
	
Write-Host ""	
Write-Host "---Optimized-min regeneration completed---"
Posted: 6/09/2022 3:01:58 p.m. by Gitesh Shah | with 0 comments