Skip to content
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

fix(gatsby-plugin-utils): path pieces too long and url safe base64 encoding #35160

Merged
merged 22 commits into from Apr 6, 2022

Conversation

veryspry
Copy link
Contributor

@veryspry veryspry commented Mar 17, 2022

Description

The PR solves two bugs:

  1. When downloading images with the gatsbyImage resolver locally, sometimes base64 encoded pieces would exceed system limits. The fix is to move to query parameters and use md5 hashes for the base64 pieces when an image is downloaded and saved to disk during build time.
  2. The base64 spec includes forward slashes making it unsuitable for use in URL's, with the query param approach we can ditch base64

Related Issues

Fixes #35126

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Mar 17, 2022
@wardpeet wardpeet changed the title fix(gatsbyImage resolver): path pieces too long and url safe base64 encoding fix(gatsby-plugin-utils): path pieces too long and url safe base64 encoding Mar 18, 2022
@LekoArts LekoArts added topic: core Relates to Gatsby's core (e.g. page loading, reporter, state machine) and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Mar 18, 2022
@wardpeet
Copy link
Contributor

Sharing a bit on why I changed the code here. Image Url & args can indeed be any value like @veryspry did. Instead of creating our own md5 hash, we use createContentDigest (which is also an md5 hash but native to Gatsby). For url and args we will use query string parameters as they can be any length and keep code between image-cdn and local images the same.

@TylerBarnes
Copy link
Contributor

TylerBarnes commented Mar 18, 2022

@wardpeet for files on disk (with image CDN turned off) wont the query args not work?

EDIT: I see you have an md5 in the file path with the args - that makes sense 👍 I can see how it works now

@wardpeet wardpeet force-pushed the veryspry/image-cdn-long-filename branch 2 times, most recently from 0675146 to 52ab2ca Compare March 18, 2022 21:42
Copy link
Contributor

@LekoArts LekoArts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Judging by the tests this looks good

@wardpeet wardpeet force-pushed the veryspry/image-cdn-long-filename branch from 9c96d24 to 3d98ed4 Compare March 30, 2022 20:10
TylerBarnes
TylerBarnes previously approved these changes Apr 1, 2022
@LiamMartens
Copy link

Just bumping this because we are running into this issue as well 👍

@wardpeet wardpeet merged commit 3f12544 into master Apr 6, 2022
@wardpeet wardpeet deleted the veryspry/image-cdn-long-filename branch April 6, 2022 13:51
@davidpaulsson
Copy link

If I want to test this without the ERROR ENAMETOOLONG: name too long, mkdir error happening, is there a specific pkg or version I should set a resolution to?

@LekoArts
Copy link
Contributor

@davidpaulsson You can use the @next versions

@thekage91
Copy link

thekage91 commented Apr 21, 2022

Hi, I run into this issue during the building of my site. I retrieve datas from WordPress and the process fails during gatsby.IMAGE_CDN job which return this error

UNHANDLED REJECTION ENAMETOOLONG: name too long, mkdir '/Users/davide/Workspace/GitHub-Repo/ecommerce-scraper-frontend/packages/blog/public/_gatsby/image/aHR0cHM6Ly9yZXMuY2xvdWRpbmFyeS5jb20vcHJpY3lvL2ltYWdlcy9mX2F1dG8scV9hdXRvL3YxNjQ0NzAxMzExL3ZpbmlwZXJ
iYWNjby1tYXRyaWNlLWNvbmZyb250by1wb3NpemlvbmFtZW50aS1nZW5uYWlvLXZzLWx1Zy92aW5pcGVyYmFjY28tbWF0cmljZS1jb25mcm9udG8tcG9zaXppb25hbWVudGktZ2VubmFpby12cy1sdWcucG5nP19pPUFB'



  WorkerError: ENAMETOOLONG: name too long, mkdir '/Users/davide/Workspace/GitHub-Repo/ecommerce-scraper-frontend/packages/blog/public/_gatsby/image/aHR0cHM6Ly9yZXMuY2xvdWRpbmFyeS5jb20vcHJpY3lvL2ltYWdlcy9mX2F1dG8scV9hdXRvL3YxNjQ0NzAxMzExL3ZpbmlwZXJiYWNj
  by1tYXRyaWNlLWNvbmZyb250by1wb3NpemlvbmFtZW50aS1nZW5uYWlvLXZzLWx1Zy92aW5pcGVyYmFjY28tbWF0cmljZS1jb25mcm9udG8tcG9zaXppb25hbWVudGktZ2VubmFpby12cy1sdWcucG5nP19pPUFB'
  
  - manager.ts:311 enqueueJob
    [ecommerce-scraper-frontend]/[gatsby]/src/utils/jobs/manager.ts:311:21

I have "gatsby": "^4.14.0-next.0" version but the problem still persist :(

Can anyone help me?
Thank you

@veryspry
Copy link
Contributor Author

Hi, I run into this issue during the building of my site. I retrieve datas from WordPress and the process fails during gatsby.IMAGE_CDN job which return this error

UNHANDLED REJECTION ENAMETOOLONG: name too long, mkdir '/Users/davide/Workspace/GitHub-Repo/ecommerce-scraper-frontend/packages/blog/public/_gatsby/image/aHR0cHM6Ly9yZXMuY2xvdWRpbmFyeS5jb20vcHJpY3lvL2ltYWdlcy9mX2F1dG8scV9hdXRvL3YxNjQ0NzAxMzExL3ZpbmlwZXJ
iYWNjby1tYXRyaWNlLWNvbmZyb250by1wb3NpemlvbmFtZW50aS1nZW5uYWlvLXZzLWx1Zy92aW5pcGVyYmFjY28tbWF0cmljZS1jb25mcm9udG8tcG9zaXppb25hbWVudGktZ2VubmFpby12cy1sdWcucG5nP19pPUFB'



  WorkerError: ENAMETOOLONG: name too long, mkdir '/Users/davide/Workspace/GitHub-Repo/ecommerce-scraper-frontend/packages/blog/public/_gatsby/image/aHR0cHM6Ly9yZXMuY2xvdWRpbmFyeS5jb20vcHJpY3lvL2ltYWdlcy9mX2F1dG8scV9hdXRvL3YxNjQ0NzAxMzExL3ZpbmlwZXJiYWNj
  by1tYXRyaWNlLWNvbmZyb250by1wb3NpemlvbmFtZW50aS1nZW5uYWlvLXZzLWx1Zy92aW5pcGVyYmFjY28tbWF0cmljZS1jb25mcm9udG8tcG9zaXppb25hbWVudGktZ2VubmFpby12cy1sdWcucG5nP19pPUFB'
  
  - manager.ts:311 enqueueJob
    [ecommerce-scraper-frontend]/[gatsby]/src/utils/jobs/manager.ts:311:21

I have "gatsby": "^4.14.0-next.0" version but the problem still persist :(

Can anyone help me? Thank you

Hey @thekage91 👋

For some extra context, this PR made a change to md5 hash some of the image path segments via the gatsby createContentHash helper function instead of base64 encode them.

Taking a look at the path in your error:

'/Users/davide/Workspace/GitHub-Repo/ecommerce-scraper-frontend/packages/blog/public/_gatsby/image/aHR0cHM6Ly9yZXMuY2xvdWRpbmFyeS5jb20vcHJpY3lvL2ltYWdlcy9mX2F1dG8scV9hdXRvL3YxNjQ0NzAxMzExL3ZpbmlwZXJ
iYWNjby1tYXRyaWNlLWNvbmZyb250by1wb3NpemlvbmFtZW50aS1nZW5uYWlvLXZzLWx1Zy92aW5pcGVyYmFjY28tbWF0cmljZS1jb25mcm9udG8tcG9zaXppb25hbWVudGktZ2VubmFpby12cy1sdWcucG5nP19pPUFB'

I extract the last piece:

aHR0cHM6Ly9yZXMuY2xvdWRpbmFyeS5jb20vcHJpY3lvL2ltYWdlcy9mX2F1dG8scV9hdXRvL3YxNjQ0NzAxMzExL3ZpbmlwZXJiYWNjby1tYXRyaWNlLWNvbmZyb250by1wb3NpemlvbmFtZW50aS1nZW5uYWlvLXZzLWx1Zy92aW5pcGVyYmFjY28tbWF0cmljZS1jb25mcm9udG8tcG9zaXppb25hbWVudGktZ2VubmFpby12cy1sdWcucG5nP19pPUFB

This will either be a base64 encoded version of the source image URL OR an md5 hash of the image source URL depending on the Gatsby version you are on.

If I base64 decode the string:

echo "aHR0cHM6Ly9yZXMuY2xvdWRpbmFyeS5jb20vcHJpY3lvL2ltYWdlcy9mX2F1dG8scV9hdXRvL3YxNjQ0NzAxMzExL3ZpbmlwZXJiYWNjby1tYXRyaWNlLWNvbmZyb250by1wb3NpemlvbmFtZW50aS1nZW5uYWlvLXZzLWx1Zy92aW5pcGVyYmFjY28tbWF0cmljZS1jb25mcm9udG8tcG9zaXppb25hbWVudGktZ2VubmFpby12cy1sdWcucG5nP19pPUFB" | base64 --decode

The operation is successful and I get this back which tells me that the version of gatsby you have installed does not include this fix.

https://res.cloudinary.com/pricyo/images/f_auto,q_auto/v1644701311/viniperbacco-matrice-confronto-posizionamenti-gennaio-vs-lug/viniperbacco-matrice-confronto-posizionamenti-gennaio-vs-lug.png?_i=AA%                                        

Can you try using the explicit @next version of Gatsby? I'm wondering if the explicit version you used, ^4.14.0-next.0, just doesn't have this fix included.

Let me know if this helps and if not, feel free to ping me back here and hopefully we can get this sorted out 🙂

mwfrost pushed a commit to mwfrost/gatsby that referenced this pull request Apr 20, 2023
…coding (gatsbyjs#35160)

Co-authored-by: Ward Peeters <ward@coding-tech.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: core Relates to Gatsby's core (e.g. page loading, reporter, state machine)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants