Skip to content

Commit

Permalink
docs: wording and style
Browse files Browse the repository at this point in the history
Co-authored-by: Angelo Ashmore <[email protected]>
  • Loading branch information
lihbr and angeloashmore committed May 3, 2023
1 parent 81ab2bf commit 0801785
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
14 changes: 7 additions & 7 deletions messages/endpoint-must-use-cdn.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
# `endpoint` must use CDN

`@prismicio/client` uses either a repository name or a repository endpoint to query content from Prismic.
`@prismicio/client` uses either a Prismic repository name or a Prismic Rest API v2 repository endpoint to query content from Prismic.

The repository name or repository endpoint must be provided when creating a `@prismicio/client` like the following:

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

// Using the repository name
const client = prismic.createClient("example")
const client = prismic.createClient("example-prismic-repo")

// Using the repository endpoint
const client = prismic.createClient("https://example.cdn.prismic.io/api/v2")
const client = prismic.createClient("https://example-prismic-repo.cdn.prismic.io/api/v2")
```

When creating a `@prismicio/client` with a repository endpoint, endpoint's subdomain must feature the `.cdn` suffix.
When creating a `@prismicio/client` with a repository endpoint, the endpoint's subdomain must feature the `.cdn` suffix.

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

// ✅ Correct
const client = prismic.createClient("https://example.cdn.prismic.io/api/v2")
const client = prismic.createClient("https://example-prismic-repo.cdn.prismic.io/api/v2")

// ❌ Incorrect
const client = prismic.createClient("https://example.prismic.io/api/v2")
const client = prismic.createClient("https://example-prismic-repo.prismic.io/api/v2")
```

Not using the `.cdn` version of your repository endpoint can have unexpected side-effects and cause performance issues when query Prismic.
Not using the `.cdn` version of your repository endpoint can have unexpected side-effects and cause performance issues when querying Prismic.
10 changes: 3 additions & 7 deletions src/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,10 +504,7 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
constructor(repositoryNameOrEndpoint: string, options: ClientConfig = {}) {
if (isRepositoryEndpoint(repositoryNameOrEndpoint)) {
if (process.env.NODE_ENV === "development") {
/**
* Matches non-API v2 `.prismic.io` endpoints
* {@link https://regex101.com/r/xRsavu/1}
*/
// Matches non-API v2 `.prismic.io` endpoints, see: https://regex101.com/r/xRsavu/1
if (/\.prismic\.io\/(?!api\/v2\/?)/i.test(repositoryNameOrEndpoint)) {
throw new PrismicError(
"@prismicio/client only supports Prismic Rest API V2. Please provide only the repository name to the first createClient() parameter or use the getRepositoryEndpoint() helper to generate a valid Rest API V2 endpoint URL.",
Expand All @@ -519,9 +516,8 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
const hostname = new URL(
repositoryNameOrEndpoint,
).hostname.toLowerCase();
/**
* Matches non-.cdn `.prismic.io` endpoints
*/

// Matches non-.cdn `.prismic.io` endpoints
if (
hostname.endsWith(".prismic.io") &&
!hostname.endsWith(".cdn.prismic.io")
Expand Down
4 changes: 2 additions & 2 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ it("constructor throws if a prismic.io endpoint is given that is not for Rest AP
prismic.createClient(prismic.getRepositoryEndpoint("qwerty"), { fetch });
}, "An endpoint created with getRepositoryEndpoint does not throw").not.toThrow();

process.env.NODE_ENV === originalNodeEnv;
process.env.NODE_ENV = originalNodeEnv;
});

it("constructor warns if a non-.cdn prismic.io endpoint is given", () => {
Expand Down Expand Up @@ -146,7 +146,7 @@ it("constructor warns if a non-.cdn prismic.io endpoint is given", () => {

consoleWarnSpy.mockRestore();

process.env.NODE_ENV === originalNodeEnv;
process.env.NODE_ENV = originalNodeEnv;
});

it("constructor throws if fetch is unavailable", () => {
Expand Down

0 comments on commit 0801785

Please sign in to comment.