Next.js Discord

Discord Forum

Edge runtime causes 500 internal server error for bots

Answered
Arya posted this in #help-forum
Open in Discord
Does anyone know why edge runtime suddenly throws an internal error for bots such as Googlebot or other web crawlers? I can't remove edge runtime because I need it for Cloudflare's D1 and R2 services to run.

131 Replies

Does anyone face the same problem?
@Anay-208 | Ping in replies Can you share screenshot?
Not sure how can I share a screenshot with more details than this, I commented components that are using D1 queries and this is when I found out edge runtime gives a 500 error, despite the client is looking alright
@Anay-208 | Ping in replies Are you on windows?
Yes, I'm on windows
You can use wsl and move your project inside it.
I got a similar error with next 15 on windows.

Using wsl magically fixes the issue
@Anay-208 | Ping in replies You can use wsl and move your project inside it. I got a similar error with next 15 on windows. Using wsl magically fixes the issue
I activated WSL before but not really familiar with it. How do I move my project there, then work on it?
@Arya I activated WSL before but not really familiar with it. How do I move my project there, then work on it?
There should be a wsl folder when you open file explorer. Move everything except node modules
I think there is a folder named like that only.

Just move it to ~ folder of wsl
What error do you get?
Pretty much "need admin privileges", because when I try to move it to ~, the paste option is not lighting up
You will need to install wsl extension then
And then CTRL+SHIFT+P, connect to WSL
@Anay-208 | Ping in replies And then CTRL+SHIFT+P, connect to WSL
I couldn't do npm install
@Anay-208 | Ping in replies Why?
Delete node modules
And try again
it was already deleted before, tried a clean install
@Arya Click to see attachment
I think you’re using windows terminal
Delete node_modules, run wsl, and try installing again
@Anay-208 | Ping in replies Delete node_modules, run wsl, and try installing again
i dont think i did this right
Install nvm in wsl
and install nodejs in it
@Anay-208 | Ping in replies and install nodejs in it
alright, i successfully installed nvm, nodejs and npm install-ed and it works, one thing now is i cant run npm run dev
what error do you see?
@Anay-208 | Ping in replies what error do you see?
remove .wrangler
and try again
@Anay-208 | Ping in replies remove `.wrangler`
deleted it, ran the command but it just fails and creates the .wrangler folder again anyway
Yes, once it creates its not really a problem, but it still didn't work?
is the fact i cloned the project from github instead of copying it directly, is a problem?
Nope
try updating the wsl version
@Anay-208 | Ping in replies try updating the wsl version
still doesn't work
can you check your ubuntu version installed inw sl
wsl -l -v
in cmd
that's probably it
in the issue saw one person saying ubuntu 22 works
you'll need to update to 22
try wsl --update
@Anay-208 | Ping in replies try `wsl --update`
i already updated and apparently my ubuntu now is version 22
try deleting node_modules and .wrangler and run dev again
im installing libc++1 just in case
try running reboot then try again
try installing wrangler version 3.105.0
@Anay-208 | Ping in replies try installing wrangler version `3.105.0`
does it have to be specifically that version?
i updated wrangler to the latest version
which was apparently 4.0
Can you just try if that version actually works to broaden down the issue?
otherwise libc++, should fix the issue
@Anay-208 | Ping in replies otherwise libc++, should fix the issue
is it different than libc++1?
@Anay-208 | Ping in replies try installing wrangler version `3.105.0`
okay this version finally works
This is a older version, that’s why it does, if you upgrade right update this version it prob won’t work
@Arya is it different than libc++1?
I’ll check the exact name of package
@Anay-208 | Ping in replies I’ll check the exact name of package
okay, there's no more 500 errors, but how do i make sure it stays that way in production after i commit this?
If you’re in the older version, you can use it temporarily if it’s urgent. Because I’m not sure about the exact cause of the issue, as I never faced this.
i see
as long as it's stable and works, eh
but now i've got problems building application while deploying in cloudflare
sigh
@Anay-208 | Ping in replies Package json should be updated
now my website throws internal server errors to the client too, i dont know what went wrong
Check console
@Anay-208 | Ping in replies Check console
that's the thing, in development (localhost) it works perfectly without 500 errors, but after deploying it to cloudflare (https://thethirdswan.com/), it throws a text that just says internal server error
i assume it only affects pages with edge runtime (again), because when I access a static page (https://thethirdswan.com/about), it's normal
@Anay-208 | Ping in replies Check console
when i check console on the affected pages, it only shows a GET message error and that's it
should i bring this up to cloudflare's discord server?
@Arya should i bring this up to cloudflare's discord server?
You can try, but it’s a code related issue
Can you send code for that page
@Anay-208 | Ping in replies You can try, but it’s a code related issue
I mean as far as I’ve noticed, I never/barely really received a reply for code related issue there
And have you added env
@Arya try running wrangler pages log tail
and then visit
@Anay-208 | Ping in replies Can you send code for that page
import { Box } from "@mui/material";
import { ArrowRightAlt } from "@mui/icons-material";
import BlogCard from "./ui/blogcard";
import Link from "next/link";
import Latest from "./ui/latest";
import Links from "./ui/links";
import CreateButton from "./ui/createbutton";

export const runtime = 'edge'

export default function Home() {
  return (
    <main>
      <h2 className="text-xl text-center">
        Hello! <br/>
        I hope you enjoy my blogs! :)
      </h2>
      <CreateButton />
        <div className="m-1">
          <div className="md:grid grid-cols-2">
            <div className="md:ml-52">
              <p>Latest</p>
              <Latest />
              <hr className="hidden md:block"/>
              <Links device="other"/>
            </div>
            <div className="md:ml-32 md:mr-52">
              <p>Other Blogs</p>
              <div className="flex flex-wrap justify-center md:block">
                <BlogCard />
              </div>
            </div>
          </div>
          <Link href="/blog">
            <Box component="section" sx={{p: 2, border: '1px solid gray', m: 2}}>
              <p>View other blogs <ArrowRightAlt /></p>
            </Box>
          </Link>
        </div>
    </main>
  );
}
@Anay-208 | Ping in replies <@1205126991804633104> try running `wrangler pages log tail`
in the terminal? i have been trying to run wrangler commands but the bash says "wrangler: command not found"
npm i -g wrangler
install it globally first
@Anay-208 | Ping in replies and then visit
[ERROR] Unknown arguments: log, tail
is it supposed to be like this?
Wrangler pages deployment tail
*
(error) Error: Disallowed operation called within global scope. Asynchronous I/O (ex: fetch() or connect()), setting a timeout, and generating random values are not allowed within global scope. To fix this error, perform this operation within a handler. https://developers.cloudflare.com/workers/runtime-apis/handlers/
(error) TypeError: Cannot redefine property: __import_unsupported
@Anay-208 | Ping in replies Wrangler pages deployment tail
those are the errors i get
@Anay-208 | Ping in replies Can you send your <Latest /> component, because there is something where you are fetching some data
import Image from "next/image";
import { getRequestContext } from "@cloudflare/next-on-pages";
import Link from "next/link";
import { dateParser } from "../lib/dateparser";
import imageFetch from "@/app/lib/imagefetch";
import { Card, CardContent } from "@mui/material";

export const runtime = 'edge'

export default async function Latest() {
    const database = getRequestContext().env.DB;
    const { results } = await database.prepare("SELECT * FROM Blog").all();
    if (!results) {
        throw new Error("No posts available!");
    }
    const latest = results.slice(-1)
    let title: any;
    let url: any;
    let description: any;
    let date: any;
    title = latest[0].title;
    url = latest[0].url;
    description = latest[0].description;
    date = latest[0].date;
    let image = await imageFetch(`${latest[0].url}-cover`);

    return (
        <Link href={`/blog/${latest[0].url}`}>
            <Card sx={{ boxShadow: 3 }} className="!bg-slate-400 hover:shadow-2xl m-2 w-full h-96 mx-auto">
                <CardContent>
                    <Image 
                        src={image}
                        width={350}
                        height={200}
                        alt={title}
                    className="m-auto"/>
                    <strong>{title}</strong>
                    <p>{description}</p>
                    <p>{dateParser(date)}</p>
                    </CardContent>
            </Card>
        </Link>
    )
  }
@Anay-208 | Ping in replies Can you share image fetch component?
import Image from "next/image";
import imageFetch from "../lib/imagefetch";

export default async function ImageFetch({ url }: {url: any}) {
    const link = await imageFetch(`${url}-cover`)
    return (
        <Image 
            src={link}
            width={150}
            height={200}
            key={url}
            alt={url}
            className="mx-auto my-1 md:relative md:float-start md:m-1"
        />
    )
}

imageFetch.ts:
export default async function imageFetch(link: any) {
    return await getSignedUrl(S3, new GetObjectCommand({Bucket: 'blog', Key: `${link}.webp`}), { expiresIn: 3600 })
}
@Anay-208 | Ping in replies use aws4fetch
Alright, I will try it later, will update soon
to confirm, try removing S3 and see if it builds
Like, avoid using S3? Technically the app still builds with S3
Problem is with SDK
You can use the new cloudflare workers package which allows you to use node runtime
no, you can do any 1
its still in beta tho: https://opennext.js.org/cloudflare
but I still recommend aws4fetch to reduce cold starts
It’ll require additional config. You can just use aws4fetch
@Anay-208 | Ping in replies It’ll require additional config. You can just use aws4fetch
used aws4fetch, still getting internal server error on production site
can you check logs again?
@Anay-208 | Ping in replies can you check logs again?
still the same error
@Anay-208 | Ping in replies Can you send BlogCard
import { getRequestContext } from "@cloudflare/next-on-pages";
import { dateParser } from "../lib/dateparser";
import Link from "next/link";
import ImageFetch from "./imagefetch";
import { Card, CardContent } from "@mui/material";

export const runtime = 'edge'

export default async function BlogCard() {
    const database = getRequestContext().env.DB;
    const { results } = await database.prepare("SELECT * FROM Blog ORDER BY date DESC LIMIT 6").all();
    
    return results.map((page): JSX.Element => {
        let title: any = page.title
        let description: any = page.description;
        let date: any = page.date;
        let url: any = page.url;
        return (
            <Link href={`/blog/${page.url}`} key={`${page.title}`}>
                <Card sx={{ boxShadow: 3 }} className="!bg-slate-400 hover:shadow-2xl m-2 md:w-full w-[200px] md:min-h-[150px]">
                    <CardContent>
                    <ImageFetch url={url}/>
                        <div className="title">
                            <strong>{title}</strong>
                        </div>
                        <div className="description">
                            <p>{description}</p>
                        </div>
                        <p>{dateParser(date)}</p>
                    </CardContent>
                </Card>
            </Link>
        )
    })
}
i read some github issue on next-on-pages and they're also having the same issue
Answer
apparently it happens on next@15.2, which is my nextjs version too
one user said downgrading to next@15.1 temporarily mitigates the errors
Hmm, you can then downgrade to next@15.1.0, I last faced this error with s3 API, and not terminating postgres connection
I never use the latest version atleast for few weeks of it launch(unless there is a security vulnerability), because of these types of issues
okay, finally
it fixed all the 500 errors after downgrading to next@15.1
what's up with these breaking changes...
anyway, thank you for your help!
@Arya https://github.com/cloudflare/next-on-pages/issues/949
Can you mark this message as solution?