Repository navigation
Conversation
Shared links pointed to /#!/events/1. The fragment is never sent to the server, so link preview bots (WhatsApp, Telegram, Signal, Slack, Discord, LinkedIn...) fetched the root of the application and showed the generic card of the website. The social bot controller only answered to a few user agents using the deprecated _escaped_fragment_ crawling scheme. Add /events/:id, /trainings/:id and /projects/:id, which render the OpenGraph and Twitter Card meta tags for any user agent and immediately redirect browsers to the matching page of the application. Unknown, deleted, disabled or unpublished items are not found. The legacy route now only matches requests with _escaped_fragment_, so the listed bots fetching / get the application instead of a 404, and it renders the same template: the image URL no longer contains a double slash and og:url is the clean share URL.
The Facebook and X share buttons of events and projects now share the fragment-free URLs, which provide link previews. A new button copies this URL to the clipboard.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a link to an event, a training or a project is shared in a messenger or a social network (WhatsApp, Telegram, Signal, Slack, Discord, LinkedIn, Facebook, X…), the preview shows the generic card of the website instead of the shared item.
Root cause
https://host/#!/events/439. The fragment (#!/events/439) is never sent to the server, so preview bots fetch/and get the generic single page application.SocialBotControlleronly answers on the root route, for a short list of user agents (facebookexternalhit,Twitterbot,Pinterest,Google.*snippet), and only for the deprecated (2015)_escaped_fragment_AJAX crawling scheme. WhatsApp, Telegram, Signal, Slack, Discord, LinkedIn… never use it.GET /without_escaped_fragment_returned a 404.og:imagecontained a double slash (root_url + image.url→https://host//uploads/…) andog:urlpointed back to the#!URL, so Facebook re-scraped the generic page.Solution
GET /events/:id,GET /trainings/:id(slug) andGET /projects/:id(slug). They render a small HTML page with:largeversion, with its dimensions),og:url= the clean share URL,og:type,og:site_name(fablab_namesetting),fb:app_id/twitter:sitewhen configured, and<link rel="canonical">;window.location.replace('/#!/events/:id')for humans, plus a visible fallback link. Bots do not run JavaScript, so they stay on the meta tags; browsers jump straight to the application.<meta http-equiv="refresh">: some scrapers (Facebook's at least) follow meta refreshes, which would bring them back to the generic page, and the application cannot run without JavaScript anyway.json_escaped./api,/open_api,/rss).format: falseis set so that e.g./projects/foo.jsonis a 404 instead of a missing template error._escaped_fragment_is present, so the listed bots fetching/get the normal application instead of a 404. It renders the same template, so it also benefits from the absolute image URL fix and the cleanog:url. The three near-identical templates were merged into onesocial_bot/show.html.erb.navigator.clipboard, with awindow.promptfallback when the clipboard API is unavailable, e.g. on plain HTTP) and shows a growl notification, like the existing iCal "copy link" feature.copy_link,link_copied) underapp.public.events_showandapp.public.projects_show, added to everyapp.public.*.ymllocale file except the Crowdin pseudo-localezu(it only contains Crowdin ids).enandfrkeep the same key layout as required bytest/meta/i18n_test.rb.Absolute URLs use
DEFAULT_HOST/DEFAULT_PROTOCOLwhen they are set (asApplicationControllerdoes), else the request host.How to test
In a browser, opening
https://your-instance/events/439immediately lands onhttps://your-instance/#!/events/439. The Facebook Sharing Debugger / LinkedIn Post Inspector can be used on the clean URL.Tests
New
test/integration/social_share_test.rb: 200 + meta tags (title, plain-text and truncated description, absolute image URL, canonical/og:url, redirect target) for an event, a training and a published project; escaping of a malicious title; 404 for unknown, deleted, disabled and unpublished items; legacy_escaped_fragment_route; listed bot on/gets the application.What was run locally (Ruby 3.2.2 container, PostgreSQL 13, Redis; no Elasticsearch, no compiled Webpack packs):
bin/rails test test/integration/social_share_test.rb: 7 tests, 48 assertions, 0 failures.test/integration/projects_test.rb(includes the existing "social bot endpoint never serves draft projects" test),test/meta/i18n_test.rb,test/integration/trainings_test.rb,test/integration/events/*: all green. Two event recurrence tests fail locally only withoutMAX_IMAGE_SIZEset (fixture image > 2 MB); they pass withMAX_IMAGE_SIZE=10485760and do not touch the changed code.rubocopon the changed Ruby files: onlyRails/ApplicationControlleronSocialBotController < ActionController::Base, which is pre-existing (the controller intentionally stays outsideApplicationController: no locale switching, CSRF cookie or Pundit needed).eslint(repository config) on the changed controllers: no new offense (events.js.erbwas checked with the ERB tags stubbed, its pre-existing offenses are unrelated).listed bots fetching the roottest stubs the Webpacker manifest, liketest/lib/mail_dependency_test.rb, because packs are not compiled in a clean checkout.