Array state will be cached in iOS 12 Safari. Is it a bug or feature?

Cover Image for Array state will be cached in iOS 12 Safari. Is it a bug or feature?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

Array State in iOS 12 Safari: Bug or Feature? 🤔

Hey tech enthusiasts! 👋 Have you recently encountered a strange behavior with the Safari browser on iOS 12? Are you scratching your head wondering if it's a bug or a brand new feature? You're not alone! Let's dive into this dilemma and find out what's really going on. 🕵️‍♂️

The Problem: Array State in iOS 12 Safari 🐞

Picture this scenario: You're happily coding away on your website and you come across this innocent-looking piece of code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <title>iOS 12 Safari bugs</title>
    <script type="text/javascript">
    window.addEventListener("load", function ()
    {
        let arr = [1, 2, 3, 4, 5];
        alert(arr.join());

        document.querySelector("button").addEventListener("click", function ()
        {
            arr.reverse();
        });
    });
    </script>
</head>
<body>
    <button>Array.reverse()</button>
    <p style="color:red;">test: click button and refresh page, code:</p>
</body>
</html>

This code creates a simple web page with a button. When the button is clicked, it reverses the order of elements in an array. Easy peasy, right? 🙌

However, the trouble begins when you refresh the page after clicking the button. Uh-oh! You notice that the array's value remains reversed! 😱

Bug or Feature? 🐛🆚🌟

At first glance, you might think this is a bug in iOS 12 Safari. Why doesn't the array revert back to its original state after refreshing the page? Isn't that how it's supposed to work? 🤔

But hold on a second, before jumping to conclusions! This behavior might be intentionally designed as a feature by the Safari team. Let's explore further. 🌐📱

The Explanation: Caching in iOS 12 Safari ✨

In the world of web development, caching is a common technique used to improve website performance. Caching allows the browser to store certain resources locally, reducing the need to fetch them from the server every time a page is loaded. This can greatly speed up page loading times and improve the user experience. 🚀

In iOS 12 Safari, it seems that the state of the array used in our code snippet is being cached. When you refresh the page, instead of discarding the cached value and creating a fresh array, Safari keeps the original state intact. This behavior is likely an optimization to avoid unnecessary memory allocation and improve performance. 🤖💨

The Solution: Clearing the Cache 🔒

So, how can we overcome this behavior and ensure that our array state is not cached by iOS 12 Safari? The easiest solution is to manually clear the cache after performing the array manipulation. Here's the updated code snippet:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <title>iOS 12 Safari bugs</title>
    <script type="text/javascript">
    window.addEventListener("load", function ()
    {
        let arr = [1, 2, 3, 4, 5];
        alert(arr.join());

        document.querySelector("button").addEventListener("click", function ()
        {
            arr.reverse();
            location.reload(true); // Clear cache and refresh page
        });
    });
    </script>
</head>
<body>
    <button>Array.reverse()</button>
    <p style="color:red;">test: click button and refresh page, code:</p>
</body>
</html>

By adding the line location.reload(true); to our code, we force the browser to clear the cache and reload the page every time the button is clicked. This ensures that our array starts fresh and is not affected by any caching mechanisms. 🔄

Update: Fixed in iOS 12.1! 🎉

Update at 2018.10.31: Good news, folks! This bug has been officially fixed in iOS 12.1. 🎉 So, if you're using the latest version of Safari, you won't encounter this caching behavior anymore. Hooray for updates! 🙌

Try it Yourself! 💻

Excited to experiment with this bug/feature in iOS 12 Safari? Give it a go by visiting this demo page:

https://abelyao.github.io/others/ios12-safari-bug.html

Play around with the button, refresh the page, and witness the array's state being cached in action. Don't forget to try the updated code with the cache-clearing solution as well. Happy coding! 🤓🚀

Conclusion: Bug or Feature? You Decide! 🕵️‍♀️🏆

Now that you're armed with knowledge about the array state caching behavior in iOS 12 Safari, it's time to form your own opinion. Is it a bug? Is it a feature? Perhaps it's a clever combination of both! 🤷‍♂️

No matter what you think, understanding the inner workings of the technologies we use is crucial for effective web development. Remember, there's always a solution to every problem, even if it involves clearing the cache! 😉

So, try out the code, explore Safari's caching behavior, and don't hesitate to leave your thoughts in the comments below. Let's keep the conversation going! 💬📢


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello