Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

503 when using image optimization #840

Closed
arelaxend opened this issue Dec 8, 2020 · 17 comments · Fixed by #841
Closed

503 when using image optimization #840

arelaxend opened this issue Dec 8, 2020 · 17 comments · Fixed by #841

Comments

@arelaxend
Copy link

arelaxend commented Dec 8, 2020

Hi there,

Is your feature request related to a problem? Please describe.
Still, we check @sls-next/[email protected] and for now we get 503 on images when we provide an url for an image for a specific listed domain. We get the same result for images located under /public

We setup next.config.js with the following:

images: {
    deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
    imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
    domains: ["stream.domain.com"],
    path: '/_next/image',
    loader: 'default',
  }

For:

<Image
            src={`https://stream.domain.com/public/image.jpg`}
            width={300}
            height={300}
            alt="my image"
            layout={`responsive`}
            priority={true}
            loading="eager"
 />

On s3 we see yet no file under _next/images

Describe the solution you'd like
That is create the optimized images in s3 under _next/images

Additional

We also found this in cloudwatch logs.

Capture d’écran 2020-12-08 à 14 05 09

A.

@dphang
Copy link
Collaborator

dphang commented Dec 8, 2020

If you check the image lambda output, just checking, are the node_modules and sharp correctly copied there? I had prebuilt modules for Lambda so it should just copy those. Also make sure the function is running Node 12.x as that is what is mentioned in the Sharp installation page. (If this fixes, will update the docs to mention it)

@jonahallibone
Copy link

jonahallibone commented Dec 8, 2020

@dphang +1 for this issue as well. Config is basic as follows:

module.exports = {
  images: {
    domains: ["prefix.cloudfront.net"],
  },
};

@dphang
Copy link
Collaborator

dphang commented Dec 8, 2020

Yea, this is strange, for some reason the libvips binaries are now not being copied over to the Lambda when I deploy (node_modules/sharp/vendor/). Trying to investigate why that's the case.

@dphang
Copy link
Collaborator

dphang commented Dec 8, 2020

I think when running it locally, it copies the vendor libs correctly, but when doing an npm publish, it seems to exclude certain files. I think if we update the files property to explicitly whitelist everything in sharp_node_modules it, it should work.

@dphang
Copy link
Collaborator

dphang commented Dec 8, 2020

I think it is because the package.json in sharp module doesn't include it (for good reason, since there are many binaries and it adds to the package). However, since we do need it and we only have Lambda Node.js 12.x platform, I think it is fine to modify it to include it (~20MB, or ~7 MB compressed). Added PR here: #840, hoping it works this time.

@dphang
Copy link
Collaborator

dphang commented Dec 8, 2020

It should be fixed now in@sls-next/[email protected] after also include 7256ab9. Will try to optimize/bundle this better in the future, but it should be workable for now. Let me know if there are other issues.

@dphang dphang closed this as completed Dec 8, 2020
@jonahallibone
Copy link

@dphang I am deploying with the new alpha as we speak, will report back

@snowjang24
Copy link

snowjang24 commented Dec 9, 2020

@dphang I have same issue, but only works on svg files, my png file has 503 error and cant load all

<Image src="/main1-2.png" width={582} height={287} loading="lazy" />
<Image src="/logo.svg" width={24} height={24} loading="lazy" />

image

@dphang dphang reopened this Dec 9, 2020
@dphang
Copy link
Collaborator

dphang commented Dec 9, 2020

@snowjang24 thanks for reporting. I'll leave this open for a few days so I can easily track any bugs. Could you please provide more details like Next.js version, Serverless-next.js version? As mentioned above I did push a couple of fixes with 1.19.0-alpha.19 so the optimizer should work fine now. For SVG files, it doesn't go through the Sharp optimization at all since it doesn't need to (it's a vector type) (see: https://github.com/serverless-nextjs/serverless-next.js/blob/master/packages/libs/lambda-at-edge/src/images/imageOptimizer.ts#L264-L267)

If you are optimizing /example.png then it should be stored in S3 in public/example.png.

What kind of URL are you optimizing? Is it absolute URL or relative URL (in public folder?) What do CloudWatch logs say about the 503 errors? 503 is very generic so it will be more helpful to have more details. Thanks!

@snowjang24
Copy link

@dphang

My dependencies is

  • Next - 10.0.3
  • serverless-next - 1.19.0-alpha.16

svg files and png files in the same directory, like public/example.png
in this case still svg works, png doesn't works

I tried to optimize the relative URL.


I saw your comment and upgrade to alpha.19 and now it works well for all png and svg ....😭

Thanks for your kind comment

@arelaxend
Copy link
Author

arelaxend commented Dec 9, 2020

It should be fixed now in@sls-next/[email protected] after also include 7256ab9. Will try to optimize/bundle this better in the future, but it should be workable for now. Let me know if there are other issues.

@dphang 👍 🥇

@jonahallibone
Copy link

Unsure if this is a specific to this lib or to Next.js in general, but it seems sometimes the conversions fail and then the server returns a 503 for the image every time after that point.

@dphang
Copy link
Collaborator

dphang commented Dec 10, 2020

@jonahallibone not sure what's going on there, please post more details such as CloudWatch logs.

@kylekirkby
Copy link

Thanks, @dphang! Working now. Any suggestions on how we can stop images from being resized on every deployment of our apps? Looks like the /* rule is being used for clearing the CF cache. AFAIK this means that the images will be re-optimized on a subsequent deployment?

@dphang
Copy link
Collaborator

dphang commented Dec 11, 2020

@kylekirkby yes, the entire cache is invalidated by default, which does include the images path. Please take a look at CloudFront paths section: https://github.com/serverless-nextjs/serverless-next.js#custom-cloudfront-configuration if you would like to specify exact paths. Note that if you do this and exclude all images, if you update an image of the same name, you'll have to ensure you invalidate the cache yourself for that specific file (either manually or using the AWS SDK), otherwise users may see the old image.

@dphang dphang closed this as completed Dec 22, 2020
@dphang
Copy link
Collaborator

dphang commented Dec 22, 2020

Closing since there were no additional reported issues.

@jahlherapath
Copy link

Hey I'm getting a 401 on my images on production:

Next - 10.2.3
serverless-next - @sls-next/[email protected]

Any idea?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
6 participants