next-drupal: TypeError: The "string" argument must be of type string or an instance of Buffer or ArrayBuffer. Received an instance of Object

Hi! Thanks for all the work you’ve put into this project. I’m following the quick start tutorial and have managed to set things up, after a some setbacks with simple_oauth library.

But, currently, I’m having an issue viewing my blog posts. I can see them at the home page and can also see that all their content is fetched in home page, as a collection. But once I visit a specific one (blog/first-post, as in blog/[node:title]), I get a 404 in the Network tab in Google Chrome.

And also this error.

 1 of 1 unhandled error

Unhandled Runtime Error
Error: Failed to load script: /_next/static/chunks/pages/404.js

Source
.next\static\chunks\main.js (83:51) @ HTMLScriptElement.script.onerror

  81 | //    executes when `src` is set.
  82 | script.onload = resolve;
> 83 | script.onerror = ()=>reject(markAssetError(new Error(`Failed to load script: ${src}`)))
     |                                           ^
  84 | ;
  85 | // 2. Configure the cross-origin attribute before setting `src` in case the
  86 | //    browser begins to fetch.

I guess the following error is due to not getting any data in the response.

error - TypeError [ERR_INVALID_ARG_TYPE]: The "string" argument must be of type string or an instance of Buffer or ArrayBuffer. Received an instance of Object
    at Function.byteLength (buffer.js:726:11)
    at ServerResponse.apiRes.end (C:\Users\trogl\Desktop\next-next-next\node_modules\next\dist\server\api-utils.js:72:41)
    at C:\Users\trogl\Desktop\next-next-next\node_modules\next-drupal\dist\index.js:566:30
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Object.apiResolver (C:\Users\trogl\Desktop\next-next-next\node_modules\next\dist\server\api-utils.js:101:9)
    at async DevServer.handleApiRequest (C:\Users\trogl\Desktop\next-next-next\node_modules\next\dist\server\next-server.js:770:9)
    at async Object.fn (C:\Users\trogl\Desktop\next-next-next\node_modules\next\dist\server\next-server.js:661:37)
    at async Router.execute (C:\Users\trogl\Desktop\next-next-next\node_modules\next\dist\server\router.js:205:32)
    at async DevServer.run (C:\Users\trogl\Desktop\next-next-next\node_modules\next\dist\server\next-server.js:841:29) {
  code: 'ERR_INVALID_ARG_TYPE',
  page: '/api/preview'
}

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 29

Most upvoted comments

I am experiencing the same phenomenon.

TypeError [ERR_INVALID_ARG_TYPE]: The "string" argument must be of type string or an instance of Buffer or ArrayBuffer. Received an instance of Object
    at new NodeError (node:internal/errors:371:5)
    at Function.byteLength (node:buffer:733:11)
    at ServerResponse.apiRes.end (/opt/preview/.docker/preview/node_modules/next/dist/server/api-utils.js:72:41)
    at /opt/preview/.docker/preview/node_modules/next-drupal/dist/index.js:593:30
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Object.apiResolver (/opt/preview/.docker/preview/node_modules/next/dist/server/api-utils.js:101:9)
    at async Server.handleApiRequest (/opt/preview/.docker/preview/node_modules/next/dist/server/next-server.js:770:9)
    at async Object.fn (/opt/preview/.docker/preview/node_modules/next/dist/server/next-server.js:661:37)
    at async Router.execute (/opt/preview/.docker/preview/node_modules/next/dist/server/router.js:205:32)
    at async Server.run (/opt/preview/.docker/preview/node_modules/next/dist/server/next-server.js:841:29) {
  code: 'ERR_INVALID_ARG_TYPE'

The version I am using is as follows.

  • next-drupal: 0.16.2
  • next.js (Drupal Module): 1.0.0-beta5

I refer to this code, On the Drupal side, the page loaded successfully by applying the following patch to the subrequests module.

        "patches": {
+	    "drupal/subrequests": {
+                "Subrequest failed validation": "https://www.drupal.org/files/issues/2019-05-27/3029570-array-not-object.patch",
+                "Get same results on different request": "https://www.drupal.org/files/issues/2019-07-18/change_request_type-63049395-09.patch"
+            }
        },


I faced the same issue (Drupal 9.2). As per @taku3202 comment, applying Drupal Subrequests patches solved it :

Aha

From \Drupal\decoupled_router\EventSubscriber\RouterPathTranslatorSubscriber::onPathTranslation:

$path = $event->getPath();
$path = $this->cleanSubdirInPath($path, $event->getRequest());
/**
   * Removes the subdir prefix from the path.
   *
   * @param string $path
   *   The path that can contain the subdir prefix.
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request to extract the path prefix from.
   *
   * @return string
   *   The clean path.
   */
  protected function cleanSubdirInPath($path, Request $request) {
    // Remove any possible leading subdir information in case Drupal is
    // installed under http://example.com/d8/index.php
    $regexp = preg_quote($request->getBasePath(), '/');
    return preg_replace(sprintf('/^%s/', $regexp), '', $path);
  }

Trying to figure out why we’re doing this now.

@demo-Ghost @rilrom Thanks for the detailed info. Is there anyway you can help me reproduce this (minimal reproducible repo)?

Few things I’ve found that may help guide you @shadcn.

I managed to reproduce the issue of a 404 appearing on individual slugs. I was able to rectify the issue by adding ‘deserialize: false’ to the options parameter of ‘getResourceFromContext’.

In the getResourceByPath function, if data is serialised it will return undefined. I have not been able to determine why.

image-1

This then returns the data back to the getStaticProps function, however the data does not contain a node.status (edit: I imagine this is likely due to deserialize not being run), so it will return notFound: true

image-2

Removing this check will create a successful page load.

Obviously this is not ideal and the checks are there for a reason but hopefully this will provide some context in order for you to further investigate.

Edit: Apologies for all the edits.

Seems there’s one more thing I forgot to mention. Setting deserialize to false is not enough, this line here also seems to cause null to be returned.

image-3