How can we force clients to refresh JavaScript files?


Q: How can we force web clients to refresh or load latest version javascript files from the web server.

This is the problem most of the web developers facing after deploying their newer versions of javascript files.

We normally ask the customer to hit Ctrl + F5 to ensure that all the latest javascript files loaded from server not from the cache.

But we can solve this in different ways as follows

Solution 1: If you are maintaining javascript versioning

If you are using javascript versions, it’s easy to solve the issue. i.e we just need to add the version to javascript file path as follows

<script src="~/scripts/demojs.js?100"></script>
<script src="~/scripts/demojs.js?version=100"></script>

so If the version of the file changed then the url also change. so if the url is different than the file which is already in cache, the browser (client) will request to get latest file from the server.

So these file will render to clients as follows

Loading from cache

image

Loading from server

image

Solution 2: If you are not using versioning use Asp.Net MVC datetime object ticks

In this case we just use ticks from C# datetime object as follows

<script src="~/scripts/demojs.js?@DateTime.Now.Ticks"></script>
<script src="~/scripts/demojs.js?currentTime=@DateTime.Now.Ticks"></script>

image

Solution 3: Restart application and Recycle Application pool in IIS

Please do following steps after completion of your deployment in IIS

  1. Restart your website

image

2. Then find the applicationpool of your website and recycle it.

image

In case of Microsoft Azure websites

Screenshot_10

Thanks

Happy coding ComputerSmile

Introduction to AngularJS filters to format and transform and sort


AngularJS is providing set of filters to Format and Transform and Sort the data.

If we want to filter the data inside javascript objects or properties we need to write hundreds of lines of code. In order to archive this we need to spend number of resources and time, But in case of AngularJS it’s completely reverse.

AngularJS is providing in-built filters to format and transform and sort the data.

AngularJS format and transform filters

AngularJS format filters help us to format and transform our data using the following filters.

  1. currency:

    • We can use this currency filter to format currency into specific format

    • for example a decimal property is having just number (300), and now if you want to display that number in currency format like $300.

  2. date:

    • We will use this filter to format date into specific format like “DD/MM/YYYY” or “MM/DD/YYYY” …

  3. filter:

    • We will use this filter to get specific or subset of data from a json object or array

  4. json:

    • This filter allows you to convert a javascript object to JSON string.

    • most of the times we will use this in debugging.

  5. limitTo:

    • we will use this filter to limit a javascript array or string into a specific number of elements.

  6. lowercase:

    • we will use this filter to transform our string to lower case

  7. uppercase:

    • we will use this filter to transform our string to upper case

  8. number:

    • we will use this filter to format a number to string and limit number of decimal

AngularJS sorting filters

we will use sortBy filter to sort the data.

$watch and $watchCollection is not firing/working on data change in angularJS


if $watch and $watchCollection is not firing on data change we need to use 3rd parameter i.e objectEquality parameter (type of boolean)

$watch(watchExpression, listener, [objectEquality])

above statement is the $watch signature with three parameters

in these three parameters third one (objectEquality) is optional and of type boolean

Example 1:

$scope.$watch(‘userProfile.userName’,
function (newValue, oldValue) {
$scope.userName = newValue;
}, true);

Example 2:

$scope.$watch(function () {
return authenticationService.userProfile;
},
function (newValue, oldValue) {
$scope.userProfile = newValue;
}, true);

Happy CodingComputerSmile

invalid uri: the uri string is too long. XDocument load


The reason for the error

invalid uri: the uri string is too long. XDocument load

is the issue with parameter we are passing. It is expecting file path or textreader or xmlreader depends on the method signature we are using.

The parameter we are following should not be the XML data in string.

XDocument.Load method is having 8 overloaded functions as shown below.

//
// Summary:
//     Creates a new System.Xml.Linq.XDocument from a System.IO.TextReader.
//
// Parameters:
//   textReader:
//     A System.IO.TextReader that contains the content for the System.Xml.Linq.XDocument.
//
// Returns:
//     An System.Xml.Linq.XDocument that contains the contents of the specified System.IO.TextReader.
public static XDocument Load(TextReader textReader);
//
// Summary:
//     Creates a new System.Xml.Linq.XDocument from an System.Xml.XmlReader.
//
// Parameters:
//   reader:
//     A System.Xml.XmlReader that contains the content for the System.Xml.Linq.XDocument.
//
// Returns:
//     An System.Xml.Linq.XDocument that contains the contents of the specified System.Xml.XmlReader.
public static XDocument Load(XmlReader reader);
//
// Summary:
//     Creates a new System.Xml.Linq.XDocument instance by using the specified stream.
//
// Parameters:
//   stream:
//     The stream that contains the XML data.
//
// Returns:
//     An System.Xml.Linq.XDocument object that reads the data that is contained in
//     the stream.
public static XDocument Load(Stream stream);
//
// Summary:
//     Creates a new System.Xml.Linq.XDocument from a file.
//
// Parameters:
//   uri:
//     A URI string that references the file to load into a new System.Xml.Linq.XDocument.
//
// Returns:
//     An System.Xml.Linq.XDocument that contains the contents of the specified file.
public static XDocument Load(string uri);
//
// Summary:
//     Creates a new System.Xml.Linq.XDocument from a System.IO.TextReader, optionally
//     preserving white space, setting the base URI, and retaining line information.
//
// Parameters:
//   textReader:
//     A System.IO.TextReader that contains the content for the System.Xml.Linq.XDocument.
//
//   options:
//     A System.Xml.Linq.LoadOptions that specifies white space behavior, and whether
//     to load base URI and line information.
//
// Returns:
//     An System.Xml.Linq.XDocument that contains the XML that was read from the specified
//     System.IO.TextReader.
public static XDocument Load(TextReader textReader, LoadOptions options);
//
// Summary:
//     Loads an System.Xml.Linq.XDocument from an System.Xml.XmlReader, optionally setting
//     the base URI, and retaining line information.
//
// Parameters:
//   reader:
//     A System.Xml.XmlReader that will be read for the content of the System.Xml.Linq.XDocument.
//
//   options:
//     A System.Xml.Linq.LoadOptions that specifies whether to load base URI and line
//     information.
//
// Returns:
//     An System.Xml.Linq.XDocument that contains the XML that was read from the specified
//     System.Xml.XmlReader.
public static XDocument Load(XmlReader reader, LoadOptions options);
//
// Summary:
//     Creates a new System.Xml.Linq.XDocument from a file, optionally preserving white
//     space, setting the base URI, and retaining line information.
//
// Parameters:
//   uri:
//     A URI string that references the file to load into a new System.Xml.Linq.XDocument.
//
//   options:
//     A System.Xml.Linq.LoadOptions that specifies white space behavior, and whether
//     to load base URI and line information.
//
// Returns:
//     An System.Xml.Linq.XDocument that contains the contents of the specified file.
public static XDocument Load(string uri, LoadOptions options);
//
// Summary:
//     Creates a new System.Xml.Linq.XDocument instance by using the specified stream,
//     optionally preserving white space, setting the base URI, and retaining line information.
//
// Parameters:
//   stream:
//     The stream containing the XML data.
//
//   options:
//     A System.Xml.Linq.LoadOptions that specifies whether to load base URI and line
//     information.
//
// Returns:
//     An System.Xml.Linq.XDocument object that reads the data that is contained in
//     the stream.
public static XDocument Load(Stream stream, LoadOptions options);

The parameter we are following should not be the XML data in string.

If you want to load xml from string to XDocument use XDocument.Parse as shown in below picture

Screenshot_3

Happy Coding ComputerSmile