Skip to content

Adopt Next's app router #665

Closed
Closed
@lorenzo-dallamuta

Description

@lorenzo-dallamuta

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:

type GetStaticPropsContext<
    Params extends ParsedUrlQuery = ParsedUrlQuery,
    Preview extends PreviewData = PreviewData
> = {
    params?: Params
    preview?: boolean
    previewData?: Preview
    draftMode?: boolean
    locale?: string
    locales?: string[]
    defaultLocale?: string
}

Preview data is also defined in the Next package as:

type PreviewData = string | false | object | undefined

ParsedUrlQuery is defined in the Node package as such (it's basically a Record<string, string[]>):

interface ParsedUrlQuery extends NodeJS.Dict<string | string[]> {}

Then in methods such as getResourceFromContext Next Drupal states that expect params as JsonApiParams:

type JsonApiParams = Record<string, any>

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 from next/headers.

/app/[...slug]/page.jsx

export default async function Page({ params: { lang, slug } }) {
    // for each required feature of the context interface the value has to be filled in
    // all other entries can be set ad undefined
    const context = { locale: lang, params: { slug } } features
    const path = await drupal.translatePathFromContext(context)
    const node = await drupal.getResourceFromContext(path, context)
    return ( ... )
}

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):

  • provide an utility to recreate a context object (which is complicated since this would need to be done on each route and the necessary data isn't available from a single source)
  • move away from using a context object and have the distinct entries of the context object become method parameters (this still requires the user to access each relevant data source and pass it with something like an options object)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions