GitXplorerGitXplorer
b

link-preview-detector

public
2 stars
2 forks
0 issues

Commits

List of commits on branch main.
Verified
cd2274d68fe708ddc724dc5208a6f0635b481471

Update BlockList.json

bbrunoinds committed 2 years ago
Verified
241df408528932ab72b2c9daeccc62352579692a

Merge pull request #1 from dajuly20/main

bbrunoinds committed 3 years ago
Unverified
33148a2c03e460f213861dac147d246b525ae6c0

Fixed typo with filename. Linux IS case sensitive -.- Really naughty and hard to find bug.

ddajuly20 committed 3 years ago
Verified
d01563d891c55d1a32f812f5affe70e25774b833

Update README.md

bbrunoinds committed 4 years ago
Verified
e8a27dac4d0f4d6e3ee1a81a10052d4f8d463c5e

Update README.md

bbrunoinds committed 4 years ago
Verified
d30fa70cf5d7264fd0a694d888a4dd44f8e3468e

Add files via upload

bbrunoinds committed 4 years ago

README

The README file for this repository.

Detect when a request is for preview generation

Use-case:

This tool addresses a common issue in modern web development: How to detect if a request is for generating a link preview?

Suppose your webpage needs to generate link previews for platforms like Facebook, WhatsApp, and Twitter. These previews are generated using meta-tags, which can slow down your page load time. Ideally, you want to generate these meta-tags only when the request is made by a bot from these platforms, not when a regular user is opening the page.

This tool provides a simple function that allows you to determine whether a page request is for generating a link preview or just a regular page visit by a user. This way, you can optimize your page load time by generating link previews only when necessary.

How to install it?

You can install this package via composer:

composer require brunoinds/link-preview-detector

How to use it?

Method 1: Auto recognition

This method auto-capture the request origin and user-agent, and detects if is for link preview.

use Brunoinds\LinkPreviewDetector\LinkPreviewDetector;

$response = LinkPreviewDetector::isForLinkPreview();
//returns a boolean (true/false). If it is true, it means the request is coming from a link preview crawler.

Method 2: Manual recognition

With this method you can pass an specific User-Agent and will return if is a link preview crawler:

use Brunoinds\LinkPreviewDetector\LinkPreviewDetector;

$userAgent = $_SERVER['HTTP_USER_AGENT'];

$response = LinkPreviewDetector::isForLinkPreviewUserAgent($userAgent);

How to implement it (examples):

With the following example you can only send the metadata when it's required.

use Brunoinds\LinkPreviewDetector\LinkPreviewDetector;
$isForLinkPreview = LinkPreviewDetector::isForLinkPreview();

if ($isForLinkPreview)
{
  echo '<meta property="og:title" content="Your Website Title" />';
  echo '<meta property="og:description" content="Your Website Description" />';
  echo '<meta property="og:image" content="URL to the image" />';
  echo '<meta property="og:url" content="URL to your website" />';
}

One of the key benefits of this library is that it allows you to optimize your server's response based on the type of request. When a request is made specifically for link preview generation, you don't need to send the entire webpage content. Instead, you can just send the meta-data required for the link preview. This means you can avoid sending unnecessary data like JavaScript files, images, and other HTML content. This can significantly improve the performance of your server and the speed at which link previews are generated. Here's an example of how you can use this library to achieve this:

use Brunoinds\LinkPreviewDetector\LinkPreviewDetector;

$isForLinkPreview = LinkPreviewDetector::isForLinkPreview();

if ($isForLinkPreview) {
  // Send only the meta-data required for link preview
  echo '<meta property="og:title" content="Your Website Title" />';

} else {
  // Send the entire webpage
   echo '
    <!DOCTYPE html>
    <html>
      <head>
        <title>Your Website Title</title>
        <script src="https://raw.githubusercontent.com/brunoinds/link-preview-detector/main/your-script.js"></script>
      </head>
      <body>
        <h1>Welcome to My Website</h1>
        <p>This is a sample paragraph.</p>
      </body>
    </html>';
}