background
The team is recruiting front-end development recently. I received a resume this morning. It is a girl who meets the requirements from the skill point, so I made an appointment for an interview at 3pm.
The interview, which lasted about 40 minutes, included some generic questions, including “Common performance optimization techniques.” The girl mentioned that she had read the Graphic HTTP, and I asked, incidentally, if I knew about the cache-related headers that are common in HTTP.
If the girl can answer one or two, like max-age, the question is basically allowed. But I was surprised by the girl’s reply :(I can’t remember the words, but something like this)
“Isn’t web cache setup something in the background?”
Why do you think web cache Settings are in the background
The girl’s logic is not hard to understand, because the resources needed for the Web, such as web pages, JavaScript, CSS, images, etc., are all hosted on the server. And the server most of the time, whether it is the background students, or the operation and maintenance students in the management, many front-end students do not have much opportunity to contact the server, let alone up to the static resources to do the cache Settings.
I believe that many front-end newcomers also think so. I’ve lost count of the number of times in the tech community that when someone discusses something that seems “not particularly relevant” at the front, one or two of the group members will say angrily:
Don’t old pack force into it, the front end doesn’t write web pages cut pictures, these things have gross use, and can’t raise wages.
For this kind of talk, I usually choose to dive away, not to fight.
Everything happens for a reason
I have my own answer to the question “Isn’t the Web cache setup something in the background?”
Just because it’s easy to throw out a conclusion doesn’t mean it’s easy for others to accept. So I took a roundabout approach and asked the girl, “Do you have a cache for your online projects?”
Again, the girl’s answer surprised me:
“No, after setting the cache, some users accessed the wrong version, so no cache was set.”
In addition to not knowing about cache Settings, the girl is also not familiar with static resource versioning.
A light bulb went off in my head — this was my chance to preach. Then the scene instantly switched from interview mode to class mode.
Cache setup issues and solutions
Question 1: What are the problems caused by not setting cache?
A: The first is of course performance. Each time a user accesses the server, the user access speed and server load are worrying. Then there is the cost. Those of you who have used cloud services should know that bandwidth and flow are all bought by MAO Grandpa.
Q2: Is the cache causing the version access error problem?
A: In terms of the error scenario, it is the cache. But caching is actually innocent, and the culprit is inappropriate static resource versioning.
Q3: What is the solution?
A: Versioning static resources (for example, hash the file names of static resources), and setting the appropriate cache time for web pages (depending on the actual situation). This allows for both version upgrades and performance.
The ultimate question: Is web caching really all about the background
See here, I believe you have come to their own answers.
From the perspective of front-end development, caching static resources can reduce users’ access time and improve users’ access experience, which is a basic and important optimization in most cases.
How long should static resources be cached? Never cache? Ten minutes? 1 hour? 1 year? .
There is no right answer, and a lot of times, it depends on the technology architecture, the business scenario.
For example, as the girl mentioned above, they don’t do static resource versioning, so never caching is acceptable. After all, many times, users have less tolerance for service errors than poor performance.
For example, if you have implemented version control schemes such as file name and hash, you can set the cache time of static resources such as JavaScript, CSS and images to be longer, such as 1 month. The cache time of web pages can be shorter, such as checking every 10 minutes.
If the front end students lack of understanding of web cache Settings, think that it is the background, then it is difficult to put forward reasonable technical solutions in many cases.
At the very least, the web cache setup is assumed to be done in the background (which is likely to be the case in large companies), so how should the cache be set up? In the end, it’s the front-end scene that counts. There are only a few cache-related headers, and there may be hundreds or thousands of business/technical scenarios. In this case, the front-end can be regarded as Party A and the background as Party B, and only the front-end knows the requirements.
Write in the back
For the most part, this question was only a small part of the interview, and I didn’t get too hung up on it. I just feel that many students have certain misunderstandings on similar issues, and feel that it is not quick to spit out.
PS: Yunhan Financial Technology is looking for a front-end developer in Shenzhen with an annual salary of 15W-30W. Interested parties can send a personal letter or resume. Job description can be clicked on the portal.