-
Notifications
You must be signed in to change notification settings - Fork 299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sort JSON-LD data by key #458
Conversation
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.
I recommend optimizing as much as possible to reduce impact on performance.
d2d7b2b
to
47513bf
Compare
@ashmaroli I figured we could make this a first-class thing and just change |
Ok I decided to measure the
I went up the chain and am taking advantage of the fact that Hashes preserve insertion order. So if you insert "@context" before "@type", then iterating over the Hash (and serializing it to JSON) will give you "@context" first, and "@type" second consistently. I overwrote the original Drop#to_h to sort the keys. |
eb943df
to
a3caeb4
Compare
This is a wonderful idea! Good job! 👏 I think this is sufficient for an approval now, but definitely could be optimized further.. Essentially, the def to_json(state)
keys.sort.each_with_object({}) do |(key, _), result|
result[key] = self[key]
end.compact.to_json(state)
end 👉 I'll think about this till tomorrow. and shall register my approval then. Thank you very much @parkr |
Deterministic output is desired to improve diffability between builds. If the output randomly changes order each time it's built, then every page on a given site will change with every Jekyll build. Tools like rsync and git which can diff files will always show a change even when the content hasn't truly changed.
a3caeb4
to
af87d9f
Compare
@ashmaroli Great point! Further optimized to add an |
👍 However, this PR may have exposed a bug in Jekyll Core.. Specifically: keys.sort.each_with_object({}) do |(key, _), result| From Jekyll Core codebase def keys
(content_methods |
mutations.keys |
fallback_data.keys).flatten
end Why is the keys.sort.each_with_object({}) do |(key, _), result| instead of: keys.sort.each_with_object({}) do |key, result| UPDATE: Apparently, it's just an implementation bias. |
Yeah I'm not sure why we did that. I'm going to leave it as-is to match Jekyll Core. |
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.
One final edit and this is good to go.
Co-authored-by: Ashwin Maroli <[email protected]>
pinging @mattr- for 👀 |
@ashmaroli let's give Matt until the weekend. If he's not free to take a look by then, then I think your and my approval counts as 2 Jekyll maintainers' approval so we can merge and cut a release. |
Thanks @parkr |
@jekyllbot: merge +minor |
Thanks @mattr-! Thanks @ashmaroli! |
Deterministic output is desired to improve diffability between builds.
If the output randomly changes order each time it's built, then every
page on a given site will change with every Jekyll build. Tools like
rsync and git which can diff files will always show a change even when
the content hasn't truly changed.
Fixes #362. Fixes #436.