Server Side Rendering with meta tags in Angular [ANGULAR 2/4/5] [LARAVEL (PHP)]

Issues

When a post having open graph tags is shared on social media, they use crawlers that fetch relevant metadata.

The issue with AngularJS and dynamic metatags or you have a "single page app" with push state URLs (React, Angular, Ember, Vue, Preact etc...)

When you share your link relevant site bot make fetch meta data from that, but problem is most social sites don't execute javascript.In fact, they use the plain HTML as they see it.

Solution

When Facebook/Twitter does not want to evaluate Javascript, we have to serve it something it can digest. Therefore, I built a conditional redirect for these crawlers to a small script that emits the information.

Idea

(problem)
URL(https://frontend/user/id)==>Angular(javascript) API call==>backend(server method)==>Run script==>return response

Step 1 :bot hit url call frontend(angular or etc)page
Step 2 :angular page call API or backend route
Step 3 :backend return respons than bot get response or metadata

bot stop execution step 2 because javaseript

(Solution)
Here we are direct hit to server route

URL(https://backend/user/id)==>backend(server Route)===>Run script==>return plin html code (meta data)

Step 1 :bot hit URL call backend (server method)
Step 2 :backend return response(html + metadata) to bot.

Example


~~~~~~~~~~~~~~~~~~~~~Route~~~~~~~~~~~~~~~~~~~~~


1
Route::get('get-metadata/{id}', 'Web\path\to\metadtaController@getMetaData');


You will see a URL like this: - https://www.website.domain/webpath/get-metadata/{id}

~~~~~~~~~~~~~~~~~~~~~Controller~~~~~~~~~~~~~~~~~~~~~


use Illuminate\Http\RedirectResponse;


    public function getMetaData($id) {

        try {


            if (strpos($_SERVER["HTTP_USER_AGENT"], "facebookexternalhit/") !== false || strpos($_SERVER["HTTP_USER_AGENT"], "Facebot") !== false) {

                $userInfo = User::select('id', 'title', 'description', 'image_url')->find($id)->toArray();

                $fbData['url'] = 'webpath/get-metadata/' . $userInfo['id'];

                $html = '<!doctype html>';

                $html .= '<html>';

                $html .= '<head>';

                $html .= '<meta charset="utf-8">';
                
                $html .= '<meta name="twitter:card" content="summary" />';

                $html .= '<meta property = "og:title" content = "' . $userInfo['title'] . '"/>';

                $html .= '<meta property="og:type"  content="profile" />';

                $html .= '<meta property="og:description"  content="' . $userInfo['description'] . '" />';

                $html .= '<meta property="og:image"  content="' . $userInfo['image_url'] . '" />';

                $html .= '<meta property="og:url"  content="' . $fbData['url'] . '" />';

                $html .= '</head>';

                $html .= '<body></body>';

                $html .= '</html>';

                return view('facebook', ['code' => $html]);

            } else {

                //When user agent other then facebook bot clicks on your social media link then it will be redirect to this link(below mentioned)

                $redirectUrl = 'https://www.website.domain/(URL to public page goes here)' . $id;

                return new RedirectResponse($redirectUrl);

            }

        } catch (Exception $ex) {

            throw new Exception($ex);

        }

    }

~~~~~~~~~~~~~~~~~~~~~View~~~~~~~~~~~~~~~~~~~~~

//facebooktwitter.blade.php


<?php echo $code; ?>



Comments

Post a Comment