0

I have seen many solutions for sharing on LinkedIn/Facebook. But in those solutions, the content being shared is static, that is the content we are mentioning in OG tags of our index.html file.

But my pages are being rendered on the client side and I want to share different content every time on user action.

anyway for that I can share new content every time with javascript.?

Scenario- I have 10 rows on the web page and every row has a social share button, on the click of which user is sharing the row specific content on social media. I am working with angular framework.

Page reload/redirect is not suggested

1 Answer 1

0

I had the same problem, I wanted to share the user's score in the quote section of the facebook sharing link. Unfortunately, in 2017 Meta deprecated the ability to edit previews attached to link posts: here's the article

However, I found a workaround, although is not very pretty. I'm using Next.JS with next-share in my project, but you can probably achieve the same with other frameworks:

I created a dynamic route with the data I wanted to share as a parameter, and used it to make the content string for the meta tag og:description. I then used the url of the dynamic route for the share button, this way the facebook crawler is reading the dynamic description. You can't use redirection though, I had to make some sort of landing page with a button that redirects the user to the desired page. Here's the code:

dummy page:

// /app/[phrase]/page.jsx
import FacebookLandingPage from '../../components/FacebookLandingPage.jsx'

export default function Phrase({ params }) {
    const gameData = params.phrase.split("%2C");
    const difficulty = gameData[1] == "Easy" || gameData[1] == "Medium" || gameData[1] == "Hard" || gameData[1] == "Expert" ? gameData[1] : "";
    let minutes;
    let seconds;
    let attempts;
    
    if (gameData[0] == "crack-the-code" 
        && parseInt(gameData[2]) 
        && parseInt(gameData[2]) < 10) {
        attempts = gameData[2]
    } else if (gameData[0] == "sudoku" 
        && parseInt(gameData[2]) 
        && parseInt(gameData[3])) {
        minutes = gameData[2]
        seconds = gameData[3];
    }
    
    const img = params.phrase.match("sudoku") ? "sudoku" : params.phrase.match("crack-the-code") ? "crack-the-code" : "";
    const sudokuPhrase = `I completed a${difficulty == "Easy" || difficulty == "Expert" ? "n" : ""} ${difficulty} Sudoku in ${minutes} minute${minutes != 1 ? "s" : ""} and ${seconds} second${seconds != 1 ? "s" : ""}! Can you do better?`;
    const ctcPhrase = `I cracked a${difficulty == "Easy" || difficulty == "Expert" ? "n" : ""} ${difficulty} code in ${difficulty == "Easy" ? 7 - attempts : difficulty == "Medium" ? 8 - attempts : difficulty == "Hard" ? 9 - attempts : 10 - attempts} attempts! Can you do better?`;
    const sharePhrase = gameData[0] == "crack-the-code" ? ctcPhrase : gameData[0] == "sudoku" ? sudokuPhrase : "";
    
    return (
        <>
            <meta 
                property='og:image' 
                content={`https://puzzle-challenge.vercel.app/images/${img}.png`}
                />
            <meta 
                property='og:description' 
                content={sharePhrase}
            />
            <FacebookLandingPage/>
        </>
    )
}

then, in the game page:

<FacebookShareButton 
    url={`https://puzzle-challenge.vercel.app/sudoku,${difficulty},${minutes},${seconds}`}
    hashtag={'#sudoku'}
>
    <FacebookIcon size={32} round className='my-1'/>
</FacebookShareButton>
Sign up to request clarification or add additional context in comments.

2 Comments

"then I used the parameter itself as content for the meta tag og:description" - without any checking of the value, this is a bit "dangerous" of course ... No one is stopping me from sharing https://puzzle-challenge.vercel.app/Your%20Mama%20hunts%20Sasquatch%20naked now, and people will think this comes "from" your app ;-)
@Stelly, Thanks for your answer, This will definitely work, if we redirect, but in my case page reload is not allowed, Scenario- I have 10 rows on the web page and every row has a social share button, on the click of which user is sharing the row specific content on social media

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.