Overview

Next.js for Drupal v2.0.0 was released on 2025/2/11.

https://next-drupal.org/

https://next-drupal.org/blog/next-drupal-2-0

When I tried it out, I found that the handling of BASE_PATH required attention, so this is a memo about it.

Environment Variables

The sample environment variables are as follows.

##NN#DD#DEERRRSRXXAUURUeeTTuPPePeq__tAAqAuPIhLLuLhiUMe__i_trBAnCCrRteLGtLLeEpdIEiIIdVsC_cEEA:_DaNNfL/DOtTToI/RMi__rDnUAoISAePInDEOTxAN=CnEtL=RR-_-_seEdSdBitTeErAtr=mCuSeiRaRpE.eenEa_evtdTlUxer=.RaiRRoLmteeer=phvvtghliear/teslidt.tieopcfhdvcsoriaes:most//mite/fohns/rniviaositdmremfo.i/rnenaomx/dmeacmnmoi/tpnna-lf/dveicma.gircnniosf/ameicbrgolv/neisfsceiergs//iscceoernsvs/iuccmoeensrs/unmeexrt

When specifying NEXT_PUBLIC_DRUPAL_BASE_URL with a base path included, such as https://site.example.com/xxx, API requests were sent to https://site.example.com/jsonapi/, failing to correctly retrieve resources.

Cause

When checking getResourceCollection where the error was occurring, I found that the buildUrl function used to create the request URL was using new URL(path, this.baseUrl);.

baaussi}y}}y}lnndcc)i}rclps=ci}rco}c}tc}a)crUoo;feoaeofe.p;o)ho)w;oernnsutbcta{npt`stgtwdnlrs;inwnc;ar"ntlsse(ruuahr}s(au$eoeiiesoeessieaieEsu(ttaslrilc)tptr{aStotstcsa.ttxctsrtrpHre.nle=hahnlrtRnheoaordhthprnausacasdP{ltocresArpelucerA:etoojtrenhreuE="aoh=tchisuitnerhbeu:hnrsohladPcarn"rchaPno=taid:cPustoisop,rlahrld",aa&`ilaguhlopeagphposewnt=cer)c;p"ml&/ser(r{:inooTr(o:tp.,hishaho,se$.Sa)czsipya`nitti=oenDm{iS!{bem;etentpmFsooihlnae=rs=neppugsCh:tieseepnoreasrwutgaaimoio::ttsnow.c(p&s(mttlelst=nc=i.swfadhUa&t{ehhdnl.rstohon.IeiePRlrn.}Utewua?ypianecfttsaLJtits`r}ciew.ptnwsxaJcer(synt;l$tt,aleiga.tcshrrapopg=a({ihio,oiw,hoieimaneirtoAtcnrtiennsastAoflthnuasetEgpl)hpfyosi(ttl?sthroi,i(cWsthhe.ohArrnz{Pssai.y,ipuiuoesetaeeltaps!arstrsehraaehpe.=rc.hso.?iarr(i,b=aef,(ujsmcc?"Pumerst.shh/roiosctcohbP)`"eplpoceniaoa;/)ftdtlh(ssbr$)iiEil(c).eja{xonoeeo;dUeml{}ndncnlercso$spstdlsltc{)o?ipee)s=api.oocr;.=la{ndniti=ettenia}h(fotol"`}{af,nio`u:zb:,lt{ejty"(e"Lpjc"oest;co"a$nl{)&et&y:?p"ejgo}sep.ott`nQi);uo;enrsy.Olbojceaclte":inosiedar0c,hParams?searchParams.getQueryObject():searchParams

When I asked ChatGPT, I received the following answer.

When using new URL(path, this.baseUrl), if the path is an absolute path (such as /jsonapi), only the host portion of baseUrl is applied, and path prefixes like /xxx are ignored. As a result, API requests are sent to /jsonapi/ instead of the intended https://site.example.com/xxx/jsonapi/.

Solution

ChatGPT suggested using patch-package. Below is the response.


patch-package allows you to apply patches to files within node_modules and maintain the changes.

Steps

  • Install patch-package
npminstallpatch-packagepostinstall-postinstall
  • Add the following to scripts in package.json
"}sc"rpiopsttsi"n:st{all":"patch-package"
  • Directly edit node_modules/next-drupal/dist/index.js (if you have already made the changes, this is OK as-is)
bui}ldcc)i}rUoo;fernnsutlsse(ru(ttaslrpHre.nausacastrenhreuhladPcar,rlahrl=cer)c;shahenDm{ae=rs=rwuc(p&shUa&tPRlraLJtir(syna"opgm/neisxAof)xpfyxi({"Pssaee+raaarrpmccashhtP)hoa;,brjatemhcsitss=..=b=as"eoUbrjle)c;t"&&Fi"xgeedtQbuyerhyaOrbdjceocdti"ngininsetahricshPcaarsaems?searchParams.getQueryObject():searchParams
  • Create the patch
npxpatch-packagenext-drupal

A file named patches/next-drupal+<version>.patch will be created.

  • With this setup, when you run npm install, the postinstall script will automatically apply the patch.

Summary

With the above solution, I was able to communicate between Next.js 15’s App Router and Drupal 11.

There may be better approaches, but I hope this is helpful.