Skip to content

Next.js Integration

These instructions assume you’ve already created a JamComments account as well as a “site” within that account. If you haven’t, learn more about that process on the Getting Started page.

Prerequisites

In order to use this plugin, you’ll need a JamComments account, where you’ll also need to have created a site and generated an API key.

Installation

Terminal window
npm install @jam-comments/next

Usage

In your getStaticProps or getServerSideProps hook, retrieve the comments for a given post by using the fetchMarkup method from @jam-comments/next/server, and then passing your API key, domain, and comment data to your rendered page, which should then be passed to the <JamComments /> component from @jam-comments/next.

[slug].js
import { JamComments } from "@jam-comments/next";
export default function Post({ content, commentData }) {
return (
<article>
<div dangerouslySetInnerHTML={{ __html: content }}></div>
<JamComments markup={commentData} />
</article>
);
}
export async function getStaticProps({ params }) {
const content = await getContentFromSomewhere();
const { fetchMarkup } = await import("@jam-comments/next/server");
const blogPostingSchema = {
"@context": "https://schema.org",
"@type": "BlogPosting",
headline: post.title,
datePublished: post.date,
};
// Retrieve all comments already made on this post.
const commentData = await fetchMarkup({
domain: process.env.JAM_COMMENTS_DOMAIN,
apiKey: process.env.JAM_COMMENTS_API_KEY,
path: `/posts/${params.slug}`,
schema: blogPostingSchema,
});
// Pass domain, API key, and comments to `props` for use client-side.
return {
props: {
commentData,
content,
},
};
}

Overriding Copy in UI

The JamComments UI comes with its own set of copy for its components (submission confirmation, submit button text, etc.). Many of this copy can be overridden.

PropertyWhere It AppearsDefault
confirmationMessageIn the success banner after submitting a comment.”Comment successfully submitted!”
submitButtonIn the comment submission button.”Submit”
namePlaceholderIn the “name” input.(empty)
emailPlaceholderIn the “email” input.(empty)
commentPlaceholderIn the comment textarea.”Write something in plain text or Markdown…”
writeTabIn the tab for composing a comment.”Write”
previewTabIn the tab for previewing a comment.”Preview”
authButtonIn the link for signing in or registering.”Log In or Register”
logOutButtonIn the link for logging out.”Log Out”

You can pass these values in the copy prop:

const commentData = await fetchMarkup({
domain: process.env.JAM_COMMENTS_DOMAIN,
apiKey: process.env.JAM_COMMENTS_API_KEY,
path: `/posts/${params.slug}`,
copy: {
commentPlaceholder: "Write something",
submitButton: "Send it!",
namePlaceholder: "Ur Mom",
emailPlaceholder: "[email protected]",
},
});

Environment Configuration

In non-production mode, this plugin will render a list of dummy comments on your pages, making it easier to adjust styles before deploying to production. Additionally, any new comments will be silently submitted.

In order to remove these dummy comments and allow submissions to go through, either the NODE_ENV or JAM_COMMENTS_ENVIRONMENT environment variables must be set to production.

Setting a Date Format

The default date format for comments is 'm/d/Y. If you’re from another country, that might be weird. So, you can customize it by using the dateFormat option. Note that the format must follow PHP’s DateTime standards.

const commentData = await fetchMarkup({
domain: process.env.JAM_COMMENTS_DOMAIN,
apiKey: process.env.JAM_COMMENTS_API_KEY,
path: `/posts/${params.slug}`,
dateFormat: "l, F j, Y", // Thursday, October 31, 2024
});

Contributions

The source for this plugin is open to contributions. If you have a bug fix or idea for improvement, leave a pull request or issue in the GitHub repository.