Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add getToolbarSrc() helper #281

Merged
merged 1 commit into from
Apr 7, 2023
Merged

feat: add getToolbarSrc() helper #281

merged 1 commit into from
Apr 7, 2023

Conversation

angeloashmore
Copy link
Member

@angeloashmore angeloashmore commented Apr 6, 2023

Types of changes

  • Chore (a non-breaking change which is related to package maintenance)
  • Bug fix (a non-breaking change which fixes an issue)
  • New feature (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Description

This PR adds a getToolbarSrc() helper that returns a Prismic repository's Prismic Toolbar script URL.

import * as prismic from "@prismicio/client";

const toolbarSrc = prismic.getToolbarSrc("my-repo-name");
// => https://static.cdn.prismic.io/prismic.js?new=true&repo=my-repo-name

Motivation

This helper will replace @prismicio/react's <PrismicToolbar> when used in React Server Components environments; in Server Components environments, <script> elements should be rendered on the server, which <PrismicToolbar> is not able to do.

Providing getToolbarSrc() allows a developer to get the Prismic Toolbar's script URL and render the <script> using their framework's conventions. In a Next.js app, for example, a developer would use next/script.

import * as prismic from "@prismicio/client";
import Script from "next/script";

export default function Page() {
  const toolbarSrc = prismic.getToolbarSrc("my-repo-name");

  return (
    <main>
      <Script src={toolbarSrc} />
    </main>
  );
}

Checklist:

  • My change requires an update to the official documentation.
  • All TSDoc comments are up-to-date and new ones have been added where necessary.
  • All new and existing tests are passing.

🐋

@angeloashmore angeloashmore requested a review from lihbr April 6, 2023 19:23
Copy link
Member

@lihbr lihbr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, don't want to add to the scope if this PR (I can do it later), but any thoughts on adding preview helpers too? + maybe we could expose getPreviewCookie()

Something to stop us from reimplementing this code everytime:

import { getPreviewCookie } from "./lib/getPreviewCookie";

export const isPreviewSession = (cookieJar: string): boolean => {
	const previewCookie = getPreviewCookie(cookieJar);

	if (!previewCookie) {
		return false;
	}

	const session = JSON.parse(decodeURIComponent(previewCookie));

	return Object.keys(session).some((key) => {
		return key in session &&
			typeof session[key] === 'object' &&
			session[key] !== null &&
			'preview' in session[key] &&
			session[key]
	});
};

@angeloashmore
Copy link
Member Author

any thoughts on adding preview helpers too? + maybe we could expose getPreviewCookie()

Yes, let's do it! @samlfair suggested a similar thing here as well: #268

Those additions are outside the scope of this PR, as you mentioned. Let's add these helpers in a separate PR:

  • isPreviewSessionActive()
  • getPreviewCookie()

(We may need to iterate on the names, but it's at least clear what they should do.)

@angeloashmore angeloashmore merged commit f0e768c into v7 Apr 7, 2023
@angeloashmore angeloashmore deleted the aa/v7-getToolbarSrc branch April 7, 2023 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants