-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add missing string handler in v-for (#4499)
Fix #4497
- Loading branch information
Showing
2 changed files
with
19 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
974247f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is going to hang sites when a backend returns html(large string) instead of the expected json array.
Converting a string to an array is easy in javascript
<span v-for="letter in text.split('')">
.Is the convenience worth the risk?
Maybe introduce a
Vue.options.directives.for.maxStringLength
?Infinity for the current behaviour, 0 to disable and a number to limit the iterators to for example 1024.
974247f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If your API sends you HTML instead of expected JSON your problem is with your API, not with the rendering.
So this:
..would only heal the symptom, not the cause, so I don't see a real necessity for it.
974247f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you're developing a backend, errors like:
Warning: Cannot modify header information - headers already sent by...
are not uncommon and followed by theecho json_encode($myArray);
cause large strings. When this hangs the browser it's not a pleasant developer experience.Agree, and having to remember to set an option to 0 for every project is not something i want to do either.
Luckily the issue can be prevented.
Backend tip
Frameworks like Laravel, which will send
500 Internal server
for small (warning/notice level) mistakes, this prevents the response being handled by javascript.Frontend tip
Use an ajax library that allow you to enforce the responsetype to json, this also prevents the response from being handled.
974247f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bfanger Abouth the
split
method: I tested multiple ways of doing it and thefor
loop is the fastest way974247f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, iterating over a string is faster than converting it to an array and then iterating over that. (as it needs more cpu and more memory).
But take https://jsonplaceholder.typicode.com/users of example, a small 5.6kb json file with 10 users.
When that file had an error before sending the json header, the
v-for
would be creating 5600 instances of the dom structure instead 10 (1 loop for every byte)The massive amount of generated dom is what causing the browser to hang.
974247f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't seem to hang: https://jsfiddle.net/posva/at54mdyu/
The front should handle errors and make sure the data is passed correctly. Imagine you forgot to apply a filter to a query on the back but everything else is right. Your front is really going to hang because you are creating 1M rows instead of just 5 😄 . There's nothing Vue can do about this