What is the most efficient method for updating URLs in CSS files before the website goes live?

The project I am currently working on has been worked on by various coders with different backgrounds. One of my recent achievements was writing a clean Capistrano recipe to minimize CSS and JS files. Now, the challenge ahead is figuring out how to identify and rewrite various URL patterns scattered throughout our CSS codebase before they are pushed live.

url('../../images/
url(../images/
url("../../../images/
url(images/

For example:

.test{background:url('/images/test.jpg') no-repeat top left}.pouet{background:url("../../images/bg.png")}

It's important to note that some URLs have quotation marks while others do not. My goal is to transform them into:

.test{background:url('http://www.cdnImages.com/images/test.jpg') no-repeat top left}.pouet{background:url("http://www.cdnImages.com/images/bg.png")}

All instances must be replaced with my CDN URL accurately without room for error.

I have devised a regex pattern that suits my requirements: rubular permalink

Initially, I attempted using the sed command like this:

sed '/url([^\/](.*?)images/http:\/\/www.cdnImages.com' myFile.css

However, it did not yield the desired results.

Further research led me to try this approach:

 find #{current_release}/public/static/css/ -name '*.css' | xargs sed -i 's@url\([^\/](.*?)images|static\/@#{images_cdn}@g'

Although the regular expression seems to capture the correct output (see here), there appears to be an issue somewhere in the implementation.

Any suggestions or ideas? Thank you.

Answer №1

Check out my test scenario

console.log("This is a test case for demonstration purposes");\n \
| sed 's@url[(][^)][^)]*)@url(http://cdn.example.com)@g'

.test{background:url(http://cdn.example.com) no-repeat top left}.example{background:url(http://cdn.example.com)}

Oh, and one more thing to consider

console.log("This is a test case for demonstration purposes");\n \
| sed 's@url[(][^)][^)]*)@url('"'"'http://cdn.example.com'"'"')@g'

.test{background:url('http://cdn.example.com') no-repeat top left}.example{background:url('http://cdn.example.com')}

It's worth noting that in some sed commands, you may need to escape the first '@' sign like this: \@.

I noticed that your example maintains the respective single quotes and double quotes that were originally there. I'm assuming it's acceptable to change them all to single quotes. If not, it adds an extra layer of complexity to the task.

Since you mentioned that this is a critical production deployment with no room for error, thorough testing is essential.

For a deeper understanding of sed usage, analyze what the search pattern @url[(][^)][^)]*)@ is accomplishing.

And for shell scripting proficiency, think about why I made the adjustment to '"'"'http://cdn.example.com'"'"' in the final iteration. ;-)

Unfortunately, I'm running short on time at the moment.

I hope this information proves valuable.

Answer №2

It seems like some people are overcomplicating things, but it's actually quite straightforward. This is something I do regularly without any issue.

data=$(cat <<EOF
.test{background:url('/images/test.jpg') no-repeat top left}.pouet{background:url("../../images/bg.png")}
EOF
)
prefix="http://www.cdnImages.com/"
rule=$(cat <<EOF
s|/*\(\.\./\)*\(images/[^'"]*\)|$prefix\2|g
EOF
)

echo "$data" | sed "$rule"

Remember to pay attention to the escaping of \' and "$prefix".

Answer №3

After much brainstorming, I have devised the ideal command to suit our requirements:

execute "find #{current_release}/public/static/css/ -name '*.css' -print0 | xargs -0 sed -i -E 's@(\.\.\/)+(images|static|img)@#{images_cdn}@g'"

Efficient and secure execution!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing Python and Selenium to Locate an Element Based on Text Within a Span Class

My challenge is locating an element without any ID or distinctive identifiers. The HTML code structure resembles the following: <div class="item_select"> <span class="item_selection_number">A </span> </div> <div class="item_ ...

What is the reason behind Svelte not scoping the tag under a class, but instead using individual tag.class to style a component?

It is common practice to scope CSS styles for components like this: .my-component-01 h1 { ... } .my-component-01 h2 { ... } However, Svelte takes a different approach by applying the styles directly to the tags: h1.svelte-hlsza1{color:orange} h2.svelte- ...

Tips for effectively downsizing an HTML Canvas to achieve high-quality images

Introduction I am currently facing issues with blurry visuals in my canvas animation, especially on devices with high pixel densities like mobile and retina screens. In order to address this problem, I have researched canvas down-scaling techniques and f ...

Having trouble getting the styles property to work in the component metadata in Angular 2?

Exploring Angular 2 and working on a demo app where I'm trying to apply the styles property within the component metadata to customize all labels in contact.component.html. I attempted to implement styles: ['label { font-weight: bold;color:red } ...

Applying CSS styles to my container through a button click event

Currently, I am attempting to apply a padding-top to my container when a button is clicked. Unfortunately, the padding-top is not being added as expected. $('#login').on('click', function(){ $('#loginform1').css('padd ...

The elements from base.html and the extended page are now placed on separate rows rather than on the same row

I'm relatively new to HTML, Jinja, and CSS and have been playing around with creating a webpage that retrieves data from a sqlite3 database. As of now, I am facing some challenges regarding formatting. To assist with table formatting and overall aest ...

What is the best way to address background-image overflow on a webpage?

I'm facing a challenge in removing the overflow from a background-image within a div. There are 4 divs with similar images that combine to form one background image (I adjust the position of each image so they align across the 4 divs). Essentially, I ...

Shiny Chrome, Flexible Flexbox, and plump div elements

Looking to enhance my understanding of flexbox. Encountering a display issue in Chrome where the right column is too wide and the left column is too skinny, while it displays correctly in Firefox. Any suggestions on how to fix this for Chrome? .child ...

The image within the element is being cropped despite having a higher z-index

On my page, I am dynamically generating a table using an API request. Each row in the table has an icon that, when hovered over by the user, displays an image associated with that item. However, I seem to have misunderstood how z-index functions, as I have ...

Photos inside a flexbox do not resize correctly

When placing 2 images inside a flexbox with the column direction, I anticipated that the images would resize accordingly when adjusting browser window size. However, this is not the case. Regardless of the CSS styles applied, I am unable to maintain the o ...

What methods can I use to accomplish the transformation of superimposed images?

Struggling with transforming a content box on my webpage. I have a div containing three overlapping images positioned using Grid, and I want to scale one of them to fill the entire div when scrolled over. While I managed to achieve this effect, I'm un ...

Troubleshooting a Problem with the Horizontal Scrollbar in Dynamic jqGrid

Currently, I am working on an ASP.net MVC project where I have implemented both dynamic and static jq grids. However, I am encountering an issue with the horizontal scroll bar in my application. To address this problem, I attempted to modify the overflow ...

The element will display above all other elements in its parent container

I am currently developing a React application, and the code structure is set up as follows: <Div> <Div style={{maxHeight:'60vh'}}> //This section includes the code for displaying a list item. Each item has its context menu that can ...

Tips for refreshing the appearance of a window in angular when it is resized

I have a chat feature integrated into my application. I am looking to dynamically resize an image within the chat window when the width of the window falls below a certain threshold. Is there a method available to modify the CSS style or class based on the ...

Problem with IE off-canvas scrolling

Currently, I am facing an issue with the scrolling functionality of an off-canvas sidebar on my Joomla 3 website. It seems to be working fine in Chrome and Firefox, but when it comes to Internet Explorer, the visible scroll bar refuses to move when attempt ...

Switch out the image in the dropdown and update the button once it is selected

In this scenario, I am looking to dynamically change the image in a button dropdown when it is clicked. The goal is for the dropdown image to swap and replace with the actual one upon clicking. For reference, here's the fiddle: https://jsfiddle.net/32 ...

Creating an arrow line with CSS styling can be achieved easily

In order to create a horizontal line with a breakdown in the middle that displays an arrow, I want to use only HTML and CSS without relying on bitmap images. If needed, Font Awesome can be used to achieve the desired result. It's important for me to h ...

What's the key ingredient I need to add for a dropdown navigation menu?

What am I overlooking in my HTML and CSS coding to create a standard list dropdown in my navigation menu? When I preview this section of the page in Chrome and IE, the navigation area appears fine until I hover over the Fishing and Guides link. Instead of ...

Creating a notification feature for an HTML application

I am in the process of creating an HTML app through intel XDK. While I understand that using HTML to build apps is not as common, it is the language I am most familiar with, along with CSS. One feature I would like to include in my app is a weekly notific ...

HTML - Custom styled <select> element

My goal is to fully customize the style of a component and hide the selector image. All graphical styles such as borders and shadows have been defined in a previous table. Now, I want to remove all styling from the selection menu, including the icon used t ...