DEV Community

Cover image for How to add Astro social share to your Astro application
Femi Akinyemi
Femi Akinyemi

Posted on

How to add Astro social share to your Astro application

Introduction

Astro is a modern web framework that combines the best features of static site generators (SSGs) and server-side rendering (SSR) frameworks. It is designed to be fast, flexible, and easy to use, making it an excellent choice for building various websites, including blogs, portfolios, e-commerce sites, and more.

Astro offers several benefits, including:

  • Speed: Astro websites are typically very fast, thanks to server-side rendering and component islands.

  • SEO: Astro websites are also well-suited for SEO, as they are static and can be pre-rendered.

  • Flexibility: Astro is a very flexible framework, and many websites can be built.

  • User-friendliness: Astro is beginner-friendly and offers a gentle learning curve, making it accessible even for those new to web development.

One of the benefits of using Astro is that it makes it easy to add social sharing buttons to your website. Social sharing is essential because social sharing is a great way to increase traffic to your website and promote your content.

Social sharing is essential for web applications because it can help to:

  • Boost website traffic
  • Content promotion
  • Establish brand presence
  • Lead generation and sales

This article explains how to add social sharing buttons to your Astro application.

In this article, you will build an application that displays random jokes using the random dad joke API with a social share button using Astro social share.

The source code for the application is readily available here on GitHub.

By the end of this article, you will have a better understanding of:

  • What is Astro social share?
  • How to install Astro social share
  • How to use Astro social share # Understanding Astro social share

Astro social share is your Astro site's social media share button. It's also one of the recently added Astro integrations.

Astro social share comes with some good features, which are:

  • No dependencies or external scrips
  • Share buttons for:
    • Facebook
    • Hacker News
    • LinkedIn
    • Reddit
    • Twitter
  • Icons included from https://simpleicons.org/

    Prerequisites

To fully grasp the concepts presented in this article, the following are required:

  • Basic understanding of JavaScript
  • Node.js installed, version v18.14.1 or later.
  • Code Editor - Preferably VS Code or another code editor. # Bootstrapping Astro application

To bootstrap your Astro application, you will use the create astro setup wizard following the steps below:

  • Install Astro by running the following command in your terminal:

npm create astro@latest

  • Confirm your choice by typing 'y' to install the create-astro package.

  • When prompted to specify the location for your new project, enter the folder name where you'd like to create a new project directory. For example, you can type. /Astro-Social-Share.

  • You'll be presented with a list of starter templates. Use the arrow keys (up and down) to navigate to the "Empty" template, and then press the Return or Enter key to select it.

  • When asked if you want to install project dependencies, type y.

  • If you are asked whether you plan on using TypeScript, type n.

  • When prompted about initializing a new git repository, type y.

Now, you can navigate to your Astro-Social-Share directory to begin and preview your application by running the command below in your terminal:

cd cd Astro-Social-Share

npm run dev
Enter fullscreen mode Exit fullscreen mode

Click the localhost link within your terminal window to view a real-time preview of your brand-new Astro website!

This is how the browser preview of the Astro 'Empty Project' starter website should appear:

Astro Preview Page

Setting up API page

Now, you need to set up the Home Page. The home page will be the entry point for your application's home page. In the src/pages/index.astro, paste the code below:


    ---
    import {GET} from "../pages/api/fetchdata";

    import { TwitterShareButton, SocialShare } from "astro-social-share";
    const TwitterBUTTON = [TwitterShareButton];

    let response = await GET();
    const data = await response.json();
    ---

    <html lang="en">
      <body>
        <div class="h-screen flex flex-col items-center">
          <h1 class="text-3xl font-bold mt-12 mb-12">Share Your Daily Joke on X</h1>

          <p
            id="joke-btn"
            class="bg-blue-500 hover-bg-blue-700 text-white font-bold py-2 px-4 rounded">
            Click on the X icon below to share your daily joke
          </p>

          <div class="mt-12" id="joke-container">{data.joke}</div>

          <SocialShare
            buttons={TwitterBUTTON}
            description={data?.joke}
            via="akinyemi_t"
            title={data.joke}
          />
        </div>
      </body>
    </html>

Enter fullscreen mode Exit fullscreen mode

Here is the explanation of the code provided above:

  • The first line imports the GET function from a file at ../pages/api/fetchdata. It suggests that you are making a GET request to fetch some data.

  • Next, you import components related to social sharing using Astro. Specifically, you import the TwitterShareButton and SocialShare components from the astro-social-share library.

  • You define TwitterBUTTON as an array containing the TwitterShareButton. This variable plays a role later when configuring social sharing.

  • The code then awaits the result of the GET request, assigns it to the response variable, and parses the JSON content from the response into the data variable.

  • The HTML section starts with the opening <html> tag and defines the web page's structure.

  • Inside the <body> tag, the content of the web page looks as follows:

    • A <div> element with the class h-screen flex flex-col items-center is used to center the content vertically in the viewport.
    • An <h1> element with the text Share Your Daily Joke on X is displayed at the top of the page.
    • A <p> element with the ID joke-btn displays a message encouraging the user to click an X icon below to share a daily joke.
    • Another <div> element with the ID joke-container displays the content of the data.joke property, which contains the daily joke fetched from the API.
    • The <SocialShare> component enables sharing on social media. Configured with the buttons prop set to TwitterBUTTON (likely to display a Twitter share button), the description assigned to the data.joke set to akinyemi_t (the Twitter username), and the joke's title. Text.

Testing and debugging

Follow the steps below to test your application:

Navigate into your directory and run

npm run dev
Enter fullscreen mode Exit fullscreen mode

Your application is now running at localhost:4321

To view your homepage, visit localhost:4321 in your browser. You should see the homepage.

Final Demo Page

Now, you can share your joke on social media by clicking the X icon.

Image description

What's next?

There are so many other things you can do with Astro social share. You can integrate other social buttons, like

  • Facebook
  • Hacker News
  • LinkedIn
  • Reddit
  • Twitter

Also, you can make the application more interactive by adding more features like changing the jokes.

Conclusion

Astro is a fast and flexible web framework suitable for various website types. Adding social sharing buttons to your Astro application enhances traffic, content promotion, brand presence, and lead generation.

This article has shown how to integrate social sharing buttons using the Astro social share component, using a random joke application as an example.

The provided GitHub source code serves as a practical reference.

By following the article, you've gained the ability to empower your web applications with Astro social share, making your content more shareable and engaging.

Top comments (0)