Skip to content

Commit

Permalink
feat(next-drupal): remove unneeded previewSecret option/env variable
Browse files Browse the repository at this point in the history
Fixes #661
  • Loading branch information
JohnAlbin committed Feb 29, 2024
1 parent d69ca0d commit 08d55f3
Show file tree
Hide file tree
Showing 27 changed files with 20 additions and 114 deletions.
3 changes: 1 addition & 2 deletions examples/example-blog/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ NEXT_IMAGE_DOMAIN=localhost
DRUPAL_CLIENT_ID=52ce1a10-bf5c-4c81-8edf-eea3af95da84
DRUPAL_CLIENT_SECRET=SA9AGbHnx6pOamaAus2f9LG9XudHFjKs
DRUPAL_SITE_ID=1ccd4bc4-bbc6-4c19-a816-c7d100feff02
DRUPAL_PREVIEW_SECRET=secret
DRUPAL_FRONT_PAGE=/home
DRUPAL_FRONT_PAGE=/home
3 changes: 1 addition & 2 deletions examples/example-client/.env.example
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Required
NEXT_PUBLIC_DRUPAL_BASE_URL=http://localhost:8080
NEXT_IMAGE_DOMAIN=localhost
DRUPAL_PREVIEW_SECRET=
DRUPAL_CLIENT_ID=
DRUPAL_CLIENT_SECRET=

# Optional
DRUPAL_SITE_ID=
DRUPAL_FRONT_PAGE=/home
DRUPAL_JSONAPI_PREFIX=/jsonapi
DRUPAL_JSONAPI_PREFIX=/jsonapi
3 changes: 0 additions & 3 deletions examples/example-graphql/.env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# See https://next-drupal.org/docs/environment-variables
NEXT_PUBLIC_DRUPAL_BASE_URL=https://dev.next-drupal.org
NEXT_IMAGE_DOMAIN=dev.next-drupal.org

# Required for Preview Mode
DRUPAL_PREVIEW_SECRET=secret
1 change: 0 additions & 1 deletion examples/example-graphql/lib/drupal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export const drupal = new DrupalClient(
clientId: process.env.DRUPAL_CLIENT_ID,
clientSecret: process.env.DRUPAL_CLIENT_SECRET,
},
previewSecret: process.env.DRUPAL_PREVIEW_SECRET,
}
)

Expand Down
6 changes: 3 additions & 3 deletions examples/example-graphql/pages/api/revalidate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NextApiRequest, NextApiResponse } from "next"
import type { NextApiRequest, NextApiResponse } from "next"

export default async function handler(
request: NextApiRequest,
Expand All @@ -8,7 +8,7 @@ export default async function handler(
const secret = request.query.secret as string

// Validate secret.
if (secret !== process.env.DRUPAL_PREVIEW_SECRET) {
if (secret !== process.env.DRUPAL_REVALIDATE_SECRET) {
return response.status(401).json({ message: "Invalid secret." })
}

Expand All @@ -23,7 +23,7 @@ export default async function handler(
return response.json({})
} catch (error) {
return response.status(404).json({
message: error.message,
message: (error as Error).message,
})
}
}
3 changes: 1 addition & 2 deletions examples/example-marketing/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ NEXT_IMAGE_DOMAIN=localhost
DRUPAL_CLIENT_ID=52ce1a10-bf5c-4c81-8edf-eea3af95da84
DRUPAL_CLIENT_SECRET=SA9AGbHnx6pOamaAus2f9LG9XudHFjKs
DRUPAL_SITE_ID=example_marketing
DRUPAL_PREVIEW_SECRET=secret
DRUPAL_FRONT_PAGE=/home
DRUPAL_FRONT_PAGE=/home
1 change: 0 additions & 1 deletion examples/example-marketing/lib/drupal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export const drupal = new DrupalClient(
process.env.NEXT_PUBLIC_DRUPAL_BASE_URL,
{
frontPage: "/home",
previewSecret: "secret",
auth: {
clientId: process.env.DRUPAL_CLIENT_ID,
clientSecret: process.env.DRUPAL_CLIENT_SECRET,
Expand Down
6 changes: 3 additions & 3 deletions examples/example-marketing/pages/api/revalidate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NextApiRequest, NextApiResponse } from "next"
import type { NextApiRequest, NextApiResponse } from "next"

export default async function handler(
request: NextApiRequest,
Expand All @@ -8,7 +8,7 @@ export default async function handler(
const secret = request.query.secret as string

// Validate secret.
if (secret !== process.env.DRUPAL_PREVIEW_SECRET) {
if (secret !== process.env.DRUPAL_REVALIDATE_SECRET) {
return response.status(401).json({ message: "Invalid secret." })
}

Expand All @@ -28,7 +28,7 @@ export default async function handler(
return response.json({})
} catch (error) {
return response.status(404).json({
message: error.message,
message: (error as Error).message,
})
}
}
3 changes: 1 addition & 2 deletions examples/example-umami/.env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
NEXT_PUBLIC_DRUPAL_BASE_URL=http://localhost:8080
NEXT_IMAGE_DOMAIN=localhost
DRUPAL_PREVIEW_SECRET=secret
NEXTAUTH_SECRET=
NEXTAUTH_URL=http://localhost:3000
DRUPAL_CLIENT_ID=
DRUPAL_CLIENT_SECRET=
DRUPAL_CLIENT_SECRET=
7 changes: 1 addition & 6 deletions examples/example-umami/lib/drupal.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import { DrupalClient } from "next-drupal"

export const drupal = new DrupalClient(
process.env.NEXT_PUBLIC_DRUPAL_BASE_URL,
{
previewSecret: process.env.DRUPAL_PREVIEW_SECRET,
}
)
export const drupal = new DrupalClient(process.env.NEXT_PUBLIC_DRUPAL_BASE_URL)
7 changes: 0 additions & 7 deletions modules/next/src/Controller/NextSiteEntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,6 @@ public function environmentVariables(NextSiteInterface $next_site) {
'DRUPAL_CLIENT_SECRET' => 'Retrieve this from /admin/config/services/consumer',
];

if ($secret = $next_site->getPreviewSecret()) {
$variables += [
'preview_variables' => '# Required for Draft Mode',
'DRUPAL_PREVIEW_SECRET' => $secret,
];
}

if ($revalidate_secret = $next_site->getRevalidateSecret()) {
$variables += [
'revalidate_variables' => '# Required for On-demand Revalidation',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public function testEnvironmentVariables() {

$this->assertEquals(\Drupal::requestStack()->getCurrentRequest()->getSchemeAndHttpHost(), $build['container']['NEXT_PUBLIC_DRUPAL_BASE_URL']['#context']['value']);
$this->assertEquals(\Drupal::requestStack()->getCurrentRequest()->getHost(), $build['container']['NEXT_IMAGE_DOMAIN']['#context']['value']);
$this->assertEquals($this->nextSite->getPreviewSecret(), $build['container']['DRUPAL_PREVIEW_SECRET']['#context']['value']);
$this->assertEquals($this->nextSite->getRevalidateSecret(), $build['container']['DRUPAL_REVALIDATE_SECRET']['#context']['value']);
}

Expand All @@ -63,12 +62,12 @@ public function testEnvironmentVariables() {
*/
public function testOverriddenEnvironmentVariables() {
$GLOBALS['config']['next.next_site.' . $this->nextSite->id()] = [
'preview_secret' => 'overridden',
'revalidate_secret' => 'overridden',
];
$overridden_entity = NextSite::load($this->nextSite->id());
$controller = NextSiteEntityController::create($this->container);
$build = $controller->environmentVariables($overridden_entity);
$this->assertEquals('overridden', $build['container']['DRUPAL_PREVIEW_SECRET']['#context']['value']);
$this->assertEquals('overridden', $build['container']['DRUPAL_REVALIDATE_SECRET']['#context']['value']);
}

}
4 changes: 0 additions & 4 deletions packages/next-drupal/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ export class DrupalClient {

private withAuth?: DrupalClientOptions["withAuth"]

private previewSecret?: DrupalClientOptions["previewSecret"]

/**
* Instantiates a new DrupalClient.
*
Expand All @@ -142,7 +140,6 @@ export class DrupalClient {
withAuth = DEFAULT_WITH_AUTH,
fetcher,
auth,
previewSecret,
accessToken,
throwJsonApiErrors = true,
} = options
Expand All @@ -158,7 +155,6 @@ export class DrupalClient {
this.headers = headers
this.logger = logger
this.withAuth = withAuth
this.previewSecret = previewSecret
this.cache = cache
this.accessToken = accessToken
this.throwJsonApiErrors = throwJsonApiErrors
Expand Down
10 changes: 0 additions & 10 deletions packages/next-drupal/src/types/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,6 @@ export type DrupalClientOptions = {
*/
useDefaultResourceTypeEntry?: boolean

/**
* The secret to use for preview mode.
*
* * **Default value**: `null`
* * **Required**: *No*
*
* [Documentation](https://next-drupal.org/docs/client/configuration#previewsecret)
*/
previewSecret?: string

/**
* A long-lived access token you can set for the client.
*
Expand Down
2 changes: 2 additions & 0 deletions packages/next-drupal/tests/DrupalClient/basic-methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { DrupalClient, JsonApiErrors } from "../../src"
import { BASE_URL, mockLogger, spyOnFetch, spyOnFetchOnce } from "../utils"
import type { DrupalNode, JsonApiError, Serializer } from "../../src"

jest.setTimeout(10000)

afterEach(() => {
jest.restoreAllMocks()
})
Expand Down
16 changes: 0 additions & 16 deletions packages/next-drupal/tests/DrupalClient/constructor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,22 +247,6 @@ describe("options parameter", () => {
})
})

describe("previewSecret", () => {
test("defaults to `undefined`", () => {
const client = new DrupalClient(BASE_URL)
expect(client.previewSecret).toBe(undefined)
})

test("sets up a custom previewSecret", () => {
const customPreviewSecret = "custom-secret-value"

const client = new DrupalClient(BASE_URL, {
previewSecret: customPreviewSecret,
})
expect(client.previewSecret).toBe(customPreviewSecret)
})
})

describe("serializer", () => {
test("defaults to `new Jsona()`", () => {
const client = new DrupalClient(BASE_URL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { DRAFT_DATA_COOKIE_NAME, DrupalClient } from "../../src"
import { BASE_URL, mockLogger, mocks, spyOnFetch } from "../utils"
import type { DrupalNode, JsonApiResourceWithPath } from "../../src"

jest.setTimeout(10000)

afterEach(() => {
jest.restoreAllMocks()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { DrupalClient } from "../../src"
import { BASE_URL, mocks, spyOnFetch } from "../utils"
import type { DrupalNode, DrupalSearchApiJsonApiResponse } from "../../src"

jest.setTimeout(10000)

afterEach(() => {
jest.restoreAllMocks()
})
Expand Down
3 changes: 0 additions & 3 deletions starters/basic-starter/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@ NEXT_IMAGE_DOMAIN=site.example.com
DRUPAL_CLIENT_ID=Retrieve this from /admin/config/services/consumer
DRUPAL_CLIENT_SECRET=Retrieve this from /admin/config/services/consumer

# Required for Preview Mode
DRUPAL_PREVIEW_SECRET=Retrieve this from /admin/config/services/next

# Required for On-demand Revalidation
DRUPAL_REVALIDATE_SECRET=Retrieve this from /admin/config/services/next
2 changes: 1 addition & 1 deletion starters/basic-starter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A simple starter for building your site with Next.js and Drupal.

## Deploy to Vercel

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fchapter-three%2Fnext-drupal-basic-starter&env=NEXT_PUBLIC_DRUPAL_BASE_URL,NEXT_IMAGE_DOMAIN,DRUPAL_PREVIEW_SECRET,DRUPAL_CLIENT_ID,DRUPAL_CLIENT_SECRET&envDescription=Learn%20more%20about%20environment%20variables&envLink=https%3A%2F%2Fnext-drupal.org%2Fdocs%2Fenvironment-variables&project-name=next-drupal&demo-title=Next.js%20for%20Drupal&demo-description=A%20next-generation%20front-end%20for%20your%20Drupal%20site.&demo-url=https%3A%2F%2Fdemo.next-drupal.org&demo-image=https%3A%2F%2Fnext-drupal.org%2Fimages%2Fdemo-screenshot.jpg)
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fchapter-three%2Fnext-drupal-basic-starter&env=NEXT_PUBLIC_DRUPAL_BASE_URL,NEXT_IMAGE_DOMAIN,DRUPAL_CLIENT_ID,DRUPAL_CLIENT_SECRET&envDescription=Learn%20more%20about%20environment%20variables&envLink=https%3A%2F%2Fnext-drupal.org%2Fdocs%2Fenvironment-variables&project-name=next-drupal&demo-title=Next.js%20for%20Drupal&demo-description=A%20next-generation%20front-end%20for%20your%20Drupal%20site.&demo-url=https%3A%2F%2Fdemo.next-drupal.org&demo-image=https%3A%2F%2Fnext-drupal.org%2Fimages%2Fdemo-screenshot.jpg)

## Documentation

Expand Down
2 changes: 0 additions & 2 deletions starters/basic-starter/lib/drupal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ import { DrupalClient } from "next-drupal"
const baseUrl: string = process.env.NEXT_PUBLIC_DRUPAL_BASE_URL || ""
const clientId = process.env.DRUPAL_CLIENT_ID || ""
const clientSecret = process.env.DRUPAL_CLIENT_SECRET || ""
const previewSecret = process.env.DRUPAL_PREVIEW_SECRET

export const drupal = new DrupalClient(baseUrl, {
auth: {
clientId,
clientSecret,
},
previewSecret,
})
3 changes: 0 additions & 3 deletions starters/graphql-starter/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@ NEXT_IMAGE_DOMAIN=site.example.com
DRUPAL_CLIENT_ID=Retrieve this from /admin/config/services/consumer
DRUPAL_CLIENT_SECRET=Retrieve this from /admin/config/services/consumer

# Required for Preview Mode
DRUPAL_PREVIEW_SECRET=Retrieve this from /admin/config/services/next

# Required for On-demand Revalidation
DRUPAL_REVALIDATE_SECRET=Retrieve this from /admin/config/services/next
2 changes: 0 additions & 2 deletions starters/graphql-starter/lib/drupal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ import { DrupalClient } from "next-drupal"
const baseUrl: string = process.env.NEXT_PUBLIC_DRUPAL_BASE_URL || ""
const clientId = process.env.DRUPAL_CLIENT_ID || ""
const clientSecret = process.env.DRUPAL_CLIENT_SECRET || ""
const previewSecret = process.env.DRUPAL_PREVIEW_SECRET

export const drupal = new DrupalClient(baseUrl, {
auth: {
clientId,
clientSecret,
},
previewSecret,
})

export const graphqlEndpoint = drupal.buildUrl("/graphql")
Expand Down
9 changes: 0 additions & 9 deletions www/content/docs/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,6 @@ If your resources follow the `entity_type--bundle` naming convention, you can sa

---

### previewSecret

- **Default value**: `null`
- **Required**: No

The secret to use for preview mode.

---

### headers

- **Default value**: `{ "Content-Type": "application/vnd.api+json", Accept: "application/vnd.api+json" }`
Expand Down
17 changes: 0 additions & 17 deletions www/content/docs/environment-variables.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ excerpt: Configuration variables for Next.js and Drupal.
NEXT_PUBLIC_DRUPAL_BASE_URL=http://localhost:8080
NEXT_IMAGE_DOMAIN=localhost
# Required for Preview Mode
DRUPAL_PREVIEW_SECRET=
# Required for On-demand Revalidation
DRUPAL_REVALIDATE_SECRET=
Expand Down Expand Up @@ -44,20 +41,6 @@ const drupal = new DrupalClient(process.env.NEXT_PUBLIC_DRUPAL_BASE_URL)

---

## Preview Mode

Environment variables required for preview mode.

| Name | Description |
| ----------------------- | :----------------------------------------------------------------------- |
| `DRUPAL_PREVIEW_SECRET` | The secret for [preview mode](/docs/preview-mode). Example: `6SZy9xkdtR` |

```ts
const drupal = new DrupalClient(process.env.NEXT_PUBLIC_DRUPAL_BASE_URL, {
previewSecret: process.env.DRUPAL_PREVIEW_SECRET,
})
```

## On-demand Revalidation

Environment variables required for on-demand revalidation.
Expand Down
10 changes: 0 additions & 10 deletions www/content/docs/reference/preview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ await drupal.preview(request, response, options?: {
## Notes

- The `preview` method should be called in an API route.
- Remember to set a `previewSecret` on the client.

```ts title=lib/drupal.ts
export const drupal = new DrupalClient(
process.env.NEXT_PUBLIC_DRUPAL_BASE_URL,
{
previewSecret: process.env.DRUPAL_PREVIEW_SECRET,
}
)
```

---

Expand Down
2 changes: 0 additions & 2 deletions www/content/docs/upgrade-guide.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ export const drupal = new DrupalClient(
process.env.NEXT_PUBLIC_DRUPAL_BASE_URL,
{
frontPage: "/home",
previewSecret: process.env.DRUPAL_PREVIEW_SECRET,
auth: {
clientId: process.env.DRUPAL_CLIENT_ID,
clientSecret: process.env.DRUPAL_CLIENT_SECRET,
Expand All @@ -114,7 +113,6 @@ export const drupal = new DrupalClient(
process.env.NEXT_PUBLIC_DRUPAL_BASE_URL,
{
frontPage: "/home",
previewSecret: process.env.DRUPAL_PREVIEW_SECRET,
auth: {
clientId: process.env.DRUPAL_CLIENT_ID,
clientSecret: process.env.DRUPAL_CLIENT_SECRET,
Expand Down

0 comments on commit 08d55f3

Please sign in to comment.