wulflok
05/21/2023, 11:48 AMappDir
setup on Next 13.4.2, I created a not-found.js
file to return a custom 404 and not the Next default. On my local setup using npm run dev
this works correctly and gives me my custom page.
On Pages, it returns the Next default error message and not the custom page.
Given that it works locally, I'm wondering what might be creating this weird behavior. I couldn't find anything searching here or on the Next GitHub issues, so if anybody has seen this I'd appreciate a pointer. Thanks! ๐Better James
05/21/2023, 11:58 AM@cloudflare/next-on-pages
.
When you run npm run dev
, I imagine it's going through next dev
. If so, things will function as expected as that uses Next.js' own dev server and routing implementation. If you would like to test what things are like locally, you can build with @cloudflare/next-on-pages@1
and then start up wrangler.
Anyway, if you're getting an unexpected 404 page, I would hazard a guess that you might be using the older version of next-on-pages - v0.10.1. You could check your build command and/or devdeps to confirm this, but that old routing system doesn't support many things that are required for the app directory and new Next.js versions to function as expected. If you are on an old version, please try switching to @cloudflare/next-on-pages@1
.
IIRC, there also might be an issue with prerendered routes and not-found
when using next-on-pages, but I cannot recall exactly, so it's quite likely that you'll also need to opt your page into the edge runtime for it function properly too, but I might be wrong about that part - been a while since I last tried one.
I have a modified version of the app directory demo deployed with working custom not found route-level pages that you see at:
https://app-playground-xnd.pages.dev/not-found/clothing/does-not-existwulflok
05/21/2023, 12:00 PMwulflok
05/21/2023, 3:21 PMnpm run build
as my build step and the default /out
directory. On local, this worked but I got the default Next error page on Pages.
2. I changed the build in the Pages dashboard to use @cloudflare/next-on-pages@1
as suggested (actually, used the presets from the drop-down). This worked but now I get a new error for 404s.
Error: Could not access built-in node modules, please make sure that your Cloudflare Pages project has the 'nodejs_compat' compatibility flag set.
wulflok
05/21/2023, 3:21 PMwulflok
05/21/2023, 3:22 PMnot-found.js
file looks like this:
export default function NotFound() {
return (
<div className="dark:text-gray-300">
<h2>Not Found</h2>
<p>Could not find requested resource</p>
<a href="/">Return to the home page</a>
</div>
)
}
Better James
05/21/2023, 3:22 PMnodejs_compat
compatibility flag in your Pages project settingsBetter James
05/21/2023, 3:22 PMBetter James
05/21/2023, 3:23 PMwulflok
05/21/2023, 3:23 PM--compatibility-flag=nodejs_compat
<-- like that?wulflok
05/21/2023, 3:24 PMBetter James
05/21/2023, 3:24 PMwulflok
05/21/2023, 3:24 PMBetter James
05/21/2023, 3:25 PMwulflok
05/21/2023, 3:25 PMwulflok
05/21/2023, 3:32 PMNotFound
function in the not-found.js
file.wulflok
05/21/2023, 3:32 PMwulflok
05/21/2023, 3:35 PMnpm run dev
locally. So maybe something with rewrites? I'm digging but nothing plainly apparent is showing.Better James
05/21/2023, 3:35 PMwulflok
05/21/2023, 3:36 PMBetter James
05/21/2023, 3:39 PMwulflok
05/21/2023, 3:40 PMwulflok
05/21/2023, 3:40 PMBetter James
05/21/2023, 3:43 PMwulflok
05/21/2023, 3:44 PMBetter James
05/21/2023, 3:45 PMwulflok
05/21/2023, 3:46 PMBetter James
05/21/2023, 3:46 PMwulflok
05/22/2023, 2:26 AMnpm next build
comment to the cloudflare next-on-pages tool. But for your future reference:
- I was on an older version of wrangler (2.6.2) and things were broken with a fresh create-next-app
too until I updated that to 3.0.0.
- Also, in my next.config.js
I had output: 'export'
which caused the next-on-pages watch command to endlessly loop. removing that fixed the loop.
So this was a case of getting all my versions and dependencies updated, and removing old stand-alone next commands or left-overs in config files. Blah.Better James
05/22/2023, 8:03 AM