High TTFB with Pages

Hi! So, there're kinda two issues here. First one is a high TTFB, averaging 500-800ms for a static Nextjs website. I tried to fix that by creating a cache rule for the website in Cloudflare. However, after I had done that, in some occassions the website returns a blank page with no content. Refreshing a couple of times sometimes returns the correct content, looks like something went wrong with caching. However, is it normal that the TTFB is so high with CF Pages without any extra caching setup?
54 Replies
Bjarn
BjarnOP16mo ago
Guess it's something with the app router, if the connection is slow, the entire page is blank which I think is getting cached by some edge locations.
Bjarn
BjarnOP16mo ago
I do think there's an issue here tho... CF Pages Functions (for a static site) reply really slow
Chaika
Chaika16mo ago
You're using next-on-pages or static export? Link to your site?
Bjarn
BjarnOP16mo ago
Next-on-pages. I currently reenabled my cache rule so it's faster (https://spectate.net) but on the pages url it's slow: https://spectate-marketing-frontend.pages.dev/
Bjarn
BjarnOP16mo ago
You can see here where I disabled the cache rule, TTFB goes through the roof. Can't remember it was like that before without the cache rule
Chaika
Chaika16mo ago
next-on-pages is special and uses a function to handle all requests. TTFB is really low with CF Pages and static assets, but this isn't just static assets
Bjarn
BjarnOP16mo ago
That's what I thought too, the app itself (app.spectate.net) is a separate CF pages project, also Nextjs App Router with next-on-pages, its TTFB is quite low, total response takes 200-300ms including authentication etc. This website is just a static marketing site, it doesn't do any serverside stuff, started to happen when migrated to App Router though... so I guess that has definitely something to do with it and maaaaaybe I see something, will get back to it asap
Chaika
Chaika16mo ago
Is the site open source or any more information? I'd ask @Better James for his help if he has time, he is the resident nextjs person and maintainer of next-on-pages. Not really sure if there's enough information to go off though. Maybe something that changed recently or some known app router quirk Edit: opps, that was supposed to be a silent ping NotLikeThis could trry #next-on-pages as well
James
James16mo ago
My initial thoughts would be that your route is opted into the edge runtime and doing SSR stuff, maybe sending fetch requests that take a while to complete, before returning back to the client
Bjarn
BjarnOP16mo ago
Yea I see what's happening here Before with the pages dir, I had getStaticProps() During the rewrite to App Router, I of course removed that one. But still opted in to next-on-pages due to api routes. Note: all content is static from the CMS. But I guess it's now retrieving those as I haven't set the "dynamic" property, going to try to play with that I thought it wasn't necessary as it doesn't do any fetch or other server side calls but maybe (have to check) it's because I accidentally made the root layout an async function
James
James16mo ago
Generally my view is to only opt routes into the edge runtime that need to do work on the server, otherwise leave them opted out and they'll get prerendered at build time
Bjarn
BjarnOP16mo ago
Ahhh really? That's interesting, didn't know that, I just opted in everything to run on edge. Woops. Or at least the root layout, which forces sub segments to be edge too
Bjarn
BjarnOP16mo ago
I know this isn't CF pages related, but, I'm getting this now (which is why I always opted in for edge). These should only get the latest content from the CMS, they also have generateStaticParams. Am I missing something here with next-on-pages?
James
James16mo ago
Yeah so with dynamic routes that get prerendered, they generate a fallback nodejs function. To avoid generating a fallback function and to only prerender routes for the ones provided in generateStaticParams, you can do export const dynamicParams = false;
Bjarn
BjarnOP16mo ago
Ahhhhhhhhhhhh alright! I saw that in the docs indeed
Bjarn
BjarnOP16mo ago
They're SSG now, but still complains about edge (same error as before)
Bjarn
BjarnOP16mo ago
did you mean to also enable edge, no right?
James
James16mo ago
No Umm, are you sure you put the export and that you're returning paths in generateStaticParams?
Bjarn
BjarnOP16mo ago
Yep :S Very similar to getStaticPaths, and if I build and run locally it does get the paths
Bjarn
BjarnOP16mo ago
oh wait a sec, nope it's throwing 404's (I tested with edge still on a couple of mins ago woops) that's odd
James
James16mo ago
DD if it returns an empty array it'll try to generate a fallback function instead and ignore the route segment option saying not to 🙄
Bjarn
BjarnOP16mo ago
oh dang. this change... lolsob
Bjarn
BjarnOP16mo ago
Bjarn
BjarnOP16mo ago
^screenshot from the Prismic docs notice the difference.. woops. I'm still using the asLink helper which was required with getStaticPaths
James
James16mo ago
smh
Bjarn
BjarnOP16mo ago
The pain hahaha Aaaanyway, regarding the TTFB I found it pretty odd that the homepage (which isn't from the CMS, thus static) was so slow especially compared to my app itself which has auth etc
Bjarn
BjarnOP16mo ago
Bjarn
BjarnOP16mo ago
This is what I saw in the Function Metrics. The worker functions were having a hard time
James
James16mo ago
Oh my gosh
Bjarn
BjarnOP16mo ago
Also received a couple of "CPU limits exceeded" errors which I never experienced with my app with auth middlewares etc. Yea that was my reaction too 😂
Bjarn
BjarnOP16mo ago
For reference, this is the app ^
James
James16mo ago
Well, I've not seen someone that high before I'll be honest. I wonder if that was because it was all on the edge runtime maybe when it didnt need to be
Bjarn
BjarnOP16mo ago
Hmm, the app is also on the edge router for every page since I set it in the root layout
James
James16mo ago
Then my second message would that your root page is probably doing some heavy lifting
Bjarn
BjarnOP16mo ago
It was async, but didn't do anything. Obviously an issue, but don't think it would cause this high of a cpu time
James
James16mo ago
For comparison, my blog (all prerendered) ranges from taken 1ms-14ms. The app directory demo which has SSR ranges from 2ms to 120ms, depending on the route
Bjarn
BjarnOP16mo ago
Yep that's what I experience too with the app
James
James16mo ago
So you have to have been doing something on the root page that was taking years There is no chance in a million years that the routing we do would take that long
Bjarn
BjarnOP16mo ago
I'm fine sharing it tbh - https://gist.github.com/bjarn/9d54047240b005af1cf7b07cd9cb1468 Really that was it, I'm just as surprised haha the page itself didn't have functions either, but was async maybe that really is the issue I'm going to see and build again
James
James16mo ago
That's crazy also that's the layout not the actual page, i meant it was something on the page that was taking years not the layout, since the layout will apply to all the other routes that work fine
Bjarn
BjarnOP16mo ago
Yup, same for the page, doesn't do anything special https://gist.github.com/bjarn/565c926daabed9ef2d10a57082f03d82
James
James16mo ago
How bizarre
Bjarn
BjarnOP16mo ago
yuuup
Bjarn
BjarnOP16mo ago
Well, it's a nextjs thing for sure.
Bjarn
BjarnOP16mo ago
nothing to do with next-on-pages or CF Insane.
James
James16mo ago
😔
Bjarn
BjarnOP16mo ago
I can feel the homepage being slow when navigating too while other pages are instant wow. I found it You're not going to believe me
Bjarn
BjarnOP16mo ago
Bjarn
BjarnOP16mo ago
It's... an SVG (which is a component precompiled by svgr) On the website, you see that giant world map with all the dots? That's the one. That caused all the issues.
James
James16mo ago
Oh wow lmao, how unfortunate...
Bjarn
BjarnOP16mo ago
It contains too many layers, badly optimised never thought it could be the culprit. I guess nextjs was rendering it on the worker, causing these issues What a relieve haha, thanks btw to the both of you for helping out!
James
James16mo ago
Glad to hear all is well now 🙂
Bjarn
BjarnOP16mo ago
Much better 🙂
James
James16mo ago
😍
Want results from more Discord servers?
Add your server