Skip to content
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

Social Card upgrade - Part 1 #2090

Merged
merged 10 commits into from Mar 22, 2019

Conversation

mscoutermarsh
Copy link
Contributor

Dev.to + HTML/CSS to Image ⚡️

👋 @benhalpern

This gives the SocialPreviewsController the ability to render png's.

Current URL:
https://res.cloudinary.com/practicaldev/image/url2png/s--pncSBGoq--/c_fill,g_north,h_400,w_800/https://dev.to/social_previews/article/75561%3Fbust%3D0-Build%20a%20GitHub%20Action%20with%20Ruby-true"

Future:
https://dev.to/social_previews/article/89948.png

Fancy Gif

Screen Recording 2019-03-16 at 03 07 PM

Backwards compatible

Implemented this so that this PR is a no-risk deploy. Nothing is breaking. All the old URLs work exactly the same as before. The social_preview routes now also support .png.

How does caching work?

The rendered html is used as the cache key (Dalli SHAs it). This means, we get cache busting "for free", the cache will bust whenever the template changes, or the data included in the template (such as article title or user's name) updates.

To do this, I had to remove some of the randomness in the templates. They are now "deterministically random", meaning the object's ID is used as the seed. So you get the same sequence of random numbers each time.

Release Plan

  • Set HCTI_API_USER_ID and HCTI_API_KEY env variables.
  • Ship it
  • Check some preview urls, make sure they look ✨
  • Follow up PR to update meta tags to point at new URLs

What type of PR is this? (check all applicable)

  • Feature

Mobile & Desktop Screenshots/Recordings (if there are UI changes)

image
(https://hcti.io/v1/image/e632881d-b0f6-4d33-be3f-a5863cf260a2)

image
(https://hcti.io/v1/image/7455df4e-636e-4bed-b0f6-90cb419977fb)

image
(https://hcti.io/v1/image/6c52de9d-4d37-4008-80f8-67155589e1a1)

Added to documentation?

  • no documentation needed

This will be used by social_previews to convert the templates into images.

Setup a fallback image primarily for local dev, so that this won't cause
errors if authentication is missing.

Caching is setup to use contents of the html/css. So it with
autobust/generate a new image whenever the markup changes.
+ Being random breaks caching (since we generate cachekey by a SHA of html content)
+ Use Roboto for when generating via HTML/CSS to Image
+ Make rand deterministic so it doesn't break caching
When adding .png to the url, these routes will now generate an image and
redirect to it.

This leaves the existing pages as they were, so we're backwards
compatible with all the existing links out in the world.
@CLAassistant
Copy link

CLAassistant commented Mar 18, 2019

CLA assistant check
All committers have signed the CLA.

@@ -1,28 +1,65 @@
class SocialPreviewsController < ApplicationController
# No authorization required for entirely public controller

PNG_CSS = "body { transform: scale(0.3); } .preview-div-wrapper { overflow: unset; margin: 5vw; }".freeze
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filling the entire viewport results in a ~4000x4000 pixel image. Scaling it down makes rendering way faster.

Copy link
Contributor

@benhalpern benhalpern left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great. Fabulous implementation.

@benhalpern benhalpern merged commit dd84051 into forem:master Mar 22, 2019
@pr-triage pr-triage bot added the PR: merged bot applied label for PR's that are merged label Mar 22, 2019
@benhalpern
Copy link
Contributor

I've added keys to the prod site, still on the free plan, and can upgrade when ready.

I'm curious about the right path forward will be for the migration so that we might not need to re-generate all the existing generated images where possible. There's a day one issue where we'd need to re-generate all the images as they are ingested by Twitter/FB bots etc. It's not the end of the world, but there could be a long-tail of unique images. Haven't fully thought it through.

@mscoutermarsh
Copy link
Contributor Author

@benhalpern 🙌. I think best option would be to set a "migration" DateTime inside the object/service that gives back the og:image url.

Then if object.updated_at > IMAGE_MIGRATION_TIME_STAMP, use the new URL. Otherwise, use the old already-generated-url.

WDYT? ^

mscoutermarsh added a commit to mscoutermarsh/dev.to that referenced this pull request Mar 25, 2019
This is a follow up to part 1: forem#2090.

This PR updates the og:image URLs to use the new social_preview.png urls
added in Part 1.

So that the already generated/cached images do not need to be recreated,
this sets a MIGRATION_DATETIME. Any object that has been updated after
that date will generate a new image

Objects from before that date will not. They will use the new url.
@mscoutermarsh mscoutermarsh mentioned this pull request Mar 25, 2019
2 tasks
benhalpern pushed a commit that referenced this pull request May 1, 2019
* Social Cards Part 2

This is a follow up to part 1: #2090.

This PR updates the og:image URLs to use the new social_preview.png urls
added in Part 1.

So that the already generated/cached images do not need to be recreated,
this sets a MIGRATION_DATETIME. Any object that has been updated after
that date will generate a new image

Objects from before that date will not. They will use the new url.

* Fix weird change made by rubocop autocorrect

* Handle organizations + users

* Run CI

* Use time with zone to be explicit about timezone

* Simplify branching in user_social_image_url method

* Fix param alignment + bump migration date
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: merged bot applied label for PR's that are merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants