-
Notifications
You must be signed in to change notification settings - Fork 175
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
Adopt Next's app router #665
Comments
The context object is removed from app router architecture and so agreeably, we should move away from dependence on methods that require context too. Those methods are essentially the "Pages" router support. Fortunately, much of next-drupal already contains context-less methods. I wanna share a method I wrote that, with a bit of work, could be useful for next-drupal for App router support. https://gist.github.com/fiasco/00625053156c37f9f478721cd313e140 Some features of this method:
I've noticed that my react server components are not caching the way I want to yet and I think this is related to how next-drupal fetches content (nextjs cache-control headers are directly tied to how fetch() calls are made during the page load). We're using this method to provide toggle-able view modes in DraftMode between published, draft and HTML diff views. We also had to implement our own methods and API routes to enable and end DraftMode. I can share those if they're helpful. |
Did you try to pass a custom fetcher to the DrupalClient instance? Something like this:
|
Yes, I set the next.revalidate to 3600 but haven't had success with that yet. It worked fine with other pages in the site that rely on S3 json resources but not with the Drupal responses which make me think it might be related to how Drupal is responding. |
I with with settings the dynamic constant to "force-static" which solved the issue |
Not sure if this applies, but atm time based revalidation has some issues with next. For me setting no-store for the fetcher function of next drupal and place its transactions inside some dynamic_cache worked to have them invalidate manually (it also worked for time based but only in dev environment). |
The Pages Router-specific methods in DrupalClient are:
The example-router-migration is a new example that has been added to this repo that shows how to have full App Router support, but it uses a fake context parameter with So it looks like we just need to have 2 new methods to fix this issue. |
The DrupalClient class has been refactored to inherit from the NextDrupalFetch (base class) and NextDrupal (JsonAPI/App Router class). DrupalClient class contains the methods that are only needed to support Next.js' Pages Router. The getPathFromContext() method has been replaced with the constructPathFromSegment() method for App Router usages. Issue #665
The DrupalClient class has been refactored to inherit from the NextDrupalFetch (base class) and NextDrupal (JsonAPI/App Router class). DrupalClient class contains the methods that are only needed to support Next.js' Pages Router. The getPathFromContext() method has been replaced with the constructPathFromSegment() method for App Router usages. Issue #665
…ticParams() The App Router's new generateStaticParams() replaces Pages Router's getStaticProps(). The new getResourceCollectionPathSegments() method supports generateStaticParams() by returning a list of path segments. This new method replaces the old getStaticPathsFromContext() method. Fixes #665
PR #716 adds two new methods: constructPathFromSegment()This is the refactored getResourceCollectionPathSegments()This method supports Next.js' Each item in the returned array looks like this:
In many cases, the Examples:When used in a export async function generateStaticParams(): Promise<NodePageParams[]> {
const resources = await drupal.getResourceCollectionPathSegments(
["node--page", "node--article"]
);
return resources.map((resource) => {
return {
slug: resource.segments,
}
});
} When used in a export async function generateStaticParams(): Promise<NodePageParams[]> {
const resources = await drupal.getResourceCollectionPathSegments(
["node--article"],
{
// The pathPrefix will be removed from the returned path segments array.
pathPrefix: "/blog",
// The list of locales to return.
locales: ["en", "es"],
// The default locale.
defaultLocale: "en",
}
);
return resources.map((resource) => {
// NOTE: Because `pathPrefix` was set to "/blog",
// "blog" will not be included in the segments array.
// Grab the first item from the segments array to get
// the needed "category" param.
const category = resource.segments.unshift();
return {
lang: resource.locale,
category,
slug: resource.segments,
};
})
} |
The DrupalClient class has been refactored to inherit from the NextDrupalFetch (base class) and NextDrupal (JsonAPI/App Router class). DrupalClient class contains the methods that are only needed to support Next.js' Pages Router. The getPathFromContext() method has been replaced with the constructPathFromSegment() method for App Router usages. Issue #665
…ticParams() The App Router's new generateStaticParams() replaces Pages Router's getStaticProps(). The new getResourceCollectionPathSegments() method supports generateStaticParams() by returning a list of path segments. This new method replaces the old getStaticPathsFromContext() method. Fixes #665
…ticParams() The App Router's new generateStaticParams() replaces Pages Router's getStaticProps(). The new getResourceCollectionPathSegments() method supports generateStaticParams() by returning a list of path segments. This new method replaces the old getStaticPathsFromContext() method. Fixes #665
The DrupalClient class has been refactored to inherit from the NextDrupalFetch (base class) and NextDrupal (JsonAPI/App Router class). DrupalClient class contains the methods that are only needed to support Next.js' Pages Router. The getPathFromContext() method has been replaced with the constructPathFromSegment() method for App Router usages. Issue #665
…ticParams() The App Router's new generateStaticParams() replaces Pages Router's getStaticProps(). The new getResourceCollectionPathSegments() method supports generateStaticParams() by returning a list of path segments. This new method replaces the old getStaticPathsFromContext() method. Fixes #665
…ticParams() The App Router's new generateStaticParams() replaces Pages Router's getStaticProps(). The new getResourceCollectionPathSegments() method supports generateStaticParams() by returning a list of path segments. This new method replaces the old getStaticPathsFromContext() method. Fixes #665
The DrupalClient class has been refactored to inherit from the NextDrupalFetch (base class) and NextDrupal (JsonAPI/App Router class). DrupalClient class contains the methods that are only needed to support Next.js' Pages Router. The getPathFromContext() method has been replaced with the constructPathFromSegment() method for App Router usages. Issue #665
…ticParams() The App Router's new generateStaticParams() replaces Pages Router's getStaticProps(). The new getResourceCollectionPathSegments() method supports generateStaticParams() by returning a list of path segments. This new method replaces the old getStaticPathsFromContext() method. Fixes #665
…ticParams() The App Router's new generateStaticParams() replaces Pages Router's getStaticProps(). The new getResourceCollectionPathSegments() method supports generateStaticParams() by returning a list of path segments. This new method replaces the old getStaticPathsFromContext() method. Fixes #665
The DrupalClient class has been renamed to NextDrupalPages and has been refactored to inherit from the NextDrupalBase (base class) and NextDrupal (JsonAPI/App Router class). NextDrupalPages class contains the methods that are only needed to support Next.js' Pages Router. NextDrupalPages is also available as "DrupalClient" for backwards-compatibility. The getPathFromContext() method has been replaced with the constructPathFromSegment() method for App Router usages. Issue #665
…ticParams() The App Router's new generateStaticParams() replaces Pages Router's getStaticProps(). The new getResourceCollectionPathSegments() method supports generateStaticParams() by returning a list of path segments. This new method replaces the old getStaticPathsFromContext() method. Fixes #665
The DrupalClient class has been renamed to NextDrupalPages and has been refactored to inherit from the NextDrupalBase (base class) and NextDrupal (JsonAPI/App Router class). NextDrupalPages class contains the methods that are only needed to support Next.js' Pages Router. NextDrupalPages is also available as "DrupalClient" for backwards-compatibility. The getPathFromContext() method has been replaced with the constructPathFromSegment() method for App Router usages. Issue #665
…ticParams() The App Router's new generateStaticParams() replaces Pages Router's getStaticProps(). The new getResourceCollectionPathSegments() method supports generateStaticParams() by returning a list of path segments. This new method replaces the old getStaticPathsFromContext() method. Fixes #665
The DrupalClient class has been renamed to NextDrupalPages and has been refactored to inherit from the NextDrupalBase (base class) and NextDrupal (JsonAPI/App Router class). NextDrupalPages class contains the methods that are only needed to support Next.js' Pages Router. NextDrupalPages is also available as "DrupalClient" for backwards-compatibility. The getPathFromContext() method has been replaced with the constructPathFromSegment() method for App Router usages. Issue #665
…ticParams() The App Router's new generateStaticParams() replaces Pages Router's getStaticProps(). The new getResourceCollectionPathSegments() method supports generateStaticParams() by returning a list of path segments. This new method replaces the old getStaticPathsFromContext() method. Fixes #665
The DrupalClient class has been renamed to NextDrupalPages and has been refactored to inherit from the NextDrupalBase (base class) and NextDrupal (JsonAPI/App Router class). NextDrupalPages class contains the methods that are only needed to support Next.js' Pages Router. NextDrupalPages is also available as "DrupalClient" for backwards-compatibility. The getPathFromContext() method has been replaced with the constructPathFromSegment() method for App Router usages. Issue #665
…ticParams() The App Router's new generateStaticParams() replaces Pages Router's getStaticProps(). The new getResourceCollectionPathSegments() method supports generateStaticParams() by returning a list of path segments. This new method replaces the old getStaticPathsFromContext() method. Fixes #665
The DrupalClient class has been renamed to NextDrupalPages and has been refactored to inherit from the NextDrupalBase (base class) and NextDrupal (JsonAPI/App Router class). NextDrupalPages class contains the methods that are only needed to support Next.js' Pages Router. NextDrupalPages is also available as "DrupalClient" for backwards-compatibility. The getPathFromContext() method has been replaced with the constructPathFromSegment() method for App Router usages. Issue #665
…ticParams() The App Router's new generateStaticParams() replaces Pages Router's getStaticProps(). The new getResourceCollectionPathSegments() method supports generateStaticParams() by returning a list of path segments. This new method replaces the old getStaticPathsFromContext() method. Fixes #665
The DrupalClient class has been renamed to NextDrupalPages and has been refactored to inherit from the NextDrupalBase (base class) and NextDrupal (JsonAPI/App Router class). NextDrupalPages class contains the methods that are only needed to support Next.js' Pages Router. NextDrupalPages is also available as "DrupalClient" for backwards-compatibility. The getPathFromContext() method has been replaced with the constructPathFromSegment() method for App Router usages. Issue #665
…ticParams() The App Router's new generateStaticParams() replaces Pages Router's getStaticProps(). The new getResourceCollectionPathSegments() method supports generateStaticParams() by returning a list of path segments. This new method replaces the old getStaticPathsFromContext() method. Fixes #665
The DrupalClient class has been renamed to NextDrupalPages and has been refactored to inherit from the NextDrupalBase (base class) and NextDrupal (JsonAPI/App Router class). NextDrupalPages class contains the methods that are only needed to support Next.js' Pages Router. NextDrupalPages is also available as "DrupalClient" for backwards-compatibility. The getPathFromContext() method has been replaced with the constructPathFromSegment() method for App Router usages. Issue #665
…ticParams() The App Router's new generateStaticParams() replaces Pages Router's getStaticProps(). The new getResourceCollectionPathSegments() method supports generateStaticParams() by returning a list of path segments. This new method replaces the old getStaticPathsFromContext() method. Fixes #665
The DrupalClient class has been renamed to NextDrupalPages and has been refactored to inherit from the NextDrupalBase (base class) and NextDrupal (JsonAPI/App Router class). NextDrupalPages class contains the methods that are only needed to support Next.js' Pages Router. NextDrupalPages is also available as "DrupalClient" for backwards-compatibility. The getPathFromContext() method has been replaced with the constructPathFromSegment() method for App Router usages. Issue #665
…ticParams() The App Router's new generateStaticParams() replaces Pages Router's getStaticProps(). The new getResourceCollectionPathSegments() method supports generateStaticParams() by returning a list of path segments. This new method replaces the old getStaticPathsFromContext() method. Fixes #665
The DrupalClient class has been renamed to NextDrupalPages and has been refactored to inherit from the NextDrupalBase (base class) and NextDrupal (JsonAPI/App Router class). NextDrupalPages class contains the methods that are only needed to support Next.js' Pages Router. NextDrupalPages is also available as "DrupalClient" for backwards-compatibility. The getPathFromContext() method has been replaced with the constructPathFromSegment() method for App Router usages. Issue #665
Package
next-drupal (NPM package)
Describe the feature request
Many Next Drupal methods rely on the context object that the page router provides to
getStaticProps
.According to the typings included in the Next package a context is an object with the shape:
Preview data is also defined in the Next package as:
ParsedUrlQuery is defined in the Node package as such (it's basically a
Record<string, string[]>
):Then in methods such as
getResourceFromContext
Next Drupal states that expect params asJsonApiParams
:In other words: it should be doable for the user to "recreate their own context" as a stopgap solution.
So far I was able to recreate a context object with the utilities provided by the app router version of Next, so for example I would take the slug or the locale from the parameters of the main component function or check if draft mode is active by importing the
draftMode
function fromnext/headers
.I believe there's a wider discussion to be had for Next Drupal: most of the "context methods" internally call one of the non-context variants, with inputs enriched by handling the context object.
This was a desirable solution when the context object was provided to the user by the Next framework, but this is not the case anymore.
Broadly speaking, I see two possible paths to solve this issue (I'm partial to the second one):
The text was updated successfully, but these errors were encountered: