Skip to content

Commit

Permalink
docs(next-drupal): requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Monti committed Sep 9, 2024
1 parent 6c0e389 commit 239b578
Show file tree
Hide file tree
Showing 31 changed files with 540 additions and 146 deletions.
2 changes: 1 addition & 1 deletion www/config/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export const docsConfig: DocsConfig = {
},
{
title: "buildEndpoint",
href: "/docs/reference/buildEndpoint",
href: "/docs/reference/buildendpoint",
},
{
title: "getAccessToken",
Expand Down
8 changes: 0 additions & 8 deletions www/content/docs/authentication.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,6 @@ const articles = await drupal.getResourceCollection("node--article", {

If you're using [NextAuth](https://next-auth.js.org) you can use the results from `getSession`:

<!--
************************************************************
* TODO: Remove this block before publishing.

* Comment: Need to corroborate this with NextAuth updates
************************************************************
-->

```ts
import { getSession } from "next-auth/react"

Expand Down
10 changes: 1 addition & 9 deletions www/content/docs/cache.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,14 @@ title: Caching
excerpt: Using a custom cache for resources.
---

The `NextDrupal` has support for caching resources.
The `NextDrupal` client has support for caching resources.

This is handy when dealing with global data: you can fetch data once and re-use during builds.

You can provide your own cache implementation using the [`cache`](/docs/configuration#cache) option.

---

<!--
************************************************************
* TODO: Remove this block before publishing.

* Comment: Not sure how this cache option works with Next.js caching system
************************************************************
-->

## Example

Here's an example on how you can use Redis to cache resources.
Expand Down
4 changes: 2 additions & 2 deletions www/content/docs/client.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ title: NextDrupal Client
excerpt: A powerful JSON:API client for Drupal.
---

The `NextDrupal` is a powerful JSON:API client that ships with helpers for working with Drupal data.
The `NextDrupal` client is a powerful JSON:API client that ships with helpers for working with Drupal data.

You can use the `NextDrupal` client to fetch JSON:API data from Drupal to build static pages.
You can use the `NextDrupal` client to fetch JSON:API data from Drupal to build static pages in advance, or server render pages on-demand.

It also comes with full support for JSON:API write operations which means you can create JSON:API resources from Next.js to Drupal.

Expand Down
18 changes: 1 addition & 17 deletions www/content/docs/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ excerpt: Initialization and options for NextDrupal client.

## Initialization

To create a new `NextDrupal`, use the following initialization:
To create a new `NextDrupal` client, use the following initialization:

```ts
import { NextDrupal } from "next-drupal"
Expand Down Expand Up @@ -125,14 +125,6 @@ You can find more info about using a custom fetcher [here](/docs/fetcher).

### cache

<!--
************************************************************
* TODO: Remove this block before publishing.

* Comment: Not Sure how this works with NextJS cache system
************************************************************
-->

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

Expand Down Expand Up @@ -181,14 +173,6 @@ Set whether the client should use authenticated requests by default. If set to `

---

<!--
************************************************************
* TODO: Remove this block before publishing.

* Comment: I believe this option is no longer available on the new NextDrupal options
************************************************************
-->

### headers

- **Default value**: `{ "Content-Type": "application/vnd.api+json", Accept: "application/vnd.api+json" }`
Expand Down
4 changes: 2 additions & 2 deletions www/content/docs/creating-resources.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ If you're working with JSON:API, be mindful to select the option:

`Accept all JSON:API create, read, update, and delete operations.`

on the following path: `/admin/config/services/jsonapi` on Drupal.
on the following path: `/admin/config/services/jsonapi` in Drupal.

</Callout>

The `NextDrupal` client ships with a `createResource` and a `createFileResource` methods for creating JSON:API resources.
The `NextDrupal` client ships with `createResource` and `createFileResource` methods for creating JSON:API resources.

---

Expand Down
2 changes: 1 addition & 1 deletion www/content/docs/deleting-resources.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ If you're working with JSON:API, be mindful to select the option:

`Accept all JSON:API create, read, update, and delete operations.`

on the following path: `/admin/config/services/jsonapi` on Drupal.
on the following path: `/admin/config/services/jsonapi` in Drupal.

</Callout>

Expand Down
2 changes: 1 addition & 1 deletion www/content/docs/fetcher.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ excerpt: Using a custom fetcher with NextDrupal.
************************************************************
* TODO: Remove this block before publishing.

* Comment: Not sure how a custom fetcher will work because of the caching options of the Nextjs extended fetch API
* Comment: In order to support a custom fetcher and handle caching withouth the native polyfilled fetch, 'unstable_cache' from next should be used, but also the NextDrupal client should support passing revalidate options.
************************************************************
-->

Expand Down
7 changes: 0 additions & 7 deletions www/content/docs/fetching-resources.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,3 @@ const article = await drupal.getResourceCollection(
// highlight-end
)
```

<!--
/// This link is broken: Remove it or create page with examples ?
## More

See the [Examples](/examples) section for data fetching examples.
-->
21 changes: 13 additions & 8 deletions www/content/docs/pages.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ title: Building Pages
excerpt: How to build pages using JSON:API resources from Drupal.
---

In Next.js V14, data fetching has evolved significantly from previous versions. Instead of using `getStaticProps` and `getServerSideProps` you now use the native `fetch` function enhanced by Next.js to handle server-side data fetching. This allows you to configure caching and revalidation directly within your fetch requests. These can be used in Server Components, Route Handlers, and Server Actions. You can read more about it [here](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
In Next.js V14, data fetching has evolved significantly from previous versions. Instead of using `getStaticProps` and `getServerSideProps` you now use the native `fetch` function enhanced by Next.js to handle server-side data fetching.

The `NextDrupal` client provides several functions to help you query JSON:API resources from Drupal and manage these revalidation options on the `fetch` request level.
The `NextDrupal` client provides several functions to help you query JSON:API resources from Drupal.

<!--
************************************************************
* TODO: Remove this block before publishing.

* Comment: Need to corroborate (and maybe update) once next-drupal supports `next` revalidation options
* Comment: Need to corroborate (and maybe update) once next-drupal supports `next` revalidation options. Possible replacement for above introduction once it's supported

In Next.js V14, data fetching has evolved significantly from previous versions. Instead of using `getStaticProps` and `getServerSideProps` you now use the native `fetch` function enhanced by Next.js to handle server-side data fetching. This allows you to configure caching and revalidation directly within your fetch requests. These can be used in Server Components, Route Handlers, and Server Actions. You can read more about it [here](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)

The `NextDrupal` client provides several functions to help you query JSON:API resources from Drupal and manage these revalidation options on the `fetch` request level.

************************************************************
-->

Expand Down Expand Up @@ -52,7 +57,7 @@ export default function AboutPage() {

## Dynamic pages

You can use Next.js [dynamic route](https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes) to build static pages for Drupal entity types.
You can use Next.js [dynamic routes](https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes) to build static pages for Drupal entity types.

Start by creating a page at `/app/[...slug]/page.tsx`, where `[...slug]` maps to the **path alias** for an entity type (or content type) in Drupal.

Expand Down Expand Up @@ -134,17 +139,17 @@ export default function Page({ params }) {
params.addFields("node--article", ["title", "path", "body", "uid"])
}

const node = await drupal.getResourceByPath(path, {
const node = await drupal.getResource(path, path.entity.uuid {
params: params.getQueryObject(),
})

// Render different Components based on Node type
// Render different Components based on Node type.
if (node.type === "node--page") {
return <PageComponent />
return <PageComponent node={node}/>
}

if (node.type === "node--article") {
return <ArticleComponent />
return <ArticleComponent node={node}/>
}

return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,23 @@ const drupal = new DrupalClient(process.env.NEXT_PUBLIC_DRUPAL_BASE_URL, {
```ts
// https://example.com/jsonapi/node/article.
const url = await drupal.buildEndpoint({ resourceType: `node--article` })
```

<!--
************************************************************
* TODO: Remove this block before publishing.
// https://example.com/jsonapi/en/node/article.
const url = await drupal.buildEndpoint({
locale: `en`,
resourceType: `node--article`,
})

* Comment: Add more examples. Not sure how the 'path' works
************************************************************
-->
// https://example.com/jsonapi/en/node/article?include=field_image.
const url = await drupal.buildEndpoint({
locale: `en`,
resourceType: `node--article`,
searchParams: `include=field_image`,
})

// https://example.com/jsonapi/en/views/articles/page_1.
const url = await drupal.buildEndpoint({
locale: `en`,
path: `/views/articles/page_1`,
})
```
12 changes: 10 additions & 2 deletions www/content/docs/reference/fetch.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@ const response = await drupal.fetch(input, init?): Promise<Response>
- Optional
- The fetch options with `withAuth`.
- If `withAuth` is set, `fetch` will fetch an `Authorization` header before making the request.
<!--
************************************************************
* TODO: Remove this block before publishing.
* Comment: I'll remove the proposed `next` prop till is supported
- `next: NextFetchRequestConfig`
- Set the `revalidate` option in seconds to configure [Time-based revalidation](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#time-based-revalidation).
- Set the `revalidate` option in seconds to configure [Time-based revalidation](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#time-based-revalidation).
Ex: `next: { revalidate: 3600 } // revalidate resource every hour`
- Set the `tags` option to confiure [Tag-based On demand revalidation](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#on-demand-revalidation).
- Set the `tags` option to confiure [Tag-based On demand revalidation](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#on-demand-revalidation).
Ex: `next: { tags: ['resource-tag'] }`
************************************************************
-->

---

Expand Down
40 changes: 40 additions & 0 deletions www/content/docs/reference/getentryforresourcetype.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: getEntryForResourceType
excerpt: Get the JSON:API entry for a resource type.
---

```ts
const url = await drupal.getEntryForResourceType(type, locale?): Promise<string>
```
- `type: string`
- **Required**
- The resource type. Example: `node--article`.
- `locale: string`
- Optional
- The locale to fetch the index. Example: `es` or `fr`.
---
## Notes
By default, when retrieving resources in `getResource` or `getResourceCollection`, the `NextDrupalPages` make a request to Drupal to fetch the JSON:API resource entry..
Example: if you provide `node--article`, `NextDrupalPages` will make a request to `http://example.com/jsonapi/node/article`.

If you would like to infer the entry from the resource type, use the [`useDefaultResourceTypeEntry`](/docs/configuration#usedefaultresourcetypeentry) option.

```ts
const drupal = new NextDrupalPages(process.env.NEXT_PUBLIC_DRUPAL_BASE_URL, {
useDefaultResourceTypeEntry: true,
})
```

---

## Examples

```ts
// https://example.com/jsonapi/node/article.
const url = await drupal.getEntryForResourceType(`node--article`)
```
13 changes: 3 additions & 10 deletions www/content/docs/reference/getmenu.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const resource = await drupal.getMenu<T = DrupalMenuItem>(
defaultLocale,
withCache,
cacheKey,
next
}
): Promise<{
items: T[]
Expand All @@ -42,11 +41,6 @@ const resource = await drupal.getMenu<T = DrupalMenuItem>(
- `defaultLocale: string`: The default locale of the site.
- `withCache: boolean`: Set `withCache` if you want to store and retrieve the menu from cache.
- `cacheKey: string`: The cache key to use.
- `next: NextFetchRequestConfig`
- Set the `revalidate` option in seconds to configure [Time-based revalidation](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#time-based-revalidation).
Ex: `next: { revalidate: 3600 } // revalidate resource every hour`
- Set the `tags` option to confiure [Tag-based On demand revalidation](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#on-demand-revalidation).
Ex: `next: { tags: ['resource-tag'] }`

---

Expand Down Expand Up @@ -79,14 +73,13 @@ const menu = await drupal.getMenu("main", {
************************************************************
* TODO: Remove this block before publishing.

* Comment: Don't know how this cache works with Nextjs caching system
************************************************************
-->

* Comment: commented example until revalidation props are supported.
- Get the `main` menu with tag-based revalidation.

```ts
const menu = await drupal.getMenu("main", {
next: { tags: ["main--menu"] }, // revalidates resource when calling `revalidateTag('main--menu')`
})
```
************************************************************
-->
32 changes: 32 additions & 0 deletions www/content/docs/reference/getpathfromcontext.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
title: getPathFromContext
excerpt: Return the path (slug) from getStaticProps or getServerSideProps context.
---

```ts
const slug = drupal.getPathFromContext(
context,
options?: {
pathPrefix,
}
): string
```

- `context: GetStaticPropsContext | GetServerSidePropsContext`
- **Required**
- The context from `getStaticProps` or `getServerSideProps`.
- `options`
- Optional
- `pathPrefix: string`: Set the pathPrefix if you're calling from a subdir. Example: for `/articles/[...slug].tsx`, use `pathPrefix: "/articles"`.

---

## Examples

- Get the path (slug) from `getStaticProps` context.

```ts
export async function getStaticProps(context) {
const slug = await drupal.getPathFromContext(context)
}
```
Loading

0 comments on commit 239b578

Please sign in to comment.