Next.js recommends using their new App Router over the legacy Pages Router. The full router migration guide is available in the Next.js documentation.
The new App Router is also designed to facilitate sites that need to migrate from the Pages Router in a piecemeal fashion rather than all at once.
This codebase is an example of a next-drupal
site that is in the middle of a Next.js Pages to App Router migration.
- Update the
next-drupal
package to the latest 2.x version. - Update the
next
module on your Drupal site to the latest 2.x version.- The most recent version is available at https://www.drupal.org/project/next
- Run your Drupal site’s /update.php script.
- Migrate from Preview Mode to Draft mode. Preview mode only works with the legacy Pages Router. Draft mode works with both routers.
- Update the
/pages/api/preview.ts
file to match the one in this Git repo. - Update the
/pages/api/exit-preview.ts
file to match the one in this Git repo. - Delete your
/pages/api/revalidate.ts
file. - Create a
/app/api
directory and add all the files from this Git repo’s/app/api
directory.
- Update the
Follow Next.js’ router migration guide.
Over time, you will be moving all the files from /pages
to /app
. However, these JavaScript files should remain in the /pages
directory to prevent Preview/Draft Mode from breaking:
/pages/api/exit-preview.ts
/pages/api/preview.ts
- Turn off the legacy Preview Mode.
- Go to the Next.js site configuration on your Drupal site at
/admin/config/services/next
. - For each Next.js configuration, change the end of the URL in the “Draft URL (or Preview URL)” setting from
preview
todraft
, e.g.https://example.com/api/preview
tohttps://example.com/api/draft
.
- Go to the Next.js site configuration on your Drupal site at
- Delete the last files in your
/pages
directory:/pages/api/exit-preview.ts
/pages/api/preview.ts
- Update your
lib/drupal.ts
file and switch from theDrupalClient
class (with dual Pages/App Router support) to the new, leanerNextDrupal
class (with only App Router support).
Licensed under the MIT license.