Skip to content

Slow TTFB on initial document request #552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lindsaylevine opened this issue Jul 23, 2021 · 34 comments
Closed

Slow TTFB on initial document request #552

lindsaylevine opened this issue Jul 23, 2021 · 34 comments
Labels
priority: medium type: bug code to address defects in shipped code

Comments

@lindsaylevine
Copy link

this is moving the conversation that started on community to here so we can explore if/how the plugin is contributing to or not addressing this behavior.

@lindsaylevine lindsaylevine added type: bug code to address defects in shipped code priority: medium labels Jul 23, 2021
@joonassandell
Copy link

Hi, what is the status on this?

Because of the slow ttfb and the fact that we can't get the ISR working makes our service feel extremely slow in Netlify :(

Thanks!

@saeedseyfi
Copy link

saeedseyfi commented Oct 12, 2021

It seems the issue is from Nextjs itself. When the page needs to be server side rendered (has getServerSideProps or getInitialProps).

I had to disable server side rendering completely to make nextjs respond quickly.

Related to:
https://stackoverflow.com/questions/63595384/next-js-initial-page-loading-time-is-too-slow-compared-to-subsequent-page-loads
vercel/next.js#1013
vercel/next.js#9367

@koraysels
Copy link

I have the same issue and am not using getInitialPropsSetupor getServerSideProps anywhere...
consistently 5s load times through netlify

@Joroze
Copy link

Joroze commented Oct 30, 2021

I have the same issue and am not using getInitialPropsSetupor getServerSideProps anywhere...

consistently 5s load times through netlify

Can confirm. This occurs for me as well without using any SSR in my app.

I'm using:

  • Netlify NextJS Plugin v4
  • NextJS v12.1

Might have to consider switching to other jamstack service providers until this is addressed.

@tbgse
Copy link

tbgse commented Dec 14, 2021

Hi @ascorbic sorry for tagging you directly here but do you maybe know if this is still a known issue? Since we're exploring netlify as an alternative for hosting at the moment this might be a deal breaker for us and there seems to be no clear answer here. Not sure if lindsay is still active in this project.

@ascorbic
Copy link
Contributor

Hi @tbgse . There is no specific reason why this would be slower on Netlify than anywhere else. We are releasing version 4 today which is a full rewrite of the plugin and includes ISR support, so it would make sense to try that out and see if it can be reproduced. If anybody still is experiencing slow loading, please share a link to the page in question, and also say whether it's slow when running locally too.

@rshackleton
Copy link

rshackleton commented Jun 9, 2022

We've recently launched a new Next.js site and we're seeing a ~1-2s initial server response time on PSI for the home page.

The home page is using getStaticProps with no revalidation enabled so should be served as a purely static page, therefore I don't understand how there could be a high server response time.

Timing from PSI:
image

Timing from MS Edge DevTools:
image

The site is running NextJS v12.0.7 and @netlify/plugin-nextjs v4.2.7

@ascorbic
Copy link
Contributor

@rshackleton can you share the build logs?

@rshackleton
Copy link

@ascorbic can I email you them somehow to save me having to edit out client names etc?

@ascorbic
Copy link
Contributor

@rshackleton you could share a link to them here and I could access them, if that's simpler

@rshackleton
Copy link

@ascorbic we use our own CI to build the site so I can't share a link to the logs, it would be easier to email or DM them

@ascorbic
Copy link
Contributor

Ah ok. You can email them to matt.kane @ netlify.com

@rshackleton
Copy link

@ascorbic I have emailed the latest build log to you, hope that helps!

@ascorbic
Copy link
Contributor

Thanks, @rshackleton
The critical part of the logs are these:

2022-06-13T11:53:27.6622760Z Moving static page files to serve from CDN...
2022-06-13T11:53:27.6756342Z Moved 0 files
2022-06-13T11:53:27.6758547Z Skipped moving 6 files because they match redirects or beforeFiles rewrites, so cannot be deployed to the CDN and will be served from the origin instead.
2022-06-13T11:53:27.6759389Z The following files matched redirects and were not moved to the CDN:
2022-06-13T11:53:27.6759770Z 
2022-06-13T11:53:27.6759908Z - index.html
2022-06-13T11:53:27.6760738Z - index.json
2022-06-13T11:53:27.6760980Z - products.html
2022-06-13T11:53:27.6762613Z - products.json
2022-06-13T11:53:27.6762955Z - sitemap.html
2022-06-13T11:53:27.6763241Z - sitemap.json

Do you have rewrites or redirects that match these? If so, we can't serve them from the CDN, because they need to run the rewrites or redirects in front of them. Could you share the relevant rewrites and redirects from your next config?

@rshackleton
Copy link

rshackleton commented Jun 13, 2022

@ascorbic we have a redirect which needs to run for all requests to serve an unsupported browser page for ie11. This is using the next.js redirects as Netlify don't support redirecting based on user-agent because it isn't best practice (I don't argue this, but ie11 is ancient and user-agent sniffing for that one browser is arguably the best way to handle it).

  async redirects() {
    return [
      {
        source: '/:path((?!unsupported.html$).*)',
        destination: '/unsupported.html',
        has: [
          {
            type: 'header',
            key: 'User-Agent',
            value: '(.*Trident.*)',
          },
        ],
        permanent: false,
      },
    ];
  },
  async rewrites() {
    return [
      {
        source: '/robots.txt',
        destination: '/api/robots',
      },
      {
        source: '/sitemap.xml',
        destination: '/api/sitemap',
      },
    ];
  },

If Netlify had allowed redirecting based on user-agent then we wouldn't need to use the option in nextjs which I assume means all static pages aren't served statically?

@ascorbic
Copy link
Contributor

Ah, I understand. This is soemthing you could do with an Edge Function. We will be handling this automatically when we shift all of our Next rewrite, redirect and header handling to the edge, but for now you could create a simple function that achieves this. SOmething like:

export default async (request, context) => {
  if(request.headers.get('user-agent')?.includes('Trident')) {
    return context.rewrite("/unsupported.html");
  }
};

@rshackleton
Copy link

rshackleton commented Jun 13, 2022

Can't quite get that to work, I've added a new edge handler function as per the docs. It worked locally when running netlify dev but won't work when deployed to Netlify via the CLI. The "Edge Functions" part of the Netlify back end hasn't updated to indicate it has recognised any functions either.

The build log includes the edge function bundling:

2022-06-13T19:27:36.7723026Z ────────────────────────────────────────────────────────────────
2022-06-13T19:27:36.7723588Z   5. Edge Functions bundling                                    
2022-06-13T19:27:36.7723852Z ────────────────────────────────────────────────────────────────
2022-06-13T19:27:36.7725226Z ​
2022-06-13T19:27:36.7740128Z Packaging Edge Functions from netlify\edge-functions directory:
2022-06-13T19:27:36.7740538Z  - unsupported
2022-06-13T19:27:37.4956126Z ​
2022-06-13T19:27:37.4956684Z (Edge Functions bundling completed in 723ms)

@ascorbic
Copy link
Contributor

ascorbic commented Jun 14, 2022

Have you listed it in your netlify.toml?

@rshackleton
Copy link

rshackleton commented Jun 14, 2022

Yeah. I have the file here:

image

And the Netlify.toml includes it like so:

[build]
  command = "yarn build"
  publish = ".next"

[[plugins]]
  package = "@netlify/plugin-nextjs"

[functions]
  node_bundler = "esbuild"

[[edge_functions]]
  path = "/*"
  function = "unsupported"

@ascorbic
Copy link
Contributor

Hmm. Could you share the deployment part of the logs too?

@rshackleton
Copy link

Yep, I have emailed you the log for the netlify deployment

@ascorbic
Copy link
Contributor

Thanks. I'm looking into it

@rshackleton
Copy link

@ascorbic have you been able to make any progress with this?

@rshackleton
Copy link

@ascorbic sorry to keep chasing but this is a pretty significant issue for our client

@ascorbic
Copy link
Contributor

Hi @rshackleton. Sorry about the time this is taking. I did some digging into it to no avail so far. To help me diagnose it, could you share the content of .netlify/edge-functions-dist/manifest.json?

@ascorbic
Copy link
Contributor

(This is after running a build)

@rshackleton
Copy link

rshackleton commented Jun 28, 2022

@ascorbic embarrassingly I think I know why it isn't working, we're deploying with an older version of the CLI. 🤦‍♂️

I'm going to look at updating to the latest version and will check back if it works.

EDIT: Ignore, I've checked and we did update to 10.5.1 on our preview deployments to test the edge functions so it isn't quite my incompetence yet.

@rshackleton
Copy link

rshackleton commented Jun 28, 2022

@ascorbic I've run netlify build locally after updated to the latest CLI (from 10.5.1 to 10.6.3) and this is the content of the manifest.json file:

{
  "bundles": [
    {
      "asset": "684f0e2b3220d34da028612342218199150bba860ea8c31c138332c73d37f9be.js",
      "format": "js"
    }
  ],
  "routes": [
    {
      "function": "unsupported",
      "pattern": "^/.*/?$"
    }
  ],
  "bundler_version": "1.4.1"
}

There is also a file called 684f0e2b3220d34da028612342218199150bba860ea8c31c138332c73d37f9be.js inside the same folder.

EDIT: Also confirmed this same file is output when running the netlify deploy --build command on our CI server

@ascorbic
Copy link
Contributor

OK, and to confirm the deploy logs don't show it as uploading the edge function?

@rshackleton
Copy link

I can't see anything indicating it is uploading anything other than the 3 functions normally created by the nextjs plugin.

2022-06-28T14:58:24.8842491Z ────────────────────────────────────────────────────────────────
2022-06-28T14:58:24.8842637Z   4. Functions bundling                                         
2022-06-28T14:58:24.8842801Z ────────────────────────────────────────────────────────────────
2022-06-28T14:58:24.8842974Z ​
2022-06-28T14:58:24.8884926Z Packaging Functions from .netlify\functions-internal directory:
2022-06-28T14:58:24.8885394Z  - _ipx\_ipx.js
2022-06-28T14:58:24.8885794Z  - ___netlify-handler\___netlify-handler.js
2022-06-28T14:58:24.8886043Z  - ___netlify-odb-handler\___netlify-odb-handler.js
2022-06-28T14:58:24.8888470Z ​
2022-06-28T14:58:42.7500547Z ​
2022-06-28T14:58:42.7506754Z (Functions bundling completed in 17.8s)
2022-06-28T14:58:42.7525032Z ​
2022-06-28T14:58:42.7525722Z ────────────────────────────────────────────────────────────────
2022-06-28T14:58:42.7526814Z   5. Edge Functions bundling                                    
2022-06-28T14:58:42.7527329Z ────────────────────────────────────────────────────────────────
2022-06-28T14:58:42.7527550Z ​
2022-06-28T14:58:42.7541480Z Packaging Edge Functions from netlify\edge-functions directory:
2022-06-28T14:58:42.7541885Z  - unsupported
2022-06-28T14:58:45.3635008Z ​
2022-06-28T14:58:45.3635928Z (Edge Functions bundling completed in 2.6s)
2022-06-28T14:58:45.3642402Z ​
2022-06-28T14:58:45.3643449Z ────────────────────────────────────────────────────────────────
2022-06-28T14:58:45.3644156Z   6. @netlify/plugin-nextjs (onPostBuild event)                 
2022-06-28T14:58:45.3644542Z ────────────────────────────────────────────────────────────────
2022-06-28T14:58:45.3644912Z ​
2022-06-28T14:58:45.5304613Z Next.js cache saved.
2022-06-28T14:58:45.5558145Z ​
2022-06-28T14:58:45.5558435Z (@netlify/plugin-nextjs onPostBuild completed in 189ms)
2022-06-28T14:58:45.5575583Z ​
2022-06-28T14:58:45.5575798Z ────────────────────────────────────────────────────────────────
2022-06-28T14:58:45.5575952Z   Netlify Build Complete                                        
2022-06-28T14:58:45.5576106Z ────────────────────────────────────────────────────────────────
2022-06-28T14:58:45.5576689Z ​
2022-06-28T14:58:45.5576877Z (Netlify Build completed in 1m 19.7s)
2022-06-28T14:58:45.5586648Z �[32mDeploy path: �[39m       C:\agent\_work\r3\a\###\drop\packages\packages\web\.next
2022-06-28T14:58:45.5586993Z �[32mConfiguration path: �[39mC:\agent\_work\r3\a\###\drop\packages\packages\web\netlify.toml
2022-06-28T14:58:46.0050324Z ​
2022-06-28T14:58:46.0051216Z Warning: some redirects have syntax errors:
2022-06-28T14:58:46.0051672Z ​
2022-06-28T14:58:46.0052209Z Could not parse redirect line 994:
2022-06-28T14:58:46.0052654Z   /laser-world-of-photonics-preview/	media/	301
2022-06-28T14:58:46.0053410Z The destination path/URL must start with "/", "http:" or "https:"
2022-06-28T14:58:46.0327504Z Deploying to main site URL...
2022-06-28T14:58:46.5494188Z - Hashing files...
2022-06-28T14:58:46.5521863Z - Looking for a functions cache...
2022-06-28T14:58:46.5532387Z √ Deploying functions from cache (use --skip-functions-cache to override)
2022-06-28T14:58:46.8821422Z √ Finished hashing 258 files and 3 functions
2022-06-28T14:58:46.8821676Z - CDN diffing files...
2022-06-28T14:58:48.2302504Z √ CDN requesting 24 files and 2 functions
2022-06-28T14:58:48.2304627Z - Uploading 27 files
2022-06-28T14:59:09.7868683Z √ Finished uploading 27 assets
2022-06-28T14:59:09.7872238Z - Waiting for deploy to go live...
2022-06-28T14:59:16.3012429Z √ Deploy is live!

@ascorbic
Copy link
Contributor

Thanks. So it's still that while the edge function is being correctly built and added to the manifest, it's not being uploaded. I'm going to consult with the team that handles the CLI here.

@ascorbic
Copy link
Contributor

ascorbic commented Jul 1, 2022

@rshackleton This should be fixed in [email protected]. Could you upgrade and confirm? Thanks for your help and patience with this.

@rshackleton
Copy link

@ascorbic that has resolved the issue for us, the edge functions are being deployed correctly. Thanks. :)

@ascorbic
Copy link
Contributor

ascorbic commented Jul 6, 2022

Awesome! Thanks for the update

@ascorbic ascorbic closed this as completed Jul 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: medium type: bug code to address defects in shipped code
Projects
None yet
Development

No branches or pull requests

8 participants