Skip to content

(bug) Link previews for shared events, trainings and projects - #529

Open
atcen wants to merge 3 commits into
sleede:devfrom
atcen:fix/social-share-previews
Open

atcen wants to merge 3 commits into
sleede:devfrom
atcen:fix/social-share-previews

Conversation

@atcen

@atcen atcen commented Nov 21, 2026

Copy link
Copy Markdown

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

  • Shared URLs look like 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.
  • SocialBotController only 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.
  • For the listed bots, GET / without _escaped_fragment_ returned a 404.
  • In the legacy meta tags, og:image contained a double slash (root_url + image.url → https://host//uploads/…) and og:url pointed back to the #! URL, so Facebook re-scraped the generic page.

Solution

  1. Fragment-free share URLs, independent of the user agent: GET /events/:id, GET /trainings/:id (slug) and GET /projects/:id (slug). They render a small HTML page with:
    • OpenGraph and Twitter Card meta tags: title, plain-text description (HTML stripped, whitespace normalized, truncated to 300 characters on a word boundary), absolute image URL (the large version, with its dimensions), og:url = the clean share URL, og:type, og:site_name (fablab_name setting), fb:app_id / twitter:site when configured, and <link rel="canonical">;
    • an immediate 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.
    • I deliberately did not add a <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.
    • 404 for unknown records, soft-deleted events, disabled trainings and unpublished projects (same rule as the API / the existing draft protection).
    • Everything is escaped by ERB; the redirect target is JSON-encoded and json_escaped.
    • These paths do not collide with existing routes (API routes live under /api, /open_api, /rss). format: false is set so that e.g. /projects/foo.json is a 404 instead of a missing template error.
  2. Legacy bot route kept for old shared links. It now only matches when _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 clean og:url. The three near-identical templates were merged into one social_bot/show.html.erb.
  3. Frontend: the "Share on Facebook" / "Share on X" buttons of events and projects now share the clean URL (trainings have no share buttons). A "Copy link" button copies the clean URL (navigator.clipboard, with a window.prompt fallback when the clipboard API is unavailable, e.g. on plain HTTP) and shows a growl notification, like the existing iCal "copy link" feature.
  4. i18n: two new keys (copy_link, link_copied) under app.public.events_show and app.public.projects_show, added to every app.public.*.yml locale file except the Crowdin pseudo-locale zu (it only contains Crowdin ids). en and fr keep the same key layout as required by test/meta/i18n_test.rb.
  5. CHANGELOG updated in "Next release".

Absolute URLs use DEFAULT_HOST / DEFAULT_PROTOCOL when they are set (as ApplicationController does), else the request host.

How to test

# Any user agent now gets the meta tags (WhatsApp example)
curl -s -A 'WhatsApp/2.23.20.0 A' https://your-instance/events/439 | grep -E 'og:|twitter:|canonical'
curl -s -A 'WhatsApp/2.23.20.0 A' https://your-instance/trainings/some-training-slug | grep og:
curl -s -A 'WhatsApp/2.23.20.0 A' https://your-instance/projects/some-project-slug | grep og:

# Unknown / unpublished items
curl -s -o /dev/null -w '%{http_code}\n' https://your-instance/events/999999   # 404

# Legacy links still work, image URL has no double slash anymore, og:url is the clean URL
curl -s -A 'facebookexternalhit/1.1' 'https://your-instance/?_escaped_fragment_=/events/439' | grep -E 'og:(url|image)'

# Listed bots fetching the root now get the application instead of a 404
curl -s -o /dev/null -w '%{http_code}\n' -A 'facebookexternalhit/1.1' https://your-instance/   # 200

In a browser, opening https://your-instance/events/439 immediately lands on https://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 without MAX_IMAGE_SIZE set (fixture image > 2 MB); they pass with MAX_IMAGE_SIZE=10485760 and do not touch the changed code.
  • rubocop on the changed Ruby files: only Rails/ApplicationController on SocialBotController < ActionController::Base, which is pre-existing (the controller intentionally stays outside ApplicationController: no locale switching, CSRF cookie or Pundit needed).
  • eslint (repository config) on the changed controllers: no new offense (events.js.erb was checked with the ERB tags stubbed, its pre-existing offenses are unrelated).
  • Not run: the full test suite, the Jest suite, and a manual browser check of the new "Copy link" button and of real previews in WhatsApp/Telegram/etc. The listed bots fetching the root test stubs the Webpacker manifest, like test/lib/mail_dependency_test.rb, because packs are not compiled in a clean checkout.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant