Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

gatsby-plugin-sharp / gatsby-remark-images: Set destination directory with destinationDir option #27153

Closed
karlhorky opened this issue Sep 28, 2020 · 17 comments
Labels
topic: media Related to gatsby-plugin-image, or general image/media processing topics

Comments

@karlhorky
Copy link
Contributor

Summary

For the plugins gatsby-plugin-sharp and/or gatsby-remark-images (whichever makes sense and is necessary), an option destinationDir (string or function) that works similarly to the option in gatsby-remark-copy-linked-files...

// save `my-awesome-pdf.pdf` to `public/downloads/2a0039f3a61f4510f41678438e4c863a/my-awesome-pdf.pdf`
destinationDir: f => `downloads/${f.hash}/${f.name}`

// save `my-awesome-pdf.pdf` to `public/downloads/2a0039f3a61f4510f41678438e4c863a-my-awesome-pdf.pdf`
destinationDir: f => `downloads/${f.hash}-${f.name}`

// save `my-awesome-pdf.pdf` to `public/my-awesome-pdf/2a0039f3a61f4510f41678438e4c863a.pdf`
destinationDir: f => `${f.name}/${f.hash}`

...also with the new relativeDirectory property in the returned file object:

// save `my-awesome-pdf.pdf` to `public/original-path-in-src-pages/my-awesome-pdf.pdf`
destinationDir: f => `${f.relativeDirectory}/${f.name}`,

Basic example

See examples above.

Motivation

The structure of the files in the output public folder is more configurable and using the relativeDirectory option, can also match 1 to 1 the folder structure of the input src/pages directory. This makes the output easier to reason about and also easier to build tooling upon.

@karlhorky karlhorky added the type: feature or enhancement Issue that is not a bug and requests the addition of a new feature or enhancement. label Sep 28, 2020
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Sep 28, 2020
@sidharthachatterjee sidharthachatterjee added help wanted Issue with a clear description that the community can help with. and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Sep 28, 2020
@sidharthachatterjee
Copy link
Contributor

This makes a lot of sense. Happy to receive a PR adding this!

@karlhorky
Copy link
Contributor Author

Nice! Could you weigh in on where the changes should be made? In gatsby-plugin-sharp? Or gatsby-remark-images / gatsby-images? Or all three?

@wardpeet
Copy link
Contributor

wardpeet commented Oct 1, 2020

@karlhorky could you tell us more about tooling that would be beneficial here? I feel that adding an option destinationDir might cause more problems than it fixes.

Users can easily set it to downloads/${f.name} and will have issues with caching as we tell people to make these files immutable.

@wardpeet wardpeet closed this as completed Oct 1, 2020
@wardpeet wardpeet reopened this Oct 1, 2020
@wardpeet wardpeet added status: awaiting author response Additional information has been requested from the author and removed help wanted Issue with a clear description that the community can help with. labels Oct 1, 2020
@karlhorky
Copy link
Contributor Author

karlhorky commented Oct 1, 2020

@wardpeet in lieu of an Asset Manifest (see #20745), I would like the built assets to better match the inputs - eg. that output images be under the same paths as the input images:

input:
src/pages/original/path/my-awesome-image.jpg

output:
public/original/path/my-awesome-image-83j4g898hg98sheijf0239ijf9djsf9h39h98hre.jpg

This is in order to achieve server-side authentication and access control to the built assets - eg. #1100 (comment)

will have issues with caching as we tell people to make these files immutable

This is a good point, and maybe also something that should be applied to the destinationDir callback in gatsby-remark-copy-linked-files.

Maybe one way of doing this would be to require and validate that the content hash be used somewhere in the filename? We could do this by running the function and making sure that the argument is being used, à la:

const isValidFunction = `${dir({ name: `n`, hash: `h` })}` !== `${dir({})}`

But instead of validating that EITHER the name OR the hash is used, the validation could be stricter - that the hash is used.

@github-actions
Copy link

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here.
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 💪💜

@github-actions github-actions bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Oct 22, 2020
@karlhorky
Copy link
Contributor Author

not stale

@github-actions github-actions bot removed the stale? Issue that may be closed soon due to the original author not responding any more. label Oct 22, 2020
@vladar vladar removed the status: awaiting author response Additional information has been requested from the author label Nov 6, 2020
@github-actions
Copy link

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here.
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 💪💜

@github-actions github-actions bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Nov 27, 2020
@karlhorky
Copy link
Contributor Author

not stale

@github-actions github-actions bot removed the stale? Issue that may be closed soon due to the original author not responding any more. label Nov 27, 2020
@github-actions
Copy link

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here.
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 💪💜

@github-actions github-actions bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Dec 17, 2020
@karlhorky
Copy link
Contributor Author

lol such a stale bot

not stale

@github-actions github-actions bot removed the stale? Issue that may be closed soon due to the original author not responding any more. label Dec 18, 2020
@github-actions
Copy link

github-actions bot commented Jan 7, 2021

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here.
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 💪💜

@github-actions github-actions bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Jan 7, 2021
@wardpeet wardpeet added not stale and removed stale? Issue that may be closed soon due to the original author not responding any more. labels Jan 7, 2021
@wardpeet
Copy link
Contributor

wardpeet commented Jan 7, 2021

But instead of validating that EITHER the name OR the hash is used, the validation could be stricter - that the hash is used.

That's interesting, I'm still hesitant to enable this behavior as I don't fully understand the complications that this will bring. Technically we could start with adding outputDir to fluid, fixed, ...

Would it help if you got a list of assets per route?

@karlhorky
Copy link
Contributor Author

That's interesting, I'm still hesitant to enable this behavior as I don't fully understand the complications that this will bring.

Well, as long as the hash (and also versionHash, I'm just realizing) is included in the name somehow, I don't think this would cause issues. It would need to generate multiple different:

Original Path

pages/git-github/002-dvcs.png

Before

static/289ee13ff077319387caa5cb396c6cea/2a79a/002-dvcs.png
static/289ee13ff077319387caa5cb396c6cea/3b59c/002-dvcs.png
static/289ee13ff077319387caa5cb396c6cea/74866/002-dvcs.png

After (without relativeDirectory)

Here the first static part of the URL stays the same (cannot be changed through this API).

The name destinationDir doesn't actually make sense here, because it is actually ONLY changing the filename. But destinationDir also doesn't make sense in gatsby-remark-copy-linked-files...

Config: destinationDir: f => `${f.name}-${f.hash}-${f.versionHash}`

static/289ee13ff077319387caa5cb396c6cea/2a79a/002-dvcs-289ee13ff077319387caa5cb396c6cea-2a79a.png
static/289ee13ff077319387caa5cb396c6cea/3b59c/002-dvcs-289ee13ff077319387caa5cb396c6cea-3b59c.png
static/289ee13ff077319387caa5cb396c6cea/74866/002-dvcs-289ee13ff077319387caa5cb396c6cea-74866.png

After (with relativeDirectory)

Config: destinationDir: f => `${f.relativeDirectory}/${f.name}/${f.versionHash}/${f.name}-${f.hash}`

public/git-github/002-dvcs.png/2a79a/002-dvcs-289ee13ff077319387caa5cb396c6cea.png
public/git-github/002-dvcs.png/3b59c/002-dvcs-289ee13ff077319387caa5cb396c6cea.png
public/git-github/002-dvcs.png/74866/002-dvcs-289ee13ff077319387caa5cb396c6cea.png

@karlhorky
Copy link
Contributor Author

Would it help if you got a list of assets per route?

Sure, that would solve it a different way. That would be my request over at #20745

@wardpeet
Copy link
Contributor

wardpeet commented Jan 8, 2021

I feel there are more use-cases for a manifest file than there might be for renaming files. This would indicate that these files are part of a route, so part of webpack or page-data

@karlhorky
Copy link
Contributor Author

Yeah definitely, I think there are some use cases for sure for a full asset manifest.

Right now I'm using the information in webpack.stats.json, which gets me some things. But a unified file that contained everything in the webpack stats and also these new files would be great.

Back then I made a list of all the things that I found missing from webpack.stats.json.

@karlhorky
Copy link
Contributor Author

The project that I'm using it in is this Gatsby Server-side Authentication here: https://github.com/upleveled/gatsby-serverside-auth0

@LekoArts LekoArts added topic: media Related to gatsby-plugin-image, or general image/media processing topics and removed not stale type: feature or enhancement Issue that is not a bug and requests the addition of a new feature or enhancement. labels Aug 9, 2021
@gatsbyjs gatsbyjs locked and limited conversation to collaborators Aug 9, 2021
@LekoArts LekoArts closed this as completed Aug 9, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
topic: media Related to gatsby-plugin-image, or general image/media processing topics
Projects
None yet
Development

No branches or pull requests

5 participants