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(next-drupal): next revalidate options #784

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions packages/next-drupal/src/next-drupal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
JsonApiUpdateResourceBody,
JsonApiWithAuthOption,
JsonApiWithCacheOptions,
JsonApiWithNextFetchOptions,
JsonDeserializer,
Locale,
NextDrupalOptions,
Expand Down Expand Up @@ -248,7 +249,9 @@ export class NextDrupal extends NextDrupalBase {
async getResource<T extends JsonApiResource>(
type: string,
uuid: string,
options?: JsonApiOptions & JsonApiWithCacheOptions
options?: JsonApiOptions &
JsonApiWithCacheOptions &
JsonApiWithNextFetchOptions
): Promise<T> {
options = {
deserialize: true,
Expand All @@ -257,6 +260,7 @@ export class NextDrupal extends NextDrupalBase {
params: {},
...options,
}
// Not sure about this node-cache

/* c8 ignore next 11 */
if (options.withCache) {
Expand All @@ -283,6 +287,7 @@ export class NextDrupal extends NextDrupalBase {

const response = await this.fetch(endpoint, {
withAuth: options.withAuth,
next: options.next,
})

await this.throwIfJsonErrors(response, "Error while fetching resource: ")
Expand All @@ -301,7 +306,8 @@ export class NextDrupal extends NextDrupalBase {
path: string,
options?: {
isVersionable?: boolean
} & JsonApiOptions
} & JsonApiOptions &
JsonApiWithNextFetchOptions
): Promise<T> {
options = {
deserialize: true,
Expand Down Expand Up @@ -370,6 +376,7 @@ export class NextDrupal extends NextDrupalBase {
redirect: "follow",
body: JSON.stringify(payload),
withAuth: options.withAuth,
next: options.next,
})

const errorMessagePrefix = "Error while fetching resource by path:"
Expand Down Expand Up @@ -408,7 +415,8 @@ export class NextDrupal extends NextDrupalBase {
type: string,
options?: {
deserialize?: boolean
} & JsonApiOptions
} & JsonApiOptions &
JsonApiWithNextFetchOptions
): Promise<T> {
options = {
withAuth: this.withAuth,
Expand All @@ -427,6 +435,7 @@ export class NextDrupal extends NextDrupalBase {

const response = await this.fetch(endpoint, {
withAuth: options.withAuth,
next: options.next,
})

await this.throwIfJsonErrors(
Expand All @@ -445,6 +454,7 @@ export class NextDrupal extends NextDrupalBase {
pathPrefix?: PathPrefix
params?: JsonApiParams
} & JsonApiWithAuthOption &
JsonApiWithNextFetchOptions &
(
| {
locales: Locale[]
Expand Down Expand Up @@ -490,6 +500,7 @@ export class NextDrupal extends NextDrupalBase {
let opts: Parameters<NextDrupal["getResourceCollection"]>[1] = {
params,
withAuth: options.withAuth,
next: options.next,
}
if (locale) {
opts = {
Expand Down Expand Up @@ -547,7 +558,7 @@ export class NextDrupal extends NextDrupalBase {

async translatePath(
path: string,
options?: JsonApiWithAuthOption
options?: JsonApiWithAuthOption & JsonApiWithNextFetchOptions
): Promise<DrupalTranslatedPath | null> {
options = {
withAuth: this.withAuth,
Expand All @@ -562,6 +573,7 @@ export class NextDrupal extends NextDrupalBase {

const response = await this.fetch(endpoint, {
withAuth: options.withAuth,
next: options.next,
})

if (response.status === 404) {
Expand All @@ -575,7 +587,10 @@ export class NextDrupal extends NextDrupalBase {
return await response.json()
}

async getIndex(locale?: Locale): Promise<JsonApiResponse> {
async getIndex(
locale?: Locale,
options?: JsonApiWithNextFetchOptions
): Promise<JsonApiResponse> {
const endpoint = await this.buildEndpoint({
locale,
})
Expand All @@ -585,6 +600,7 @@ export class NextDrupal extends NextDrupalBase {
const response = await this.fetch(endpoint, {
// As per https://www.drupal.org/node/2984034 /jsonapi is public.
withAuth: false,
next: options?.next,
})

await this.throwIfJsonErrors(
Expand Down Expand Up @@ -657,7 +673,9 @@ export class NextDrupal extends NextDrupalBase {

async getMenu<T = DrupalMenuItem>(
menuName: string,
options?: JsonApiOptions & JsonApiWithCacheOptions
options?: JsonApiOptions &
JsonApiWithCacheOptions &
JsonApiWithNextFetchOptions
): Promise<{
items: T[]
tree: T[]
Expand Down Expand Up @@ -692,6 +710,7 @@ export class NextDrupal extends NextDrupalBase {

const response = await this.fetch(endpoint, {
withAuth: options.withAuth,
next: options.next,
})

await this.throwIfJsonErrors(response, "Error while fetching menu items: ")
Expand Down Expand Up @@ -719,7 +738,7 @@ export class NextDrupal extends NextDrupalBase {

async getView<T = JsonApiResource>(
name: string,
options?: JsonApiOptions
options?: JsonApiOptions & JsonApiWithNextFetchOptions
): Promise<DrupalView<T>> {
options = {
withAuth: this.withAuth,
Expand All @@ -741,6 +760,7 @@ export class NextDrupal extends NextDrupalBase {

const response = await this.fetch(endpoint, {
withAuth: options.withAuth,
next: options.next,
})

await this.throwIfJsonErrors(response, "Error while fetching view: ")
Expand All @@ -759,7 +779,7 @@ export class NextDrupal extends NextDrupalBase {

async getSearchIndex<T = JsonApiResource[]>(
name: string,
options?: JsonApiOptions
options?: JsonApiOptions & JsonApiWithNextFetchOptions
): Promise<T> {
options = {
withAuth: this.withAuth,
Expand All @@ -778,6 +798,7 @@ export class NextDrupal extends NextDrupalBase {

const response = await this.fetch(endpoint, {
withAuth: options.withAuth,
next: options.next,
})

await this.throwIfJsonErrors(
Expand Down
5 changes: 4 additions & 1 deletion packages/next-drupal/src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type Locale = string

export type PathPrefix = string

export interface FetchOptions extends RequestInit {
export interface FetchOptions extends RequestInit, JsonApiWithNextFetchOptions {
Copy link
Contributor

Choose a reason for hiding this comment

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

Popping in on this one based on my ongoing review of #787 Specifically: https://github.com/chapter-three/next-drupal/pull/787/files#diff-5b898e58a9c454bff499b9401f4794981861fc3d8870905f57e8082c4e24b6f5

The notes about NextFetchRequestConfig caused me to take a closer look at the types to confirm that this would actually be possible in the state of the codebase that the Docs PR is in. I believe it is possible to pass options like next: { revalidate: 3600 } - our FetchOptions interface extends Next's RequestInit which does have the next property that accepts options for revalidate and tags. That was a mouthful. Here's what the types look like:

Our fetch options:

export interface FetchOptions extends RequestInit {
  withAuth?: boolean | NextDrupalAuth
}

Next Types:

interface RequestInit {
  next?: NextFetchRequestConfig | undefined
}`

interface NextFetchRequestConfig {
  revalidate?: number | false
  tags?: string[]
}

Based on reading that code, I'd expect this to already be supported without this JsonApiWithNextFetchOptions type. But my guess is that you saw otherwise when actually working with this.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for bring this up @backlineint. Looking at this again, I noticed that in "our" functions (getResource, createResource, etc) the options parameter is of type JsonApiOptions and not FetchOptions. JsonApiOptions doesn't contain many of the properties that FetchOptions has. We should take a closer look at this and probably rework JsonApiOptions to include all/some of FetchOptions.

Copy link

@davidwhthomas davidwhthomas Sep 1, 2024

Choose a reason for hiding this comment

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

It's becoming a bit problematic not being able to pass cache options with next-drupal API requests.

For example:

  • We can't specify Next fetch cache time-based options such as revalidate.
  • We can't include Drupal cache-tags and use granular revalidateTag cache tag invalidation.
  • Next v15, now RC, no longer caches by default so it becomes more important to be able to pass cache options with the fetch requests.

What needs doing to progress this feature?

withAuth?: boolean | NextDrupalAuth
}

Expand Down Expand Up @@ -34,6 +34,9 @@ export type JsonApiWithCacheOptions = {
cacheKey?: string
}

export type JsonApiWithNextFetchOptions = {
next?: NextFetchRequestConfig
}
// TODO: Properly type this.
/* eslint-disable @typescript-eslint/no-explicit-any */
export type JsonApiParams = Record<string, any>
Loading
Loading