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

Merged

Conversation

MontiMarco92
Copy link
Contributor

@MontiMarco92 MontiMarco92 commented Jun 27, 2024

Adds next revalidate options to resource-methods and custom fetch.

This pull request is for: (mark with an "x")

  • examples/*
  • modules/next
  • packages/next-drupal
  • starters/basic-starter
  • starters/graphql-starter
  • starters/pages-starter
  • Other

GitHub Issue: #761

  • I need help adding tests. (mark with an "x")
    Code changes need test coverage. If you don't know
    how to make tests, check this box to ask for help.

Describe your changes

I've extended the custom fetch function to accept the next revalidation options.

  • fetch("https://...", { next: { revalidate: false | 0 | number } })
  • fetch("https://...", { next: { tags: ['node:1', 'tag:2'] } })

I've added the next option to the following resource-methods:

getResource
getResourceByPath
getResourceCollection
getResourceCollectionPathSegments
translatePath
getIndex
getMenu
getView
getSearchIndex

It might be necessary to add it to CRUD methods or others, but I'll wait for feedback first.

I've added some unitary tests as well but might need refinement.

Contribution

Sponsored by @brainsum

Copy link

vercel bot commented Jun 27, 2024

Someone is attempting to deploy a commit to the Chapter Three Team on Vercel.

A member of the Team first needs to authorize it.

This was referenced Jul 9, 2024
@@ -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
Collaborator

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?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agreed @davidwhthomas - working on setting some time aside this week to try to help move this one forward.

While I'm here, one thing that either needs to be added here, or handled in a follow up issue is adding the api route for tag invalidation to the basic (app router) starter.

Copy link
Collaborator

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.

Took another look at this, and I think the problem is that the next-drupal-pages class also uses JsonApiOptions. Adding fetchOptions to JsonApiOptions won't make sense for the pages router. Don't doubt that we could still refine these types, but also don't see an easy path to that in this PR.

backlineint
backlineint previously approved these changes Oct 18, 2024
Copy link
Collaborator

@backlineint backlineint left a comment

Choose a reason for hiding this comment

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

Tested this locally and both the revalidation and tags options seem to work great. I think we can merge this (ideally after accepting the small type related suggestion)

There are two things I think we need to do to complete this story on the front end, which I'll create follow up tickets for:

packages/next-drupal/src/next-drupal.ts Outdated Show resolved Hide resolved
@@ -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
Collaborator

Choose a reason for hiding this comment

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

Suggested change
export interface FetchOptions extends RequestInit, JsonApiWithNextFetchOptions {
export interface FetchOptions extends RequestInit {

I tested locally and confirmed that we don't need to add JsonApiWithNextFetchOptions here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right. I removed this from the FetchOptions type.

@@ -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
Collaborator

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.

Took another look at this, and I think the problem is that the next-drupal-pages class also uses JsonApiOptions. Adding fetchOptions to JsonApiOptions won't make sense for the pages router. Don't doubt that we could still refine these types, but also don't see an easy path to that in this PR.

@MontiMarco92
Copy link
Contributor Author

@backlineint I've made the correction to the FetchOptions interface. So it would be ready to merge. As you commented, there are a few steps to follow in further tickets:

  • Update starters
  • Add cache option to methods to follow the next js fetch options.
  • Check if it's relevant to extend this next: {revalidate} option to another CRUD methods. See main PR description to check which methods support revalidate options for now.
  • Update the docs again to add information to the corresponding methods about the new allowed parameter.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants